Passed
Push — master ( 6704cb...7d5ecd )
by Théo
03:14 queued 01:01
created

NewdocPrefixer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 31
ccs 10
cts 10
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A enterNode() 0 7 3
A isPhpNowdoc() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\PhpParser\NodeVisitor;
16
17
use Humbug\PhpScoper\PhpParser\StringNodePrefixer;
18
use PhpParser\Node;
19
use PhpParser\Node\Scalar\String_;
20
use PhpParser\NodeVisitorAbstract;
21
use function ltrim;
22
use function Safe\substr;
23
use function strpos;
24
25
final class NewdocPrefixer extends NodeVisitorAbstract
26
{
27
    private StringNodePrefixer $stringPrefixer;
28
29
    public function __construct(StringNodePrefixer $stringPrefixer)
30
    {
31
        $this->stringPrefixer = $stringPrefixer;
32 548
    }
33
34 548
    public function enterNode(Node $node): Node
35 2
    {
36
        if ($node instanceof String_ && $this->isPhpNowdoc($node)) {
37
            $this->stringPrefixer->prefixStringValue($node);
38 548
        }
39
40
        return $node;
41 102
    }
42
43 102
    private function isPhpNowdoc(String_ $node): bool
44 96
    {
45
        if (String_::KIND_NOWDOC !== $node->getAttribute('kind')) {
46
            return false;
47 7
        }
48 7
49 7
        return 0 === strpos(
50
            substr(
51
                ltrim($node->value),
52
                0,
53
                5,
54
            ),
55
            '<?php',
56
        );
57
    }
58
}
59