Passed
Pull Request — master (#506)
by Théo
02:19
created

ConstStmtReplacer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 52
ccs 23
cts 23
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceConst() 0 22 3
A __construct() 0 6 1
A enterNode() 0 15 4
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;
23
use PhpParser\Node\Name\FullyQualified;
24
use PhpParser\Node\Scalar\String_;
25
use PhpParser\Node\Stmt\Const_;
26
use PhpParser\Node\Stmt\Expression;
27
use PhpParser\NodeVisitorAbstract;
28
use UnexpectedValueException;
29
use function count;
30
31
/**
32
 * Replaces const declaration by define when the constant is whitelisted.
33
 *
34
 * ```
35
 * const DUMMY_CONST = 'foo';
36
 * ```
37
 *
38
 * =>
39
 *
40
 * ```
41
 * define('DUMMY_CONST', 'foo');
42
 * ```
43
 *
44
 * It does not do the prefixing.
45
 *
46
 * @private
47
 */
48
final class ConstStmtReplacer extends NodeVisitorAbstract
49
{
50
    private Whitelist $whitelist;
51
    private IdentifierResolver $nameResolver;
52
53 549
    public function __construct(
54
        Whitelist $whitelist,
55 549
        IdentifierResolver $nameResolver
56 549
    ) {
57
        $this->whitelist = $whitelist;
58
        $this->nameResolver = $nameResolver;
59
    }
60
61
    public function enterNode(Node $node): Node
62
    {
63
        if (!$node instanceof Const_) {
64 548
            return $node;
65
        }
66 548
67 548
        foreach ($node->consts as $constant) {
68
            $replacement = $this->replaceConst($node, $constant);
69
70 28
            if (null !== $replacement) {
71
                return $replacement;
72 28
            }
73 28
        }
74 28
75 28
        return $node;
76
    }
77 28
78
    private function replaceConst(Const_ $const, Node\Const_ $constant): ?Node
79 28
    {
80 23
        $resolvedConstantName = $this->nameResolver->resolveIdentifier($constant->name);
81
82
        if (false === $this->whitelist->isSymbolWhitelisted((string) $resolvedConstantName, true)) {
83 7
            return null;
84 1
        }
85
86
        if (count($const->consts) > 1) {
87 1
            throw new UnexpectedValueException(
88
                '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',
89
            );
90
        }
91 6
92 6
        return new Expression(
93 6
            new FuncCall(
94
                new FullyQualified('define'),
95 6
                [
96 6
                    new Arg(
97
                        new String_((string) $resolvedConstantName)
98 6
                    ),
99
                    new Arg($constant->value),
100
                ],
101
            ),
102
        );
103
    }
104
}
105