private function processTraitUse(NodeType\TraitUse $node) { foreach ($node->traits as $trait) { $traitUse = $this->unit->addTrait((string) $trait); $traitUse->setStartLine($node->getAttribute('startLine')); $traitUse->setEndLine($node->getAttribute('endLine')); } foreach ($node->adaptations as $adaptation) { if ($adaptation instanceof NodeType\TraitUseAdaptation\Alias) { $traitUse = $this->getTraitUse((string) $adaptation->trait); $traitUse->addAlias($adaptation->method, $adaptation->newName, $adaptation->newModifier ? $this->modifier[$adaptation->newModifier] : NULL); } elseif ($adaptation instanceof NodeType\TraitUseAdaptation\Precedence) { $traitUse = $this->getTraitUse((string) $adaptation->insteadof[0]); $traitUse->addExclude($adaptation->method); } else { throw new ParseErrorException(sprintf('Unexpected adaption type %s', get_class($adaptation)), ParseErrorException::UnexpectedExpr); } } }
/** * Different Traits may implement their own copies of the same method name. That's fine at this point. Later we will * use adaptations to reduce duplicates down to a single method for each name. * @param TraitUse $use * @param array $methods * @param array $properties * @throws UnknownTraitException */ private function indexTrait(TraitUse $use, array &$methods, array &$properties) { foreach ($use->traits as $useTrait) { $traitName = strval($useTrait); $trait = $this->index->getTrait($traitName); if (!$trait) { throw new \Guardrail\Exceptions\UnknownTraitException($traitName, $this->file, $use->getLine()); } foreach ($trait->stmts as $stmt) { if ($stmt instanceof Node\Stmt\Property) { foreach ($stmt->props as $prop) { // Make a deep copy of the node $properties[$prop->name] = unserialize(serialize($prop)); } } else { if ($stmt instanceof Node\Stmt\ClassMethod) { // Make a deep copy of the node $methods[$stmt->name][$traitName] = unserialize(serialize($stmt)); } } } } }