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

ClassIdentifierRecorder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 55
loc 55
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
B enterNode() 35 35 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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