示例#1
0
 /**
  * Return a single method by given name or index of info
  *
  * @param  string|int $methodNameOrInfoIndex
  * @throws Exception\InvalidArgumentException
  * @return MethodScanner
  */
 public function getMethod($methodNameOrInfoIndex)
 {
     $this->scan();
     if (is_int($methodNameOrInfoIndex)) {
         $info = $this->infos[$methodNameOrInfoIndex];
         if ($info['type'] != 'method') {
             throw new Exception\InvalidArgumentException('Index of info offset is not about a method');
         }
     } elseif (is_string($methodNameOrInfoIndex)) {
         $methodFound = false;
         foreach ($this->infos as $info) {
             if ($info['type'] === 'method' && $info['name'] === $methodNameOrInfoIndex) {
                 $methodFound = true;
                 break;
             }
         }
         if (!$methodFound) {
             return false;
         }
     }
     if (!isset($info)) {
         // @todo find a way to test this
         die('Massive Failure, test this');
     }
     $m = new MethodScanner(array_slice($this->tokens, $info['tokenStart'], $info['tokenEnd'] - $info['tokenStart'] + 1), $this->nameInformation);
     $m->setClass($this->name);
     $m->setScannerClass($this);
     return $m;
 }
 /**
  * Return a list of methods
  *
  * @return MethodScanner[]
  */
 public function getMethods()
 {
     $this->scan();
     if (!empty($this->methods)) {
         return $this->methods;
     }
     foreach ($this->infos as $info) {
         if ($info['type'] !== 'method' && $info['type'] !== 'use') {
             continue;
         }
         // Merge in trait methods
         if ($info['type'] === "use") {
             $traitMethods = [];
             $traits = $this->getTraits();
             $insteadof = $this->getBlockedTraitMethods();
             $aliases = $this->getTraitAliases();
             foreach ($traits as $trait) {
                 $tempMethods = $trait->getMethods();
                 foreach ($tempMethods as $tempMethod) {
                     $methodFullName = $trait->getName() . '::' . $tempMethod->getName();
                     $methodAlias = array_search($methodFullName, $aliases);
                     if (false !== $methodAlias) {
                         // trait::method is aliased
                         // clone the tempMethod as we need to change
                         // the name and possibly the visibility of the
                         // scanned method.
                         //
                         // @todo setName and setVisibility were added to
                         // MethodScanner to accomplish this, may not be the
                         // best option, could use ReflectionClass instead?
                         $newMethod = clone $tempMethod;
                         $newMethod->setName($methodAlias);
                         // if visibility exists, change it on the MethodScanner
                         $visibility = $this->getVisibilityForAlias($methodAlias);
                         if (null !== $visibility) {
                             $newMethod->setVisibility($visibility);
                         }
                         $traitMethods[$methodAlias] = $newMethod;
                     } elseif (in_array($methodFullName, $insteadof)) {
                         // ignore overridden methods
                         continue;
                     } else {
                         if (array_key_exists($tempMethod->getName(), $traitMethods)) {
                             throw new Exception\RuntimeException(sprintf('Trait method %s has not been applied because there are' . ' collisions with other trait methods see: (insteadof OR as)', $tempMethod->getName()));
                         }
                         $traitMethods[$tempMethod->getName()] = $tempMethod;
                     }
                 }
             }
             $this->methods = array_merge($this->methods, array_values($traitMethods));
             continue;
         }
         $m = new MethodScanner(array_slice($this->tokens, $info['tokenStart'], $info['tokenEnd'] - $info['tokenStart'] + 1), $this->nameInformation);
         $m->setClass($this->name);
         $m->setScannerClass($this);
         $this->methods[] = $m;
     }
     return $this->methods;
 }