ConstStmtReplacer::__construct()   A
last analyzed

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\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` for exposed constants.
33
 *
34
 * ```
35
 * const DUMMY_CONST = 'foo';
36
 * ```
37
 *
38
 * =>
39
 *
40
 * ```
41
 * define('DUMMY_CONST', 'foo');
42
 * ```
43
 *
44
 * @private
45
 */
46
final class ConstStmtReplacer extends NodeVisitorAbstract
47
{
48
    private IdentifierResolver $identifierResolver;
49
    private EnrichedReflector $enrichedReflector;
50
51
    public function __construct(
52
        IdentifierResolver $identifierResolver,
53 549
        EnrichedReflector $enrichedReflector
54
    ) {
55 549
        $this->identifierResolver = $identifierResolver;
56 549
        $this->enrichedReflector = $enrichedReflector;
57
    }
58
59
    public function enterNode(Node $node): Node
60
    {
61
        if (!$node instanceof Const_) {
62
            return $node;
63
        }
64 548
65
        foreach ($node->consts as $constant) {
66 548
            $replacement = $this->replaceConst($node, $constant);
67 548
68
            if (null !== $replacement) {
69
                // If there is more than one constant declare in the node we
70 28
                // will not have a replacement (this case is not supported)
71
                // hence the return statement is safe here.
72 28
                return $replacement;
73 28
            }
74 28
        }
75 28
76
        return $node;
77 28
    }
78
79 28
    private function replaceConst(Const_ $const, Node\Const_ $constant): ?Node
80 23
    {
81
        $resolvedConstantName = $this->identifierResolver->resolveIdentifier(
82
            $constant->name,
83 7
        );
84 1
85
        if (!$this->enrichedReflector->isExposedConstant((string) $resolvedConstantName)) {
86
            // No replacement
87 1
            return null;
88
        }
89
90
        if (count($const->consts) > 1) {
91 6
            // TODO
92 6
            throw new UnexpectedValueException(
93 6
                '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',
94
            );
95 6
        }
96 6
97
        return self::createConstDefineNode(
98 6
            (string) $resolvedConstantName,
99
            $constant->value,
100
        );
101
    }
102
103
    private static function createConstDefineNode(string $name, Expr $value): Node
104 23
    {
105
        return new Expression(
106
            new FuncCall(
107
                new FullyQualified('define'),
108
                [
109
                    new Arg(
110
                        new String_($name),
111
                    ),
112
                    new Arg($value),
113
                ],
114
            ),
115
        );
116
    }
117
}
118