Passed
Pull Request — master (#400)
by Théo
01:54
created

ConstStmtReplacer::enterNode()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 32
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 57
ccs 18
cts 18
cp 1
crap 6
rs 8.7857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 PhpParser\NodeVisitor\NameResolver;
18
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\FullyQualifiedNameResolver;
19
use Humbug\PhpScoper\Whitelist;
20
use PhpParser\Node;
21
use PhpParser\Node\Arg;
22
use PhpParser\Node\Expr\FuncCall;
23
use PhpParser\Node\Name;
24
use PhpParser\Node\Name\FullyQualified;
25
use PhpParser\Node\Scalar\String_;
26
use PhpParser\Node\Stmt\Const_;
27
use PhpParser\Node\Stmt\Expression;
28
use PhpParser\NodeVisitorAbstract;
29
use UnexpectedValueException;
30
use function count;
31
32
/**
33
 * Replaces const declaration by define when the constant is whitelisted.
34
 *
35
 * ```
36
 * const DUMMY_CONST = 'foo';
37
 * ```
38
 *
39
 * =>
40
 *
41
 * ```
42
 * define('DUMMY_CONST', 'foo');
43
 * ```
44
 *
45
 * It does not do the prefixing.
46
 *
47
 * @private
48
 */
49
final class ConstStmtReplacer extends NodeVisitorAbstract
50
{
51
    private Whitelist $whitelist;
52
    private FullyQualifiedNameResolver $nameResolver;
53 549
    private NameResolver $newNameResolver;
54
55 549
    public function __construct(
56 549
        Whitelist $whitelist,
57
        FullyQualifiedNameResolver $nameResolver,
58
        NameResolver $newNameResolver
59
    ) {
60
        $this->whitelist = $whitelist;
61
        $this->nameResolver = $nameResolver;
62
        $this->newNameResolver = $newNameResolver;
63
    }
64 548
65
    /**
66 548
     * @param Const_ $node
67 548
     */
68
    public function enterNode(Node $node): Node
69
    {
70 28
        if (!$node instanceof Const_) {
71
            return $node;
72 28
        }
73 28
74 28
        foreach ($node->consts as $constant) {
75 28
            /** @var Node\Const_ $constant */
76
            $oldResolvedConstantName = $this->nameResolver
77 28
                ->resolveName(
78
                    new Name(
79 28
                        (string) $constant->name,
80 23
                        $node->getAttributes(),
81
                    ),
82
                )
83 7
                ->getName();
84 1
            $resolvedConstantName = $this->newNameResolver
85
                ->getNameContext()
86
                ->getResolvedName(
87 1
                    new Name(
88
                        (string) $constant->name,
89
                        $node->getAttributes()
90
                    ),
91 6
                    Node\Stmt\Use_::TYPE_CONSTANT
92 6
                );
93 6
94
            if ((string) $oldResolvedConstantName !== (string) $resolvedConstantName) {
95 6
                // TODO: check those cases if relevant
96 6
                $x = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $x is dead and can be removed.
Loading history...
97
            }
98 6
99
            if (false === $this->whitelist->isSymbolWhitelisted((string) $resolvedConstantName, true)) {
100
                continue;
101
            }
102
103
            if (count($node->consts) > 1) {
104 23
                throw new UnexpectedValueException(
105
                    'Whitelisting a constant declared in a grouped constant statement (e.g. `const FOO = '
106
                    .'\'foo\', BAR = \'bar\'; is not supported. Consider breaking it down in multiple constant '
107
                    .'declaration statements'
108
                );
109
            }
110
111
            return new Expression(
112
                new FuncCall(
113
                    new FullyQualified('define'),
114
                    [
115
                        new Arg(
116
                            new String_((string) $resolvedConstantName)
117
                        ),
118
                        new Arg($constant->value),
119
                    ]
120
                )
121
            );
122
        }
123
124
        return $node;
125
    }
126
}
127