Passed
Pull Request — master (#565)
by Théo
02:31
created

ConstStmtReplacer::createDefineNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 10
ccs 1
cts 1
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\Symbol\EnrichedReflector;
19
use PhpParser\Node;
20
use PhpParser\Node\Arg;
21
use PhpParser\Node\Expr;
22
use PhpParser\Node\Expr\FuncCall;
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 constants `const` declarations by `define` when the constant is exposed.
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 IdentifierResolver $identifierResolver;
51
    private EnrichedReflector $enrichedReflector;
52
53 549
    public function __construct(
54
        IdentifierResolver $identifierResolver,
55 549
        EnrichedReflector $enrichedReflector
56 549
    ) {
57
        $this->identifierResolver = $identifierResolver;
58
        $this->enrichedReflector = $enrichedReflector;
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->identifierResolver->resolveIdentifier(
81
            $constant->name,
82
        );
83 7
84 1
        if (!$this->enrichedReflector->isExposedConstant((string) $resolvedConstantName)) {
85
            return null;
86
        }
87 1
88
        if (count($const->consts) > 1) {
89
            throw new UnexpectedValueException(
90
                'Exposing 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',
91 6
            );
92 6
        }
93 6
94
        return self::createDefineNode(
95 6
            (string) $resolvedConstantName,
96 6
            $constant->value,
97
        );
98 6
    }
99
100
    private static function createDefineNode(string $resolvedConstantName, Expr $value): Node
101
    {
102
        return new Expression(
103
            new FuncCall(
104 23
                new FullyQualified('define'),
105
                [
106
                    new Arg(
107
                        new String_($resolvedConstantName)
108
                    ),
109
                    new Arg($value),
110
                ],
111
            ),
112
        );
113
    }
114
}
115