Completed
Push — 2.x ( 266f66...8e3b41 )
by Akihito
01:57
created

CodeGenMethod::getMethod()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.7333
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the Ray.Aop package.
6
 *
7
 * @license http://opensource.org/licenses/MIT MIT
8
 */
9
namespace Ray\Aop;
10
11
use Doctrine\Common\Annotations\AnnotationReader;
12
use PhpParser\Builder\Method;
13
use PhpParser\Builder\Param;
14
use PhpParser\BuilderFactory;
15
use PhpParser\Comment\Doc;
16
use PhpParser\Node\NullableType;
17
use PhpParser\Node\Stmt;
18
use PhpParser\Node\Stmt\Class_;
19
use PhpParser\Node\Stmt\ClassMethod;
20
use PhpParser\NodeTraverser;
21
use PhpParser\Parser;
22
use PhpParser\PrettyPrinter\Standard;
23
use Ray\Aop\Annotation\AbstractAssisted;
24
25
final class CodeGenMethod
26
{
27
    /**
28
     * @var \PhpParser\Parser
29
     */
30
    private $parser;
31
32
    /**
33
     * @var \PhpParser\BuilderFactory
34
     */
35
    private $factory;
36
37
    /**
38
     * @var \PhpParser\PrettyPrinter\Standard
39
     */
40
    private $printer;
41
42
    private $reader;
43
44
    /**
45
     * @var AbstractAssisted
46
     */
47
    private $assisted;
48
49
    /**
50
     * @param \PhpParser\Parser                 $parser
51
     * @param \PhpParser\BuilderFactory         $factory
52
     * @param \PhpParser\PrettyPrinter\Standard $printer
53
     */
54 33
    public function __construct(
55
        Parser $parser,
56
        BuilderFactory $factory,
57
        Standard $printer
58
    ) {
59 33
        $this->parser = $parser;
60 33
        $this->factory = $factory;
61 33
        $this->printer = $printer;
62 33
        $this->reader = new AnnotationReader;
63 33
    }
64
65
    /**
66
     * @param \ReflectionClass $class
67
     * @param BindInterface    $bind
68
     *
69
     * @return array
70
     */
71 17
    public function getMethods(\ReflectionClass $class, BindInterface $bind) : array
72
    {
73 17
        $bindingMethods = array_keys($bind->getBindings());
74 17
        $stmts = [];
75 17
        $methods = $class->getMethods();
76 17
        foreach ($methods as $method) {
77 16
            $this->assisted = $this->reader->getMethodAnnotation($method, AbstractAssisted::class);
78 16
            $isBindingMethod = in_array($method->name, $bindingMethods, true);
79
            /* @var $method \ReflectionMethod */
80 16
            if ($isBindingMethod && $method->isPublic()) {
81 16
                $stmts[] = $this->getMethod($method);
82
            }
83
        }
84
85 17
        return $stmts;
86
    }
87
88
    /**
89
     * Return method statement
90
     *
91
     * @param \ReflectionMethod $method
92
     *
93
     * @return \PhpParser\Node\Stmt\ClassMethod
94
     */
95 15
    private function getMethod(\ReflectionMethod $method)
96
    {
97 15
        $methodStmt = $this->factory->method($method->name);
98 15
        $params = $method->getParameters();
99 15
        foreach ($params as $param) {
100 13
            $methodStmt = $this->getMethodStatement($param, $methodStmt);
101
        }
102 15
        $returnType = $method->getReturnType();
103 15
        if ($returnType instanceof \ReflectionType) {
0 ignored issues
show
Bug introduced by
The class ReflectionType does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
104 5
            $this->setReturnType($returnType, $methodStmt);
105
        }
106 15
        $methodInsideStatements = $this->getMethodInsideStatement($method);
107 15
        $methodStmt->addStmts($methodInsideStatements);
108
109 15
        return $this->addMethodDocComment($methodStmt, $method);
110
    }
111
112
    /**
113
     * Return parameter reflection
114
     */
115 13
    private function getMethodStatement(\ReflectionParameter $param, Method $methodStmt) : Method
116
    {
117
        /* @var $paramStmt Param */
118 13
        $paramStmt = $this->factory->param($param->name);
119
        /* @var $param \ReflectionParameter */
120 13
        $this->setParameterType($param, $paramStmt);
121 13
        $this->setDefault($param, $paramStmt);
122 13
        $methodStmt->addParam($paramStmt);
123
124 13
        return $methodStmt;
125
    }
126
127 15
    private function addMethodDocComment(Method $methodStmt, \ReflectionMethod $method) : ClassMethod
128
    {
129 15
        $node = $methodStmt->getNode();
130 15
        $docComment = $method->getDocComment();
131 15
        if ($docComment) {
132 6
            $node->setAttribute('comments', [new Doc($docComment)]);
133
        }
134
135 15
        return $node;
136
    }
137
138
    /**
139
     * @return \PhpParser\Node[]
140
     */
141 15
    private function getMethodInsideStatement(\ReflectionMethod $method) : array
142
    {
143 15
        $traverser = new NodeTraverser;
144 15
        $traverser->addVisitor(new AopTemplateConverter($method));
145 15
        $stmts = $this->getTemplateMethodNodeStmts();
146
147
        // traverse
148 15
        $stmts = $traverser->traverse($stmts);
149
150 15
        return $stmts;
151
    }
152
153 13
    private function setDefault(\ReflectionParameter $param, Param $paramStmt)
154
    {
155 13
        if ($param->isDefaultValueAvailable()) {
156 3
            $paramStmt->setDefault($param->getDefaultValue());
157
158 3
            return;
159
        }
160 13
        if ($this->assisted instanceof AbstractAssisted && in_array($param->name, $this->assisted->values, true)) {
161 1
            $paramStmt->setDefault(null);
162
        }
163 13
    }
164
165 13
    private function setParameterType(\ReflectionParameter $param, Param $paramStmt)
166
    {
167 13
        $type = $param->getType();
168 13
        if ($type == null) {
169 8
            return;
170
        }
171 7
        if ($param->isVariadic()) {
172 1
            $paramStmt->makeVariadic();
173
        }
174 7
        $paramString = (string) $param;
175 7
        $isNullableType = is_int(strpos($paramString, '<required>')) && is_int(strpos($paramString, 'or NULL'));
176 7
        $destType = $isNullableType ? new NullableType((string) $type) : (string) $type;
177 7
        $paramStmt->setTypeHint($destType);
178 7
    }
179
180 5
    private function setReturnType(\ReflectionType $returnType, Method $methodStmt)
181
    {
182 5
        $type = $returnType->allowsNull() ? new NullableType((string) $returnType) : (string) $returnType;
183 5
        $methodStmt->setReturnType($type);
184 5
    }
185
186
    /**
187
     * @return Stmt[]
188
     */
189 15
    private function getTemplateMethodNodeStmts() : array
190
    {
191 15
        $code = file_get_contents(dirname(__DIR__) . '/template/AopTemplate.php');
192
        /** @var string $code */
193 15
        $node = $this->parser->parse($code)[0];
194 15
        if (! $node instanceof Class_) {
195
            throw new \LogicException; // @codeCoverageIgnore
196
        }
197 15
        $methodNode = $node->getMethods()[0];
198 15
        if ($methodNode->stmts === null) {
199
            throw new \LogicException; // @codeCoverageIgnore
200
        }
201
202 15
        return $methodNode->stmts;
203
    }
204
}
205