Completed
Push — master ( 74c262...0b4843 )
by Théo
03:38 queued 01:33
created

StringScalarPrefixer::prefixStringScalar()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 5
nop 1
dl 0
loc 32
rs 6.7272
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\NodeVisitor;
16
17
use Humbug\PhpScoper\NodeVisitor\Resolver\FullyQualifiedNameResolver;
18
use PhpParser\Node;
19
use PhpParser\Node\Arg;
20
use PhpParser\Node\Name;
21
use PhpParser\Node\Name\FullyQualified;
22
use PhpParser\Node\Scalar\String_;
23
use PhpParser\NodeVisitorAbstract;
24
25
/**
26
 * Prefixes the string scalar values.
27
 *
28
 * ```
29
 * $x = 'Foo\Bar';
30
 * ```
31
 *
32
 * =>
33
 *
34
 * ```
35
 * $x = 'Humbug\Foo\Bar';
36
 * ```
37
 */
38
final class StringScalarPrefixer extends NodeVisitorAbstract
39
{
40
    private $prefix;
41
    private $whitelist;
42
    private $globalWhitelister;
43
    private $nameResolver;
44
45
    /**
46
     * @param string                     $prefix
47
     * @param string[]                   $whitelist
48
     * @param callable                   $globalWhitelister
49
     * @param FullyQualifiedNameResolver $nameResolver
50
     */
51 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
        string $prefix,
53
        array $whitelist,
54
        callable $globalWhitelister,
55
        FullyQualifiedNameResolver $nameResolver
56
    ) {
57
        $this->prefix = $prefix;
58
        $this->whitelist = $whitelist;
59
        $this->globalWhitelister = $globalWhitelister;
60
        $this->nameResolver = $nameResolver;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function enterNode(Node $node): Node
67
    {
68
        return ($node instanceof String_ && AppendParentNode::hasParent($node))
69
            ? $this->prefixStringScalar($node)
70
            : $node
71
        ;
72
    }
73
74
    private function prefixStringScalar(String_ $string): Node
75
    {
76
        $parentNode = AppendParentNode::getParent($string);
77
78
        if (false === ($parentNode instanceof Arg)) {
79
            return $string;
80
        }
81
82
        $stringName = new Name(
83
            preg_replace('/^\\\\(.+)$/', '$1', $string->value),
84
            $string->getAttributes()
85
        );
86
87
        // Skip if is already prefixed
88
        if ($this->prefix === $stringName->getFirst()) {
89
            $newStringName = $stringName;
90
            // Check if the class can be prefixed: class from the global namespace
91
        } elseif (1 === count($stringName->parts)
92
            && false === ($this->globalWhitelister)($stringName->toString())
93
        ) {
94
            $newStringName = $stringName;
95
            // Check if the class can be prefixed: regular class
96
        } elseif (1 < count($stringName->parts)
97
            && in_array($stringName->toString(), $this->whitelist)
98
        ) {
99
            $newStringName = $stringName;
100
        } else {
101
            $newStringName = FullyQualified::concat($this->prefix, $stringName->toString(), $stringName->getAttributes());
102
        }
103
104
        return new String_($newStringName->toString(), $string->getAttributes());
105
    }
106
}
107