Completed
Push — master ( 550011...0647fd )
by Théo
16:20 queued 07:57
created

ClassIdentifierRecorder::enterNode()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 30.9482

Importance

Changes 0
Metric Value
cc 9
nc 6
nop 1
dl 0
loc 35
ccs 6
cts 17
cp 0.3529
crap 30.9482
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
final class ClassIdentifierRecorder extends NodeVisitorAbstract
32
{
33
    private $prefix;
34
    private $nameResolver;
35
    private $whitelist;
36
37 10
    public function __construct(
38
        string $prefix,
39
        FullyQualifiedNameResolver $nameResolver,
40
        Whitelist $whitelist
41
    ) {
42 10
        $this->prefix = $prefix;
43 10
        $this->nameResolver = $nameResolver;
44 10
        $this->whitelist = $whitelist;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 10
    public function enterNode(Node $node): Node
51
    {
52 10
        if (false === ($node instanceof Identifier) || false === ParentNodeAppender::hasParent($node)) {
53 10
            return $node;
54
        }
55
56 6
        $parent = ParentNodeAppender::getParent($node);
57
58 6
        if (false === ($parent instanceof ClassLike) || $parent instanceof Trait_) {
59 6
            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