Passed
Pull Request — master (#506)
by Théo
03:11
created

ConstStmtReplacer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
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\NodeVisitor;
16
17
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver;
18
use Humbug\PhpScoper\Whitelist;
19
use PhpParser\Node;
20
use PhpParser\Node\Arg;
21
use PhpParser\Node\Expr\FuncCall;
22
use PhpParser\Node\Name\FullyQualified;
23
use PhpParser\Node\Scalar\String_;
24
use PhpParser\Node\Stmt\Const_;
25
use PhpParser\Node\Stmt\Expression;
26
use PhpParser\NodeVisitorAbstract;
27
use UnexpectedValueException;
28
use function count;
29
30
/**
31
 * Replaces const declaration by define when the constant is whitelisted.
32
 *
33
 * ```
34
 * const DUMMY_CONST = 'foo';
35
 * ```
36
 *
37
 * =>
38
 *
39
 * ```
40
 * define('DUMMY_CONST', 'foo');
41
 * ```
42
 *
43
 * It does not do the prefixing.
44
 *
45
 * @private
46
 */
47
final class ConstStmtReplacer extends NodeVisitorAbstract
48
{
49
    private Whitelist $whitelist;
50
    private IdentifierResolver $identifierResolver;
51
52
    public function __construct(
53 549
        Whitelist $whitelist,
54
        IdentifierResolver $identifierResolver
55 549
    ) {
56 549
        $this->whitelist = $whitelist;
57
        $this->identifierResolver = $identifierResolver;
58
    }
59
60
    public function enterNode(Node $node): Node
61
    {
62
        if (!$node instanceof Const_) {
63
            return $node;
64 548
        }
65
66 548
        foreach ($node->consts as $constant) {
67 548
            $replacement = $this->replaceConst($node, $constant);
68
69
            if (null !== $replacement) {
70 28
                return $replacement;
71
            }
72 28
        }
73 28
74 28
        return $node;
75 28
    }
76
77 28
    private function replaceConst(Const_ $const, Node\Const_ $constant): ?Node
78
    {
79 28
        $resolvedConstantName = $this->identifierResolver->resolveIdentifier(
80 23
            $constant->name,
81
        );
82
83 7
        if (false === $this->whitelist->isSymbolWhitelisted((string) $resolvedConstantName, true)) {
84 1
            return null;
85
        }
86
87 1
        if (count($const->consts) > 1) {
88
            throw new UnexpectedValueException(
89
                'Whitelisting a constant declared in a grouped constant statement (e.g. `const FOO = \'foo\', BAR = \'bar\'; is not supported. Consider breaking it down in multiple constant declaration statements',
90
            );
91 6
        }
92 6
93 6
        return new Expression(
94
            new FuncCall(
95 6
                new FullyQualified('define'),
96 6
                [
97
                    new Arg(
98 6
                        new String_((string) $resolvedConstantName)
99
                    ),
100
                    new Arg($constant->value),
101
                ],
102
            ),
103
        );
104 23
    }
105
}
106