Passed
Push — master ( c508dd...fecc04 )
by Théo
02:26
created

StringScoperPrefixer::scopeStringValue()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
nc 5
nop 1
dl 0
loc 16
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 9.7333
c 0
b 0
f 0
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 substr;
22
23
trait StringScoperPrefixer
24
{
25
    private $scoper;
26
    private $prefix;
27
    private $whitelist;
28
29 548
    public function __construct(PhpScoper $scoper, string $prefix, Whitelist $whitelist)
30
    {
31 548
        $this->scoper = $scoper;
32 548
        $this->prefix = $prefix;
33 548
        $this->whitelist = $whitelist;
34
    }
35
36 7
    private function scopeStringValue(String_ $node): void
37
    {
38
        try {
39 7
            $lastChar = substr($node->value, -1);
40
41 7
            $newValue = $this->scoper->scopePhp($node->value, $this->prefix, $this->whitelist);
42
43 7
            if ("\n" !== $lastChar) {
44 4
                $newValue = substr($newValue, 0, -1);
45
            }
46
47 7
            $node->value = $newValue;
48
        } catch (PhpParserError $error) {
49
            // Continue without scoping the heredoc which for some reasons contains invalid PHP code
50
        }
51
    }
52
}
53