Completed
Push — master ( eee693...06da57 )
by Alexander
06:29
created

SelfValueTransformer::transform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2018, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Instrument\Transformer;
13
14
use PhpParser\Node\Name\FullyQualified;
15
use PhpParser\NodeTraverser;
16
17
/**
18
 * Transformer that replaces `self` constants in the source code, e.g. new self()
19
 */
20
class SelfValueTransformer extends BaseSourceTransformer
21
{
22
    /**
23
     * This method may transform the supplied source and return a new replacement for it
24
     *
25
     * @param StreamMetaData $metadata Metadata for source
26
     * @return string See RESULT_XXX constants in the interface
27
     */
28 1
    public function transform(StreamMetaData $metadata): string
29
    {
30 1
        $selfValueVisitor = new SelfValueVisitor();
31 1
        $traverser        = new NodeTraverser();
32 1
        $traverser->addVisitor($selfValueVisitor);
33 1
        $traverser->traverse($metadata->syntaxTree);
34
35 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...
36
37
        // We should always vote abstain, because if there are only changes for self we can drop them
38 1
        return self::RESULT_ABSTAIN;
39
    }
40
41
    /**
42
     * Adjusts tokens in the source code
43
     *
44
     * @param StreamMetaData $metadata
45
     * @param FullyQualified[] $replacedNodes Replaced nodes in the source code
46
     */
47 1
    private function adjustSelfTokens(StreamMetaData $metadata, array $replacedNodes)
48
    {
49 1
        foreach ($replacedNodes as $replacedNode)
50
        {
51 1
            $position = $replacedNode->getAttribute('startTokenPos');
52 1
            $metadata->tokenStream[$position][1] = $replacedNode->toString();
53
        }
54 1
    }
55
}
56