GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#837)
by E
07:31 queued 05:03
created

ClassTraitElementsExtractor::getUsedProperties()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 8
nop 0
dl 0
loc 28
ccs 16
cts 16
cp 1
crap 6
rs 8.439
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace ApiGen\Parser\Reflection\Extractors;
4
5
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface;
6
use ApiGen\Contracts\Parser\Reflection\Extractors\ClassTraitElementsExtractorInterface;
7
use ApiGen\Contracts\Parser\Reflection\MethodReflectionInterface;
8
use ApiGen\Contracts\Parser\Reflection\PropertyReflectionInterface;
9
use TokenReflection\IReflection;
10
11
final class ClassTraitElementsExtractor implements ClassTraitElementsExtractorInterface
12
{
13
    /**
14
     * @var ClassReflectionInterface
15
     */
16
    private $classReflection;
17
18
    /**
19
     * @var \TokenReflection\IReflection|ClassReflectionInterface
20
     */
21
    private $originalReflection;
22
23 127
    public function __construct(ClassReflectionInterface $classReflection, IReflection $originalReflection)
24
    {
25 127
        $this->classReflection = $classReflection;
26 127
        $this->originalReflection = $originalReflection;
27 127
    }
28
29
    /**
30
     * @return ClassReflectionInterface[]
31
     */
32 1 View Code Duplication
    public function getDirectUsers(): array
0 ignored issues
show
Duplication introduced by
This method 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...
33
    {
34 1
        $users = [];
35 1
        $name = $this->classReflection->getName();
36 1
        foreach ($this->classReflection->getParsedClasses() as $class) {
37 1
            if (! $class->isDocumented()) {
38
                continue;
39
            }
40
41 1
            if (in_array($name, $class->getOwnTraitNames())) {
42 1
                $users[] = $class;
43
            }
44
        }
45
46 1
        uksort($users, 'strcasecmp');
47 1
        return $users;
48
    }
49
50
    /**
51
     * @return ClassReflectionInterface[]
52
     */
53 1 View Code Duplication
    public function getIndirectUsers(): array
0 ignored issues
show
Duplication introduced by
This method 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...
54
    {
55 1
        $users = [];
56 1
        $name = $this->classReflection->getName();
57 1
        foreach ($this->classReflection->getParsedClasses() as $class) {
58 1
            if (! $class->isDocumented()) {
59
                continue;
60
            }
61
62 1
            if ($class->usesTrait($name) && ! in_array($name, $class->getOwnTraitNames())) {
63
                $users[] = $class;
64
            }
65
        }
66
67 1
        uksort($users, 'strcasecmp');
68 1
        return $users;
69
    }
70
71
    /**
72
     * @return PropertyReflectionInterface[]
73
     */
74 1 View Code Duplication
    public function getTraitProperties(): array
0 ignored issues
show
Duplication introduced by
This method 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...
75
    {
76 1
        $properties = [];
77 1
        $traitProperties = $this->originalReflection->getTraitProperties($this->classReflection->getVisibilityLevel());
0 ignored issues
show
Unused Code introduced by
The call to ClassReflectionInterface::getTraitProperties() has too many arguments starting with $this->classReflection->getVisibilityLevel().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
78 1
        foreach ($traitProperties as $property) {
79 1
            $apiProperty = $this->classReflection->getReflectionFactory()->createFromReflection($property);
80 1
            if (! $this->classReflection->isDocumented() || $apiProperty->isDocumented()) {
81 1
                $properties[$property->getName()] = $apiProperty;
82
            }
83
        }
84
85 1
        return $properties;
86
    }
87
88
    /**
89
     * @return MethodReflectionInterface[]
90
     */
91 1 View Code Duplication
    public function getTraitMethods(): array
0 ignored issues
show
Duplication introduced by
This method 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...
92
    {
93 1
        $methods = [];
94 1
        foreach ($this->originalReflection->getTraitMethods($this->classReflection->getVisibilityLevel()) as $method) {
0 ignored issues
show
Unused Code introduced by
The call to ClassReflectionInterface::getTraitMethods() has too many arguments starting with $this->classReflection->getVisibilityLevel().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
95 1
            $apiMethod = $this->classReflection->getReflectionFactory()->createFromReflection($method);
96 1
            if (! $this->classReflection->isDocumented() || $apiMethod->isDocumented()) {
97 1
                $methods[$method->getName()] = $apiMethod;
98
            }
99
        }
100
101 1
        return $methods;
102
    }
103
104
    /**
105
     * @return PropertyReflectionInterface[]
106
     */
107
    public function getUsedProperties(): array
108
    {
109 2
        $allProperties = array_flip(array_map(function (PropertyReflectionInterface $property) {
110 1
            return $property->getName();
111 2
        }, $this->classReflection->getOwnProperties()));
112
113 2
        $properties = [];
114 2
        foreach ($this->classReflection->getTraits() as $trait) {
115 1
            if (! $trait instanceof ClassReflectionInterface) {
116 1
                continue;
117
            }
118
119 1
            $usedProperties = [];
120 1
            foreach ($trait->getOwnProperties() as $property) {
121 1
                if (! array_key_exists($property->getName(), $allProperties)) {
122 1
                    $usedProperties[$property->getName()] = $property;
123 1
                    $allProperties[$property->getName()] = null;
124
                }
125
            }
126
127 1
            if (! empty($usedProperties)) {
128 1
                ksort($usedProperties);
129 1
                $properties[$trait->getName()] = array_values($usedProperties);
130
            }
131
        }
132
133 2
        return $properties;
134
    }
135
136
    /**
137
     * @return MethodReflectionInterface[]
138
     */
139 2
    public function getUsedMethods(): array
140
    {
141 2
        $usedMethods = [];
142 2
        foreach ($this->classReflection->getMethods() as $methodReflection) {
143 1
            if ($methodReflection->getDeclaringTraitName() === ''
144 1
                || $methodReflection->getDeclaringTraitName() === $this->classReflection->getName()
145
            ) {
146 1
                continue;
147
            }
148
149 1
            $traitName = $methodReflection->getDeclaringTraitName();
150 1
            $methodName = $methodReflection->getName();
151
152 1
            $usedMethods[$traitName][$methodName]['method'] = $methodReflection;
153 1
            if ($this->wasMethodNameAliased($methodReflection)) {
0 ignored issues
show
Bug introduced by
It seems like $methodReflection defined by $methodReflection on line 142 can also be of type object<ApiGen\Parser\Ref...\Parts\VisibilityTrait>; however, ApiGen\Parser\Reflection...:wasMethodNameAliased() does only seem to accept object<ApiGen\Contracts\...hodReflectionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
154 1
                $usedMethods[$traitName][$methodName]['aliases'][$methodReflection->getName()] = $methodReflection;
155
            }
156
        }
157
158 2
        return $usedMethods;
159
    }
160
161 1
    private function wasMethodNameAliased(MethodReflectionInterface $methodReflection): bool
162
    {
163 1
        return $methodReflection->getOriginalName() !== null
164 1
            && $methodReflection->getOriginalName() !== $methodReflection->getName();
165
    }
166
}
167