1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2012, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\Proxy; |
12
|
|
|
|
13
|
|
|
use ReflectionParameter as Parameter; |
14
|
|
|
use TokenReflection\ReflectionMethod as ParsedMethod; |
15
|
|
|
use TokenReflection\ReflectionParameter as ParsedParameter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract class for building different proxies |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractProxy |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Indent for source code |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $indent = 4; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* List of advices that are used for generation of child |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $advices = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* PHP expression string for accessing LSB information |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected static $staticLsbExpression = 'static::class'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Should proxy use variadics support or not |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
protected $useVariadics = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructs an abstract proxy class |
53
|
|
|
* |
54
|
|
|
* @param array $advices List of advices |
55
|
|
|
* @param bool $useVariadics Should proxy use variadics syntax or not |
56
|
|
|
*/ |
57
|
|
|
public function __construct(array $advices = [], $useVariadics = false) |
58
|
|
|
{ |
59
|
|
|
$this->advices = $this->flattenAdvices($advices); |
60
|
|
|
$this->useVariadics = $useVariadics; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns text representation of class |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
abstract public function __toString(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Indent block of code |
72
|
|
|
* |
73
|
|
|
* @param string $text Non-indented text |
74
|
|
|
* |
75
|
|
|
* @return string Indented text |
76
|
|
|
*/ |
77
|
|
|
protected function indent($text) |
78
|
|
|
{ |
79
|
|
|
$pad = str_pad('', $this->indent, ' '); |
80
|
|
|
$lines = array_map(function($line) use ($pad) { |
81
|
|
|
return $pad . $line; |
82
|
|
|
}, explode("\n", $text)); |
83
|
|
|
|
84
|
|
|
return join("\n", $lines); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns list of string representation of parameters |
89
|
|
|
* |
90
|
|
|
* @param array|Parameter[]|ParsedParameter[] $parameters List of parameters |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
protected function getParameters(array $parameters) |
95
|
|
|
{ |
96
|
|
|
$parameterDefinitions = []; |
97
|
|
|
foreach ($parameters as $parameter) { |
98
|
|
|
// Deprecated since PHP5.6 in the favor of variadics, needed for BC only |
99
|
|
|
if ($parameter->name == '...') { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
$parameterDefinitions[] = $this->getParameterCode($parameter); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $parameterDefinitions; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return string representation of parameter |
110
|
|
|
* |
111
|
|
|
* @param Parameter|ParsedParameter $parameter Reflection parameter |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function getParameterCode($parameter) |
116
|
|
|
{ |
117
|
|
|
$type = ''; |
118
|
|
|
if ($parameter->isArray()) { |
119
|
|
|
$type = 'array'; |
120
|
|
|
} elseif ($parameter->isCallable()) { |
121
|
|
|
$type = 'callable'; |
122
|
|
|
} elseif ($parameter->getClass()) { |
123
|
|
|
$type = '\\' . $parameter->getClass()->name; |
124
|
|
|
} |
125
|
|
|
$defaultValue = null; |
126
|
|
|
$isDefaultValueAvailable = $parameter->isDefaultValueAvailable(); |
127
|
|
|
if ($isDefaultValueAvailable) { |
128
|
|
|
if ($parameter instanceof ParsedParameter) { |
|
|
|
|
129
|
|
|
$defaultValue = $parameter->getDefaultValueDefinition(); |
130
|
|
|
} else { |
131
|
|
|
$defaultValue = var_export($parameter->getDefaultValue(), true); |
132
|
|
|
} |
133
|
|
|
} elseif ($parameter->isOptional()) { |
134
|
|
|
$defaultValue = 'null'; |
135
|
|
|
} |
136
|
|
|
$code = ( |
137
|
|
|
($type ? "$type " : '') . // Typehint |
138
|
|
|
($parameter->isPassedByReference() ? '&' : '') . // By reference sign |
139
|
|
|
($this->useVariadics && $parameter->isVariadic() ? '...' : '') . // Variadic symbol |
140
|
|
|
'$' . // Variable symbol |
141
|
|
|
($parameter->name) . // Name of the argument |
142
|
|
|
($defaultValue !== null ? (" = " . $defaultValue) : '') // Default value if present |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
return $code; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Replace concrete advices with list of ids |
150
|
|
|
* |
151
|
|
|
* @param $advices |
152
|
|
|
* |
153
|
|
|
* @return array flatten list of advices |
154
|
|
|
*/ |
155
|
|
|
private function flattenAdvices($advices) |
156
|
|
|
{ |
157
|
|
|
$flattenAdvices = []; |
158
|
|
|
foreach ($advices as $type => $typedAdvices) { |
159
|
|
|
foreach ($typedAdvices as $name => $concreteAdvices) { |
160
|
|
|
if (is_array($concreteAdvices)) { |
161
|
|
|
$flattenAdvices[$type][$name] = array_keys($concreteAdvices); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $flattenAdvices; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Prepares a line with args from the method definition |
171
|
|
|
* |
172
|
|
|
* @param ParsedMethod $method |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
protected function prepareArgsLine(ParsedMethod $method) |
177
|
|
|
{ |
178
|
|
|
$args = join(', ', array_map(function(ParsedParameter $param) { |
179
|
|
|
$byReference = $param->isPassedByReference() ? '&' : ''; |
180
|
|
|
|
181
|
|
|
return $byReference . '$' . $param->name; |
182
|
|
|
}, $method->getParameters())); |
183
|
|
|
|
184
|
|
|
return $args; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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 thecomposer.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
orrequire-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 you have not tested against this specific condition, such errors might go unnoticed.