Completed
Push — master ( 0647fd...b9e92e )
by Théo
27:07 queued 08:01
created

ClassIdentifierRecorder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B enterNode() 0 35 9
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 548
    public function __construct(
38
        string $prefix,
39
        FullyQualifiedNameResolver $nameResolver,
40
        Whitelist $whitelist
41
    ) {
42 548
        $this->prefix = $prefix;
43 548
        $this->nameResolver = $nameResolver;
44 548
        $this->whitelist = $whitelist;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 547
    public function enterNode(Node $node): Node
51
    {
52 547
        if (false === ($node instanceof Identifier) || false === ParentNodeAppender::hasParent($node)) {
53 547
            return $node;
54
        }
55
56 382
        $parent = ParentNodeAppender::getParent($node);
57
58 382
        if (false === ($parent instanceof ClassLike) || $parent instanceof Trait_) {
59 320
            return $node;
60
        }
61
        /** @var ClassLike $parent */
62 292
        if (null === $parent->name) {
63
            return $node;
64
        }
65
66
        /** @var Identifier $node */
67 292
        $resolvedName = $this->nameResolver->resolveName($node)->getName();
68
69 292
        if (false === ($resolvedName instanceof FullyQualified)) {
70
            return $node;
71
        }
72
73
        /** @var FullyQualified $resolvedName */
74 292
        if ($this->whitelist->isGlobalWhitelistedClass((string) $resolvedName)
75 292
            || $this->whitelist->isSymbolWhitelisted((string) $resolvedName)
76
        ) {
77 81
            $this->whitelist->recordWhitelistedClass(
78 81
                $resolvedName,
79 81
                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 292
        return $node;
84
    }
85
}
86