Passed
Pull Request — master (#400)
by Théo
02:04
created

ConstStmtReplacer::enterNode()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

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

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