Completed
Push — refactor ( d2218a )
by Akihito
01:47
created

CodeGen::getAopProps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
ccs 18
cts 18
cp 1
cc 1
nc 1
nop 1
crap 1
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\Property;
14
use PhpParser\NodeTraverser;
15
use PhpParser\Parser;
16
use PhpParser\PrettyPrinter\Standard;
17
use Ray\Aop\Exception\InvalidSourceClassException;
18
19
final class CodeGen implements CodeGenInterface
20
{
21
    /**
22
     * @var \PhpParser\Parser
23
     */
24
    private $parser;
25
26
    /**
27
     * @var \PhpParser\BuilderFactory
28
     */
29
    private $factory;
30
31
    /**
32
     * @var \PhpParser\PrettyPrinter\Standard
33
     */
34
    private $printer;
35
36
    /**
37
     * @var CodeGenMethod
38
     */
39
    private $codeGenMethod;
40
41
    /**
42
     * @var AnnotationReader
43
     */
44
    private $reader;
45
46
    /**
47
     * @throws \Doctrine\Common\Annotations\AnnotationException
48
     */
49
    public function __construct(
50
        Parser $parser,
51
        BuilderFactory $factory,
52
        Standard $printer
53
    ) {
54 33
        $this->parser = $parser;
55
        $this->factory = $factory;
56
        $this->printer = $printer;
57
        $this->codeGenMethod = new CodeGenMethod($parser, $factory, $printer);
58
        $this->reader = new AnnotationReader;
59 33
    }
60 33
61 33
    /**
62 33
     * {@inheritdoc}
63 33
     */
64 33
    public function generate(\ReflectionClass $sourceClass, BindInterface $bind) : Code
65
    {
66
        $souce = $this->getVisitorCode($sourceClass);
67
        $methods = $this->codeGenMethod->getMethods($sourceClass, $bind, $souce);
68
        $propStms = $this->getAopProps($sourceClass);
69 17
        $classStm = $souce->class;
70
        $newClassName = sprintf('%s_%s', (string) $souce->class->name, $bind->toString(''));
71 17
        $classStm->name = new Identifier($newClassName);
72 17
        $classStm->extends = new Name('\\' . $sourceClass->name);
73 17
        $classStm->implements[] = new Name('WeavedInterface');
74 17
        $classStm->stmts = array_merge($propStms, $methods);
0 ignored issues
show
Documentation Bug introduced by
It seems like \array_merge($propStms, $methods) of type array is incompatible with the declared type array<integer,object<PhpParser\Node\Stmt>> of property $stmts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
        $parts = $souce->namespace->name->parts ?? [];
76 16
        $ns = implode('\\', $parts);
77
        $stmt = $this->factory->namespace($ns)
78
            ->addStmt($this->factory->use('Ray\Aop\WeavedInterface'))
79
            ->addStmt($this->factory->use('Ray\Aop\ReflectiveMethodInvocation')->as('Invocation'))
80
            ->addStmts($souce->use)
81
            ->addStmt($classStm)
82
            ->getNode();
83
84 17
        $code = new Code;
85
        $code->code = $this->printer->prettyPrintFile(array_merge($souce->declare, [$stmt]));
86 17
87 17
        return $code;
88 17
    }
89 17
90 17
    /**
91 1
     * Return "declare()" and "use" statement code
92
     */
93 16
    private function getVisitorCode(\ReflectionClass $class) : CodeVisitor
94 16
    {
95
        $traverser = new NodeTraverser();
96
        $visitor = new CodeVisitor();
97 16
        $traverser->addVisitor($visitor);
98 16
        $fileName = $class->getFileName();
99 16
        if (is_bool($fileName)) {
100
            throw new InvalidSourceClassException(get_class($class));
101
        }
102 16
        $file = file_get_contents($fileName);
103
        if ($file === false) {
104
            throw new \RuntimeException($fileName); // @codeCoverageIgnore
105
        }
106
        $stmts = $this->parser->parse($file);
107
        if (is_array($stmts)) {
108 17
            $traverser->traverse($stmts);
109
        }
110 17
111
        return $visitor;
112 17
    }
113 17
114 17
    private function getClassAnnotation(\ReflectionClass $class) : string
115 17
    {
116 17
        $classAnnotations = $this->reader->getClassAnnotations($class);
117
118 17
        return serialize($classAnnotations);
119
    }
120
121
    /**
122
     * @return Property[]
123
     */
124 17
    private function getAopProps(\ReflectionClass $class) : array
125
    {
126 17
        $pros = [];
127 17
        $pros[] = $this->factory
128 10
            ->property('bind')
129
            ->makePublic()
130
            ->getNode();
131 17
132
        $pros[] =
133
            $this->factory->property('bindings')
134 17
                ->makePublic()
135
                ->setDefault([])
136 17
                ->getNode();
137
138 17
        $pros[] = $this->factory
139
            ->property('methodAnnotations')
140
            ->setDefault($this->getMethodAnnotations($class))
141 17
            ->makePublic()
142
            ->getNode();
143 17
        $pros[] = $this->factory
144 17
            ->property('classAnnotations')
145 17
            ->setDefault($this->getClassAnnotation($class))
146 17
            ->makePublic()
147 17
            ->getNode();
148 17
        $pros[] = $this->factory
149 17
            ->property('isAspect')
150 17
            ->makePrivate()
151
            ->setDefault(true)
152
            ->getNode();
153 17
154
        return $pros;
155
    }
156
157
    private function getMethodAnnotations(\ReflectionClass $class) : string
158
    {
159 17
        $methodsAnnotation = [];
160
        $methods = $class->getMethods();
161 17
        foreach ($methods as $method) {
162 17
            $annotations = $this->reader->getMethodAnnotations($method);
163 17
            if ($annotations === []) {
164 17
                continue;
165 17
            }
166 17
            $methodsAnnotation[$method->name] = $annotations;
167 17
        }
168 17
169 17
        return serialize($methodsAnnotation);
170 17
    }
171
}
172