[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/Generator/ -> ParameterGenerator.php (source)

   1  <?php
   2  /*
   3   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   4   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   5   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   6   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   7   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   8   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   9   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14   *
  15   * This software consists of voluntary contributions made by many individuals
  16   * and is licensed under the MIT license.
  17   */
  18  
  19  namespace ProxyManager\Generator;
  20  
  21  use ReflectionException;
  22  use Zend\Code\Generator\ParameterGenerator as ZendParameterGenerator;
  23  use Zend\Code\Generator\ValueGenerator;
  24  use Zend\Code\Reflection\ParameterReflection;
  25  
  26  /**
  27   * Parameter generator that ensures that the parameter type is a FQCN when it is a class
  28   *
  29   * @author Marco Pivetta <ocramius@gmail.com>
  30   * @license MIT
  31   */
  32  class ParameterGenerator extends ZendParameterGenerator
  33  {
  34      /**
  35       * @override - uses `static` to instantiate the parameter
  36       *
  37       * {@inheritDoc}
  38       */
  39      public static function fromReflection(ParameterReflection $reflectionParameter)
  40      {
  41          /* @var $param self */
  42          $param = new static();
  43  
  44          $param->setName($reflectionParameter->getName());
  45          $param->setPosition($reflectionParameter->getPosition());
  46  
  47          $type = self::extractParameterType($reflectionParameter);
  48  
  49          if (null !== $type) {
  50              $param->setType($type);
  51          }
  52  
  53          self::setOptionalParameter($param, $reflectionParameter);
  54  
  55          $param->setPassedByReference($reflectionParameter->isPassedByReference());
  56  
  57          return $param;
  58      }
  59  
  60      /**
  61       * Retrieves the type of a reflection parameter (null if none is found)
  62       *
  63       * @param ParameterReflection $reflectionParameter
  64       *
  65       * @return string|null
  66       */
  67      private static function extractParameterType(ParameterReflection $reflectionParameter)
  68      {
  69          if ($reflectionParameter->isArray()) {
  70              return 'array';
  71          }
  72  
  73          if (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
  74              return 'callable';
  75          }
  76  
  77          if ($typeClass = $reflectionParameter->getClass()) {
  78              return $typeClass->getName();
  79          }
  80  
  81          return null;
  82      }
  83  
  84      /**
  85       * @return string
  86       */
  87      public function generate()
  88      {
  89          return $this->getGeneratedType()
  90              . (true === $this->passedByReference ? '&' : '')
  91              . '$' . $this->name
  92              . $this->generateDefaultValue();
  93      }
  94  
  95      /**
  96       * @return string
  97       */
  98      private function generateDefaultValue()
  99      {
 100          if (null === $this->defaultValue) {
 101              return '';
 102          }
 103  
 104          $defaultValue = $this->defaultValue instanceof ValueGenerator
 105              ? $this->defaultValue
 106              : new ValueGenerator($this->defaultValue);
 107  
 108          $defaultValue->setOutputMode(ValueGenerator::OUTPUT_SINGLE_LINE);
 109  
 110          return ' = ' . $defaultValue;
 111      }
 112  
 113      /**
 114       * Retrieves the generated parameter type
 115       *
 116       * @return string
 117       */
 118      private function getGeneratedType()
 119      {
 120          if (! $this->type || in_array($this->type, static::$simple)) {
 121              return '';
 122          }
 123  
 124          if ('array' === $this->type || 'callable' === $this->type) {
 125              return $this->type . ' ';
 126          }
 127  
 128          return '\\' . trim($this->type, '\\') . ' ';
 129      }
 130  
 131      /**
 132       * Set the default value for a parameter (if it is optional)
 133       *
 134       * @param ZendParameterGenerator $parameterGenerator
 135       * @param ParameterReflection    $reflectionParameter
 136       */
 137      private static function setOptionalParameter(
 138          ZendParameterGenerator $parameterGenerator,
 139          ParameterReflection $reflectionParameter
 140      ) {
 141          if ($reflectionParameter->isOptional()) {
 142              try {
 143                  $parameterGenerator->setDefaultValue($reflectionParameter->getDefaultValue());
 144              } catch (ReflectionException $e) {
 145                  $parameterGenerator->setDefaultValue(null);
 146              }
 147          }
 148      }
 149  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1