Completed
Push — master ( 550011...0647fd )
by Théo
16:20 queued 07:57
created

StringScoperPrefixer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 36.36%

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 4
cts 11
cp 0.3636
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A scopeStringValue() 0 16 3
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 10
    public function __construct(PhpScoper $scoper, string $prefix, Whitelist $whitelist)
30
    {
31 10
        $this->scoper = $scoper;
32 10
        $this->prefix = $prefix;
33 10
        $this->whitelist = $whitelist;
34
    }
35
36
    private function scopeStringValue(String_ $node): void
37
    {
38
        try {
39
            $lastChar = substr($node->value, -1);
40
41
            $newValue = $this->scoper->scopePhp($node->value, $this->prefix, $this->whitelist);
42
43
            if ("\n" !== $lastChar) {
44
                $newValue = substr($newValue, 0, -1);
45
            }
46
47
            $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