Completed
Push — php7.4 ( 463624...ddc4cb )
by Akihito
03:54 queued 02:53
created

CodeGen::getNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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
        $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
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function generate(\ReflectionClass $sourceClass, BindInterface $bind) : Code
65
    {
66
        $source = $this->getVisitorCode($sourceClass);
67
        $methods = $this->codeGenMethod->getMethods($sourceClass, $bind, $source);
68
        $propStms = $this->getAopProps($sourceClass);
69
        $classStm = $source->class;
70
        $newClassName = sprintf('%s_%s', (string) $source->class->name, $bind->toString(''));
71
        $classStm->name = new Identifier($newClassName);
72
        $classStm->extends = new Name('\\' . $sourceClass->name);
73
        $classStm->implements[] = new Name('WeavedInterface');
74
        $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
        $ns = $this->getNamespace($source);
76
        $stmt = $this->factory->namespace($ns)
77
            ->addStmt($this->factory->use('Ray\Aop\WeavedInterface'))
78
            ->addStmt($this->factory->use('Ray\Aop\ReflectiveMethodInvocation')->as('Invocation'))
79
            ->addStmts($source->use)
80
            ->addStmt($classStm)
81
            ->getNode();
82
        $code = new Code;
83
        $code->code = $this->printer->prettyPrintFile(array_merge($source->declare, [$stmt]));
84
85
        return $code;
86
    }
87
88
    /**
89
     * Return "declare()" and "use" statement code
90
     */
91
    private function getVisitorCode(\ReflectionClass $class) : CodeVisitor
92
    {
93
        $traverser = new NodeTraverser();
94
        $visitor = new CodeVisitor();
95
        $traverser->addVisitor($visitor);
96
        $fileName = $class->getFileName();
97
        if (is_bool($fileName)) {
98
            throw new InvalidSourceClassException(get_class($class));
99
        }
100
        $file = file_get_contents($fileName);
101
        if ($file === false) {
102
            throw new \RuntimeException($fileName); // @codeCoverageIgnore
103
        }
104
        $stmts = $this->parser->parse($file);
105
        if (is_array($stmts)) {
106
            $traverser->traverse($stmts);
107
        }
108
109
        return $visitor;
110
    }
111
112
    private function getClassAnnotation(\ReflectionClass $class) : string
113
    {
114
        $classAnnotations = $this->reader->getClassAnnotations($class);
115
116
        return serialize($classAnnotations);
117
    }
118
119
    /**
120
     * @return Property[]
121
     */
122
    private function getAopProps(\ReflectionClass $class) : array
123
    {
124
        $pros = [];
125
        $pros[] = $this->factory
126
            ->property('bind')
127
            ->makePublic()
128
            ->getNode();
129
130
        $pros[] =
131
            $this->factory->property('bindings')
132
                ->makePublic()
133
                ->setDefault([])
134
                ->getNode();
135
136
        $pros[] = $this->factory
137
            ->property('methodAnnotations')
138
            ->setDefault($this->getMethodAnnotations($class))
139
            ->makePublic()
140
            ->getNode();
141
        $pros[] = $this->factory
142
            ->property('classAnnotations')
143
            ->setDefault($this->getClassAnnotation($class))
144
            ->makePublic()
145
            ->getNode();
146
        $pros[] = $this->factory
147
            ->property('isAspect')
148
            ->makePrivate()
149
            ->setDefault(true)
150
            ->getNode();
151
152
        return $pros;
153
    }
154
155
    private function getMethodAnnotations(\ReflectionClass $class) : string
156
    {
157
        $methodsAnnotation = [];
158
        $methods = $class->getMethods();
159
        foreach ($methods as $method) {
160
            $annotations = $this->reader->getMethodAnnotations($method);
161
            if ($annotations === []) {
162
                continue;
163
            }
164
            $methodsAnnotation[$method->name] = $annotations;
165
        }
166
167
        return serialize($methodsAnnotation);
168
    }
169
170
    /**
171
     * @return null|string
172
     */
173
    private function getNamespace(CodeVisitor $source)
174
    {
175
        $parts = $source->namespace->name->parts ?? [];
176
        $ns = implode('\\', $parts);
177
178
        return $ns ? $ns : null;
179
    }
180
}
181