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

StringNodePrefixer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
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;
16
17
use Humbug\PhpScoper\Scoper\PhpScoper;
18
use Humbug\PhpScoper\Whitelist;
19
use PhpParser\Error as PhpParserError;
20
use PhpParser\Node\Scalar\String_;
21
use function Safe\substr;
22
23
/**
24
 * @private
25
 */
26
final class StringNodePrefixer
27
{
28
    private PhpScoper $scoper;
29
    private string $prefix;
30
    private Whitelist $whitelist;
31
32
    public function __construct(PhpScoper $scoper, string $prefix, Whitelist $whitelist)
33
    {
34
        $this->scoper = $scoper;
35
        $this->prefix = $prefix;
36
        $this->whitelist = $whitelist;
37
    }
38
39
    public function prefixStringValue(String_ $node): void
40
    {
41
        try {
42
            $lastChar = substr($node->value, -1);
43
44
            $newValue = $this->scoper->scopePhp(
45
                $node->value,
46
                $this->prefix,
47
                $this->whitelist,
48
            );
49
50
            if ("\n" !== $lastChar) {
51
                $newValue = substr($newValue, 0, -1);
52
            }
53
54
            $node->value = $newValue;
55
        } catch (PhpParserError $error) {
56
            // Continue without scoping the heredoc which for some reasons contains invalid PHP code
57
        }
58
    }
59
}
60