SelfValueTransformer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

2 Methods

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