Test Setup Failed
Push — master ( c4a7db...8049ea )
by Théo
02:17
created

FunctionIdentifierRecorder::enterNode()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 1
dl 0
loc 37
rs 8.3946
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;
22
use PhpParser\Node\Name\FullyQualified;
23
use PhpParser\Node\Stmt\Function_;
24
use PhpParser\NodeVisitorAbstract;
25
use function count;
26
27
/**
28
 * Records the user functions registered in the global namespace.
29
 *
30
 * @private
31
 */
32
final class FunctionIdentifierRecorder extends NodeVisitorAbstract
33
{
34
    private $prefix;
35
    private $nameResolver;
36
    private $whitelist;
37
38
    public function __construct(
39
        string $prefix,
40
        FullyQualifiedNameResolver $nameResolver,
41
        Whitelist $whitelist
42
    ) {
43
        $this->prefix = $prefix;
44
        $this->nameResolver = $nameResolver;
45
        $this->whitelist = $whitelist;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function enterNode(Node $node): Node
52
    {
53
        if (false === ($node instanceof Identifier) || false === AppendParentNode::hasParent($node)) {
54
            return $node;
55
        }
56
57
        $parent = AppendParentNode::getParent($node);
58
59
        if (false === ($parent instanceof Function_)) {
60
            return $node;
61
        }
62
63
        /** @var Identifier $node */
64
        $resolvedValue = $this->nameResolver->resolveName(
65
            new Name(
66
                $node->name,
67
                $node->getAttributes()
68
            )
69
        );
70
        $resolvedName = $resolvedValue->getName();
71
72
        if (null !== $resolvedValue->getNamespace()
73
            || false === ($resolvedName instanceof FullyQualified)
74
            || count($resolvedName->parts) > 1
75
        ) {
76
            return $node;
77
        }
78
79
        /* @var FullyQualified $resolvedName */
80
81
        $this->whitelist->recordUserGlobalFunction(
82
            $resolvedName,
83
            FullyQualified::concat($this->prefix, $resolvedName)
0 ignored issues
show
Bug introduced by
It seems like \PhpParser\Node\Name\Ful...>prefix, $resolvedName) can be null; however, recordUserGlobalFunction() 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...
84
        );
85
86
        return $node;
87
    }
88
}
89