ray-di /
Ray.Aop
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Ray\Aop; |
||
| 6 | |||
| 7 | use function array_merge; |
||
| 8 | use Doctrine\Common\Annotations\AnnotationReader; |
||
| 9 | use function implode; |
||
| 10 | use PhpParser\BuilderFactory; |
||
| 11 | use PhpParser\Node\Identifier; |
||
| 12 | use PhpParser\Node\Name; |
||
| 13 | use PhpParser\Node\Stmt; |
||
| 14 | use PhpParser\Node\Stmt\Class_; |
||
| 15 | use PhpParser\Node\Stmt\Property; |
||
| 16 | use PhpParser\NodeTraverser; |
||
| 17 | use PhpParser\Parser; |
||
| 18 | use Ray\Aop\Exception\InvalidSourceClassException; |
||
| 19 | use ReflectionClass; |
||
|
0 ignored issues
–
show
|
|||
| 20 | use RuntimeException; |
||
| 21 | |||
| 22 | final class CodeGen implements CodeGenInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var \PhpParser\Parser |
||
| 26 | */ |
||
| 27 | private $parser; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \PhpParser\BuilderFactory |
||
| 31 | */ |
||
| 32 | private $factory; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var CodeGenMethod |
||
| 36 | */ |
||
| 37 | private $codeGenMethod; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var AnnotationReader |
||
| 41 | */ |
||
| 42 | private $reader; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var AopClassName |
||
| 46 | */ |
||
| 47 | private $aopClassName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @throws \Doctrine\Common\Annotations\AnnotationException |
||
| 51 | */ |
||
| 52 | public function __construct( |
||
| 53 | Parser $parser, |
||
| 54 | 33 | BuilderFactory $factory, |
|
| 55 | AopClassName $aopClassName |
||
| 56 | ) { |
||
| 57 | $this->parser = $parser; |
||
| 58 | $this->factory = $factory; |
||
| 59 | 33 | $this->codeGenMethod = new CodeGenMethod($parser, $factory); |
|
| 60 | 33 | $this->reader = new AnnotationReader; |
|
| 61 | 33 | $this->aopClassName = $aopClassName; |
|
| 62 | 33 | } |
|
| 63 | 33 | ||
| 64 | 33 | /** |
|
| 65 | * {@inheritdoc} |
||
| 66 | * |
||
| 67 | * @param ReflectionClass<object> $sourceClass |
||
|
0 ignored issues
–
show
The doc-type
ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 68 | */ |
||
| 69 | 17 | public function generate(ReflectionClass $sourceClass, BindInterface $bind) : Code |
|
| 70 | { |
||
| 71 | 17 | $source = $this->getVisitorCode($sourceClass); |
|
| 72 | 17 | assert($source->class instanceof Class_); |
|
| 73 | 17 | $methods = $this->codeGenMethod->getMethods($sourceClass, $bind, $source); |
|
| 74 | 17 | $propStms = $this->getAopProps($sourceClass); |
|
| 75 | $classStm = $source->class; |
||
| 76 | 16 | $newClassName = ($this->aopClassName)((string) $source->class->name, $bind->toString('')); |
|
| 77 | $classStm->name = new Identifier($newClassName); |
||
| 78 | $classStm->extends = new Name('\\' . $sourceClass->name); |
||
| 79 | $classStm->implements[] = new Name('WeavedInterface'); |
||
| 80 | /** @var array<int, Stmt> $stmts */ |
||
| 81 | $stmts = array_merge($propStms, $methods); |
||
| 82 | $classStm->stmts = $stmts; |
||
| 83 | $ns = $this->getNamespace($source); |
||
| 84 | 17 | $stmt = $this->factory->namespace($ns) |
|
| 85 | ->addStmt($this->factory->use(WeavedInterface::class)) |
||
| 86 | 17 | ->addStmt($this->factory->use(ReflectiveMethodInvocation::class)->as('Invocation')) |
|
| 87 | 17 | ->addStmts($source->use) |
|
| 88 | 17 | ->addStmt($classStm) |
|
| 89 | 17 | ->getNode(); |
|
| 90 | 17 | ||
| 91 | 1 | return new Code(array_merge($source->declare, [$stmt])); |
|
| 92 | } |
||
| 93 | 16 | ||
| 94 | 16 | /** |
|
| 95 | * Return "declare()" and "use" statement code |
||
| 96 | * |
||
| 97 | 16 | * @param ReflectionClass<object> $class |
|
|
0 ignored issues
–
show
The doc-type
ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 98 | 16 | */ |
|
| 99 | 16 | private function getVisitorCode(ReflectionClass $class) : CodeVisitor |
|
| 100 | { |
||
| 101 | $traverser = new NodeTraverser(); |
||
| 102 | 16 | $visitor = new CodeVisitor(); |
|
| 103 | $traverser->addVisitor($visitor); |
||
| 104 | $fileName = $class->getFileName(); |
||
| 105 | if (is_bool($fileName)) { |
||
| 106 | throw new InvalidSourceClassException(get_class($class)); |
||
| 107 | } |
||
| 108 | 17 | $file = file_get_contents($fileName); |
|
| 109 | if ($file === false) { |
||
| 110 | 17 | throw new RuntimeException($fileName); // @codeCoverageIgnore |
|
| 111 | } |
||
| 112 | 17 | $stmts = $this->parser->parse($file); |
|
| 113 | 17 | if (is_array($stmts)) { |
|
| 114 | 17 | $traverser->traverse($stmts); |
|
| 115 | 17 | } |
|
| 116 | 17 | ||
| 117 | return $visitor; |
||
| 118 | 17 | } |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param ReflectionClass<object> $class |
||
|
0 ignored issues
–
show
The doc-type
ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 122 | */ |
||
| 123 | private function getClassAnnotation(ReflectionClass $class) : string |
||
| 124 | 17 | { |
|
| 125 | $classAnnotations = $this->reader->getClassAnnotations($class); |
||
| 126 | 17 | ||
| 127 | 17 | return serialize($classAnnotations); |
|
| 128 | 10 | } |
|
| 129 | |||
| 130 | /** |
||
| 131 | 17 | * @param ReflectionClass<object> $class |
|
|
0 ignored issues
–
show
The doc-type
ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 132 | * |
||
| 133 | * @return Property[] |
||
| 134 | 17 | */ |
|
| 135 | private function getAopProps(ReflectionClass $class) : array |
||
| 136 | 17 | { |
|
| 137 | $pros = []; |
||
| 138 | 17 | $pros[] = $this->factory |
|
| 139 | ->property('bind') |
||
| 140 | ->makePublic() |
||
| 141 | 17 | ->getNode(); |
|
| 142 | |||
| 143 | 17 | $pros[] = |
|
| 144 | 17 | $this->factory->property('bindings') |
|
| 145 | 17 | ->makePublic() |
|
| 146 | 17 | ->setDefault([]) |
|
| 147 | 17 | ->getNode(); |
|
| 148 | 17 | ||
| 149 | 17 | $pros[] = $this->factory |
|
| 150 | 17 | ->property('methodAnnotations') |
|
| 151 | ->setDefault($this->getMethodAnnotations($class)) |
||
| 152 | ->makePublic() |
||
| 153 | 17 | ->getNode(); |
|
| 154 | $pros[] = $this->factory |
||
| 155 | ->property('classAnnotations') |
||
| 156 | ->setDefault($this->getClassAnnotation($class)) |
||
| 157 | ->makePublic() |
||
| 158 | ->getNode(); |
||
| 159 | 17 | $pros[] = $this->factory |
|
| 160 | ->property('isAspect') |
||
| 161 | 17 | ->makePrivate() |
|
| 162 | 17 | ->setDefault(true) |
|
| 163 | 17 | ->getNode(); |
|
| 164 | 17 | ||
| 165 | 17 | return $pros; |
|
| 166 | 17 | } |
|
| 167 | 17 | ||
| 168 | 17 | /** |
|
| 169 | 17 | * @param ReflectionClass<object> $class |
|
|
0 ignored issues
–
show
The doc-type
ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 170 | 17 | */ |
|
| 171 | private function getMethodAnnotations(ReflectionClass $class) : string |
||
| 172 | { |
||
| 173 | 17 | $methodsAnnotation = []; |
|
| 174 | $methods = $class->getMethods(); |
||
| 175 | foreach ($methods as $method) { |
||
| 176 | 17 | $annotations = $this->reader->getMethodAnnotations($method); |
|
| 177 | if ($annotations === []) { |
||
| 178 | 17 | continue; |
|
| 179 | 17 | } |
|
| 180 | 17 | $methodsAnnotation[$method->name] = $annotations; |
|
| 181 | 16 | } |
|
| 182 | 16 | ||
| 183 | 13 | return serialize($methodsAnnotation); |
|
| 184 | } |
||
| 185 | 4 | ||
| 186 | /** |
||
| 187 | * @return null|string |
||
| 188 | 17 | */ |
|
| 189 | private function getNamespace(CodeVisitor $source) |
||
| 190 | { |
||
| 191 | 17 | $parts = $source->namespace->name->parts ?? []; |
|
| 192 | $ns = implode('\\', $parts); |
||
| 193 | |||
| 194 | 17 | return $ns ? $ns : null; |
|
| 195 | 17 | } |
|
| 196 | } |
||
| 197 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: