Test Failed
Push — master ( 1fd056...200868 )
by Théo
02:57
created

ClassIdentifierRecorder::enterNode()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 35

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
nc 6
nop 1
dl 35
loc 35
rs 8.0555
c 0
b 0
f 0
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\FullyQualifiedNameResolver;
18
use Humbug\PhpScoper\Whitelist;
19
use PhpParser\Node;
20
use PhpParser\Node\Identifier;
21
use PhpParser\Node\Name\FullyQualified;
22
use PhpParser\Node\Stmt\ClassLike;
23
use PhpParser\Node\Stmt\Trait_;
24
use PhpParser\NodeVisitorAbstract;
25
26
/**
27
 * Records the user classes registered in the global namespace which have been whitelisted and whitelisted classes.
28
 *
29
 * @private
30
 */
31 View Code Duplication
final class ClassIdentifierRecorder extends NodeVisitorAbstract
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
{
33
    private $prefix;
34
    private $nameResolver;
35
    private $whitelist;
36
37
    public function __construct(
38
        string $prefix,
39
        FullyQualifiedNameResolver $nameResolver,
40
        Whitelist $whitelist
41
    ) {
42
        $this->prefix = $prefix;
43
        $this->nameResolver = $nameResolver;
44
        $this->whitelist = $whitelist;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function enterNode(Node $node): Node
51
    {
52
        if (false === ($node instanceof Identifier) || false === ParentNodeAppender::hasParent($node)) {
53
            return $node;
54
        }
55
56
        $parent = ParentNodeAppender::getParent($node);
57
58
        if (false === ($parent instanceof ClassLike) || $parent instanceof Trait_) {
59
            return $node;
60
        }
61
        /** @var ClassLike $parent */
62
        if (null === $parent->name) {
63
            return $node;
64
        }
65
66
        /** @var Identifier $node */
67
        $resolvedName = $this->nameResolver->resolveName($node)->getName();
68
69
        if (false === ($resolvedName instanceof FullyQualified)) {
70
            return $node;
71
        }
72
73
        /** @var FullyQualified $resolvedName */
74
        if ($this->whitelist->isGlobalWhitelistedClass((string) $resolvedName)
75
            || $this->whitelist->isSymbolWhitelisted((string) $resolvedName)
76
        ) {
77
            $this->whitelist->recordWhitelistedClass(
78
                $resolvedName,
79
                FullyQualified::concat($this->prefix, $resolvedName)
1 ignored issue
show
Bug introduced by
It seems like \PhpParser\Node\Name\Ful...>prefix, $resolvedName) can be null; however, recordWhitelistedClass() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
            );
81
        }
82
83
        return $node;
84
    }
85
}
86