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

ConstStmtReplacer::enterNode()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

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

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
            $resolvedConstantName = $this->newNameResolver->getNameContext()->getResolvedName(
77 28
                new Name(
78
                    (string) $constant->name,
79 28
                    $node->getAttributes()
80 23
                ),
81
                Node\Stmt\Use_::TYPE_CONSTANT
82
            );
83 7
            $oldResolvedConstantName = $this->nameResolver
84 1
                ->resolveName(
85
                    new Name(
86
                        (string) $constant->name,
87 1
                        $node->getAttributes(),
88
                    ),
89
                )
90
                ->getName();
91 6
92 6
            if ((string) $oldResolvedConstantName !== (string) $resolvedConstantName) {
93 6
                xdebug_break();
94
                $x = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $x is dead and can be removed.
Loading history...
95 6
            }
96 6
97
            if (false === $this->whitelist->isSymbolWhitelisted((string) $resolvedConstantName, true)) {
98 6
                continue;
99
            }
100
101
            if (count($node->consts) > 1) {
102
                throw new UnexpectedValueException(
103
                    'Whitelisting a constant declared in a grouped constant statement (e.g. `const FOO = '
104 23
                    .'\'foo\', BAR = \'bar\'; is not supported. Consider breaking it down in multiple constant '
105
                    .'declaration statements'
106
                );
107
            }
108
109
            return new Expression(
110
                new FuncCall(
111
                    new FullyQualified('define'),
112
                    [
113
                        new Arg(
114
                            new String_((string) $resolvedConstantName)
115
                        ),
116
                        new Arg($constant->value),
117
                    ]
118
                )
119
            );
120
        }
121
122
        return $node;
123
    }
124
}
125