Completed
Pull Request — 2.x (#381)
by Alexander
02:10
created

SelfValueTransformer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 36
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 12 1
A adjustSelfTokens() 0 8 2
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());
0 ignored issues
show
Documentation introduced by
$selfValueVisitor->getReplacedNodes() is of type array<integer,object<PhpParser\Node>>, but the function expects a array<integer,object<Php...e\Name\FullyQualified>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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