1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2018, 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\Instrument\Transformer; |
12
|
|
|
|
13
|
|
|
use PhpParser\Node\Name\FullyQualified; |
14
|
|
|
use PhpParser\NodeTraverser; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Transformer that replaces `self` constants in the source code, e.g. new self() |
18
|
|
|
*/ |
19
|
|
|
class SelfValueTransformer extends BaseSourceTransformer |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* This method may transform the supplied source and return a new replacement for it |
23
|
|
|
* |
24
|
|
|
* @param StreamMetaData $metadata Metadata for source |
25
|
|
|
* @return string See RESULT_XXX constants in the interface |
26
|
|
|
*/ |
27
|
1 |
|
public function transform(StreamMetaData $metadata) |
28
|
|
|
{ |
29
|
1 |
|
$selfValueVisitor = new SelfValueVisitor(); |
30
|
1 |
|
$traverser = new NodeTraverser(); |
31
|
1 |
|
$traverser->addVisitor($selfValueVisitor); |
32
|
1 |
|
$traverser->traverse($metadata->syntaxTree); |
33
|
|
|
|
34
|
1 |
|
$this->adjustSelfTokens($metadata, $selfValueVisitor->getReplacedNodes()); |
|
|
|
|
35
|
|
|
|
36
|
|
|
// We should always vote abstain, because if there are only changes for self we can drop them |
37
|
1 |
|
return self::RESULT_ABSTAIN; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adjusts tokens in the source code |
42
|
|
|
* |
43
|
|
|
* @param StreamMetaData $metadata |
44
|
|
|
* @param FullyQualified[] $replacedNodes Replaced nodes in the source code |
45
|
|
|
*/ |
46
|
1 |
|
private function adjustSelfTokens(StreamMetaData $metadata, array $replacedNodes) |
47
|
|
|
{ |
48
|
1 |
|
foreach ($replacedNodes as $replacedNode) |
49
|
|
|
{ |
50
|
1 |
|
$position = $replacedNode->getAttribute('startTokenPos'); |
51
|
1 |
|
$metadata->tokenStream[$position][1] = $replacedNode->toString(); |
52
|
|
|
} |
53
|
1 |
|
} |
54
|
|
|
} |
55
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: