[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Generator/ -> ParameterGenerator.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Code\Generator;
  11  
  12  use ReflectionParameter;
  13  use Zend\Code\Reflection\ParameterReflection;
  14  
  15  use function is_string;
  16  use function method_exists;
  17  use function str_replace;
  18  use function strtolower;
  19  
  20  class ParameterGenerator extends AbstractGenerator
  21  {
  22      /**
  23       * @var string
  24       */
  25      protected $name;
  26  
  27      /**
  28       * @var TypeGenerator|null
  29       */
  30      protected $type;
  31  
  32      /**
  33       * @var ValueGenerator
  34       */
  35      protected $defaultValue;
  36  
  37      /**
  38       * @var int
  39       */
  40      protected $position;
  41  
  42      /**
  43       * @var bool
  44       */
  45      protected $passedByReference = false;
  46  
  47      /**
  48       * @var bool
  49       */
  50      private $variadic = false;
  51  
  52      /**
  53       * @var bool
  54       */
  55      private $omitDefaultValue = false;
  56  
  57      /**
  58       * @param  ParameterReflection $reflectionParameter
  59       * @return ParameterGenerator
  60       */
  61      public static function fromReflection(ParameterReflection $reflectionParameter)
  62      {
  63          $param = new ParameterGenerator();
  64  
  65          $param->setName($reflectionParameter->getName());
  66  
  67          if ($type = self::extractFQCNTypeFromReflectionType($reflectionParameter)) {
  68              $param->setType($type);
  69          }
  70  
  71          $param->setPosition($reflectionParameter->getPosition());
  72  
  73          $variadic = method_exists($reflectionParameter, 'isVariadic') && $reflectionParameter->isVariadic();
  74  
  75          $param->setVariadic($variadic);
  76  
  77          if (! $variadic && ($reflectionParameter->isOptional() || $reflectionParameter->isDefaultValueAvailable())) {
  78              try {
  79                  $param->setDefaultValue($reflectionParameter->getDefaultValue());
  80              } catch (\ReflectionException $e) {
  81                  $param->setDefaultValue(null);
  82              }
  83          }
  84  
  85          $param->setPassedByReference($reflectionParameter->isPassedByReference());
  86  
  87          return $param;
  88      }
  89  
  90      /**
  91       * Generate from array
  92       *
  93       * @configkey name                  string                                          [required] Class Name
  94       * @configkey type                  string
  95       * @configkey defaultvalue          null|bool|string|int|float|array|ValueGenerator
  96       * @configkey passedbyreference     bool
  97       * @configkey position              int
  98       * @configkey sourcedirty           bool
  99       * @configkey indentation           string
 100       * @configkey sourcecontent         string
 101       * @configkey omitdefaultvalue      bool
 102       *
 103       * @throws Exception\InvalidArgumentException
 104       * @param  array $array
 105       * @return ParameterGenerator
 106       */
 107      public static function fromArray(array $array)
 108      {
 109          if (! isset($array['name'])) {
 110              throw new Exception\InvalidArgumentException(
 111                  'Parameter generator requires that a name is provided for this object'
 112              );
 113          }
 114  
 115          $param = new static($array['name']);
 116          foreach ($array as $name => $value) {
 117              // normalize key
 118              switch (strtolower(str_replace(['.', '-', '_'], '', $name))) {
 119                  case 'type':
 120                      $param->setType($value);
 121                      break;
 122                  case 'defaultvalue':
 123                      $param->setDefaultValue($value);
 124                      break;
 125                  case 'passedbyreference':
 126                      $param->setPassedByReference($value);
 127                      break;
 128                  case 'position':
 129                      $param->setPosition($value);
 130                      break;
 131                  case 'sourcedirty':
 132                      $param->setSourceDirty($value);
 133                      break;
 134                  case 'indentation':
 135                      $param->setIndentation($value);
 136                      break;
 137                  case 'sourcecontent':
 138                      $param->setSourceContent($value);
 139                      break;
 140                  case 'omitdefaultvalue':
 141                      $param->omitDefaultValue($value);
 142                      break;
 143              }
 144          }
 145  
 146          return $param;
 147      }
 148  
 149      /**
 150       * @param  string $name
 151       * @param  string $type
 152       * @param  mixed $defaultValue
 153       * @param  int $position
 154       * @param  bool $passByReference
 155       */
 156      public function __construct(
 157          $name = null,
 158          $type = null,
 159          $defaultValue = null,
 160          $position = null,
 161          $passByReference = false
 162      ) {
 163          if (null !== $name) {
 164              $this->setName($name);
 165          }
 166          if (null !== $type) {
 167              $this->setType($type);
 168          }
 169          if (null !== $defaultValue) {
 170              $this->setDefaultValue($defaultValue);
 171          }
 172          if (null !== $position) {
 173              $this->setPosition($position);
 174          }
 175          if (false !== $passByReference) {
 176              $this->setPassedByReference(true);
 177          }
 178      }
 179  
 180      /**
 181       * @param  string $type
 182       * @return ParameterGenerator
 183       */
 184      public function setType($type)
 185      {
 186          $this->type = TypeGenerator::fromTypeString($type);
 187  
 188          return $this;
 189      }
 190  
 191      /**
 192       * @return string
 193       */
 194      public function getType()
 195      {
 196          return $this->type
 197              ? (string) $this->type
 198              : null;
 199      }
 200  
 201      /**
 202       * @param  string $name
 203       * @return ParameterGenerator
 204       */
 205      public function setName($name)
 206      {
 207          $this->name = (string) $name;
 208          return $this;
 209      }
 210  
 211      /**
 212       * @return string
 213       */
 214      public function getName()
 215      {
 216          return $this->name;
 217      }
 218  
 219      /**
 220       * Set the default value of the parameter.
 221       *
 222       * Certain variables are difficult to express
 223       *
 224       * @param  null|bool|string|int|float|array|ValueGenerator $defaultValue
 225       * @return ParameterGenerator
 226       */
 227      public function setDefaultValue($defaultValue)
 228      {
 229          if (! $defaultValue instanceof ValueGenerator) {
 230              $defaultValue = new ValueGenerator($defaultValue);
 231          }
 232          $this->defaultValue = $defaultValue;
 233  
 234          return $this;
 235      }
 236  
 237      /**
 238       * @return ValueGenerator
 239       */
 240      public function getDefaultValue()
 241      {
 242          return $this->defaultValue;
 243      }
 244  
 245      /**
 246       * @param  int $position
 247       * @return ParameterGenerator
 248       */
 249      public function setPosition($position)
 250      {
 251          $this->position = (int) $position;
 252          return $this;
 253      }
 254  
 255      /**
 256       * @return int
 257       */
 258      public function getPosition()
 259      {
 260          return $this->position;
 261      }
 262  
 263      /**
 264       * @return bool
 265       */
 266      public function getPassedByReference()
 267      {
 268          return $this->passedByReference;
 269      }
 270  
 271      /**
 272       * @param  bool $passedByReference
 273       * @return ParameterGenerator
 274       */
 275      public function setPassedByReference($passedByReference)
 276      {
 277          $this->passedByReference = (bool) $passedByReference;
 278          return $this;
 279      }
 280  
 281      /**
 282       * @param bool $variadic
 283       *
 284       * @return ParameterGenerator
 285       */
 286      public function setVariadic($variadic)
 287      {
 288          $this->variadic = (bool) $variadic;
 289  
 290          return $this;
 291      }
 292  
 293      /**
 294       * @return bool
 295       */
 296      public function getVariadic()
 297      {
 298          return $this->variadic;
 299      }
 300  
 301      /**
 302       * @return string
 303       */
 304      public function generate()
 305      {
 306          $output = $this->generateTypeHint();
 307  
 308          if (true === $this->passedByReference) {
 309              $output .= '&';
 310          }
 311  
 312          if ($this->variadic) {
 313              $output .= '... ';
 314          }
 315  
 316          $output .= '$' . $this->name;
 317  
 318          if ($this->omitDefaultValue) {
 319              return $output;
 320          }
 321  
 322          if ($this->defaultValue instanceof ValueGenerator) {
 323              $output .= ' = ';
 324              $this->defaultValue->setOutputMode(ValueGenerator::OUTPUT_SINGLE_LINE);
 325              $output .= $this->defaultValue;
 326          }
 327  
 328          return $output;
 329      }
 330  
 331      /**
 332       * @param ParameterReflection $reflectionParameter
 333       *
 334       * @return null|string
 335       */
 336      private static function extractFQCNTypeFromReflectionType(ParameterReflection $reflectionParameter)
 337      {
 338          if (! method_exists($reflectionParameter, 'getType')) {
 339              return self::prePhp7ExtractFQCNTypeFromReflectionType($reflectionParameter);
 340          }
 341  
 342          $type = method_exists($reflectionParameter, 'getType')
 343              ? $reflectionParameter->getType()
 344              : null;
 345  
 346          if (! $type) {
 347              return null;
 348          }
 349  
 350          if (! method_exists($type, 'getName')) {
 351              return self::expandLiteralParameterType((string) $type, $reflectionParameter);
 352          }
 353  
 354          return ($type->allowsNull() ? '?' : '')
 355              . self::expandLiteralParameterType($type->getName(), $reflectionParameter);
 356      }
 357  
 358      /**
 359       * For ancient PHP versions (yes, you should upgrade to 7.0):
 360       *
 361       * @param ParameterReflection $reflectionParameter
 362       *
 363       * @return string|null
 364       */
 365      private static function prePhp7ExtractFQCNTypeFromReflectionType(ParameterReflection $reflectionParameter)
 366      {
 367          if ($reflectionParameter->isCallable()) {
 368              return 'callable';
 369          }
 370  
 371          if ($reflectionParameter->isArray()) {
 372              return 'array';
 373          }
 374  
 375          if ($class = $reflectionParameter->getClass()) {
 376              return $class->getName();
 377          }
 378  
 379          return null;
 380      }
 381  
 382      /**
 383       * @param string              $literalParameterType
 384       * @param ReflectionParameter $reflectionParameter
 385       *
 386       * @return string
 387       */
 388      private static function expandLiteralParameterType($literalParameterType, ReflectionParameter $reflectionParameter)
 389      {
 390          if ('self' === strtolower($literalParameterType)) {
 391              return $reflectionParameter->getDeclaringClass()->getName();
 392          }
 393  
 394          if ('parent' === strtolower($literalParameterType)) {
 395              return $reflectionParameter->getDeclaringClass()->getParentClass()->getName();
 396          }
 397  
 398          return $literalParameterType;
 399      }
 400  
 401      /**
 402       * @return string
 403       */
 404      private function generateTypeHint()
 405      {
 406          if (null === $this->type) {
 407              return '';
 408          }
 409  
 410          return $this->type->generate() . ' ';
 411      }
 412  
 413      /**
 414       * @param bool $omit
 415       * @return ParameterGenerator
 416       */
 417      public function omitDefaultValue(bool $omit = true)
 418      {
 419          $this->omitDefaultValue = $omit;
 420  
 421          return $this;
 422      }
 423  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1