[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Reflection/ -> FunctionReflection.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-2015 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\Reflection;
  11  
  12  use ReflectionFunction;
  13  
  14  class FunctionReflection extends ReflectionFunction implements ReflectionInterface
  15  {
  16      /**
  17       * Constant use in @MethodReflection to display prototype as an array
  18       */
  19      const PROTOTYPE_AS_ARRAY = 'prototype_as_array';
  20  
  21      /**
  22       * Constant use in @MethodReflection to display prototype as a string
  23       */
  24      const PROTOTYPE_AS_STRING = 'prototype_as_string';
  25  
  26      /**
  27       * Get function DocBlock
  28       *
  29       * @throws Exception\InvalidArgumentException
  30       * @return DocBlockReflection
  31       */
  32      public function getDocBlock()
  33      {
  34          if ('' == ($comment = $this->getDocComment())) {
  35              throw new Exception\InvalidArgumentException(sprintf(
  36                  '%s does not have a DocBlock',
  37                  $this->getName()
  38              ));
  39          }
  40  
  41          $instance = new DocBlockReflection($comment);
  42  
  43          return $instance;
  44      }
  45  
  46      /**
  47       * Get start line (position) of function
  48       *
  49       * @param  bool $includeDocComment
  50       * @return int
  51       */
  52      public function getStartLine($includeDocComment = false)
  53      {
  54          if ($includeDocComment) {
  55              if ($this->getDocComment() != '') {
  56                  return $this->getDocBlock()->getStartLine();
  57              }
  58          }
  59  
  60          return parent::getStartLine();
  61      }
  62  
  63      /**
  64       * Get contents of function
  65       *
  66       * @param  bool   $includeDocBlock
  67       * @return string
  68       */
  69      public function getContents($includeDocBlock = true)
  70      {
  71          $fileName = $this->getFileName();
  72          if (false === $fileName) {
  73              return '';
  74          }
  75  
  76          $startLine = $this->getStartLine();
  77          $endLine = $this->getEndLine();
  78  
  79          // eval'd protect
  80          if (preg_match('#\((\d+)\) : eval\(\)\'d code$#', $fileName, $matches)) {
  81              $fileName = preg_replace('#\(\d+\) : eval\(\)\'d code$#', '', $fileName);
  82              $startLine = $endLine = $matches[1];
  83          }
  84  
  85          $lines = array_slice(
  86              file($fileName, FILE_IGNORE_NEW_LINES),
  87              $startLine - 1,
  88              ($endLine - ($startLine - 1)),
  89              true
  90          );
  91  
  92          $functionLine = implode("\n", $lines);
  93  
  94          $content = '';
  95          if ($this->isClosure()) {
  96              preg_match('#function\s*\([^\)]*\)\s*(use\s*\([^\)]+\))?\s*\{(.*\;)?\s*\}#s', $functionLine, $matches);
  97              if (isset($matches[0])) {
  98                  $content = $matches[0];
  99              }
 100          } else {
 101              $name = substr($this->getName(), strrpos($this->getName(), '\\')+1);
 102              preg_match('#function\s+' . preg_quote($name) . '\s*\([^\)]*\)\s*{([^{}]+({[^}]+})*[^}]+)?}#', $functionLine, $matches);
 103              if (isset($matches[0])) {
 104                  $content = $matches[0];
 105              }
 106          }
 107  
 108          $docComment = $this->getDocComment();
 109  
 110          return $includeDocBlock && $docComment ? $docComment . "\n" . $content : $content;
 111      }
 112  
 113      /**
 114       * Get method prototype
 115       *
 116       * @return array
 117       */
 118      public function getPrototype($format = FunctionReflection::PROTOTYPE_AS_ARRAY)
 119      {
 120          $returnType = 'mixed';
 121          $docBlock = $this->getDocBlock();
 122          if ($docBlock) {
 123              $return = $docBlock->getTag('return');
 124              $returnTypes = $return->getTypes();
 125              $returnType = count($returnTypes) > 1 ? implode('|', $returnTypes) : $returnTypes[0];
 126          }
 127  
 128          $prototype = array(
 129              'namespace' => $this->getNamespaceName(),
 130              'name'      => substr($this->getName(), strlen($this->getNamespaceName()) + 1),
 131              'return'    => $returnType,
 132              'arguments' => array(),
 133          );
 134  
 135          $parameters = $this->getParameters();
 136          foreach ($parameters as $parameter) {
 137              $prototype['arguments'][$parameter->getName()] = array(
 138                  'type'     => $parameter->getType(),
 139                  'required' => !$parameter->isOptional(),
 140                  'by_ref'   => $parameter->isPassedByReference(),
 141                  'default'  => $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null,
 142              );
 143          }
 144  
 145          if ($format == FunctionReflection::PROTOTYPE_AS_STRING) {
 146              $line = $prototype['return'] . ' ' . $prototype['name'] . '(';
 147              $args = array();
 148              foreach ($prototype['arguments'] as $name => $argument) {
 149                  $argsLine = ($argument['type'] ? $argument['type'] . ' ' : '') . ($argument['by_ref'] ? '&' : '') . '$' . $name;
 150                  if (!$argument['required']) {
 151                      $argsLine .= ' = ' . var_export($argument['default'], true);
 152                  }
 153                  $args[] = $argsLine;
 154              }
 155              $line .= implode(', ', $args);
 156              $line .= ')';
 157  
 158              return $line;
 159          }
 160  
 161          return $prototype;
 162      }
 163  
 164      /**
 165       * Get function parameters
 166       *
 167       * @return ParameterReflection[]
 168       */
 169      public function getParameters()
 170      {
 171          $phpReflections  = parent::getParameters();
 172          $zendReflections = array();
 173          while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
 174              $instance          = new ParameterReflection($this->getName(), $phpReflection->getName());
 175              $zendReflections[] = $instance;
 176              unset($phpReflection);
 177          }
 178          unset($phpReflections);
 179  
 180          return $zendReflections;
 181      }
 182  
 183      /**
 184       * Get return type tag
 185       *
 186       * @throws Exception\InvalidArgumentException
 187       * @return DocBlockReflection
 188       */
 189      public function getReturn()
 190      {
 191          $docBlock = $this->getDocBlock();
 192          if (!$docBlock->hasTag('return')) {
 193              throw new Exception\InvalidArgumentException(
 194                  'Function does not specify an @return annotation tag; cannot determine return type'
 195              );
 196          }
 197  
 198          $tag    = $docBlock->getTag('return');
 199  
 200          return new DocBlockReflection('@return ' . $tag->getDescription());
 201      }
 202  
 203      /**
 204       * Get method body
 205       *
 206       * @return string|false
 207       */
 208      public function getBody()
 209      {
 210          $fileName = $this->getFileName();
 211          if (false === $fileName) {
 212              throw new Exception\InvalidArgumentException(
 213                  'Cannot determine internals functions body'
 214              );
 215          }
 216  
 217          $startLine = $this->getStartLine();
 218          $endLine = $this->getEndLine();
 219  
 220          // eval'd protect
 221          if (preg_match('#\((\d+)\) : eval\(\)\'d code$#', $fileName, $matches)) {
 222              $fileName = preg_replace('#\(\d+\) : eval\(\)\'d code$#', '', $fileName);
 223              $startLine = $endLine = $matches[1];
 224          }
 225  
 226          $lines = array_slice(
 227              file($fileName, FILE_IGNORE_NEW_LINES),
 228              $startLine - 1,
 229              ($endLine - ($startLine - 1)),
 230              true
 231          );
 232  
 233          $functionLine = implode("\n", $lines);
 234  
 235          $body = false;
 236          if ($this->isClosure()) {
 237              preg_match('#function\s*\([^\)]*\)\s*(use\s*\([^\)]+\))?\s*\{(.*\;)\s*\}#s', $functionLine, $matches);
 238              if (isset($matches[2])) {
 239                  $body = $matches[2];
 240              }
 241          } else {
 242              $name = substr($this->getName(), strrpos($this->getName(), '\\')+1);
 243              preg_match('#function\s+' . $name . '\s*\([^\)]*\)\s*{([^{}]+({[^}]+})*[^}]+)}#', $functionLine, $matches);
 244              if (isset($matches[1])) {
 245                  $body = $matches[1];
 246              }
 247          }
 248  
 249          return $body;
 250      }
 251  
 252      /**
 253       * @return string
 254       */
 255      public function toString()
 256      {
 257          return $this->__toString();
 258      }
 259  
 260      /**
 261       * Required due to bug in php
 262       *
 263       * @return string
 264       */
 265      public function __toString()
 266      {
 267          return parent::__toString();
 268      }
 269  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1