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
Push — master ( cab5dd...db78bc )
by Constantin
04:29
created

classNameFromParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\CodeAnalysis\Shared\ClassSorter;
7
8
9
use Gica\CodeAnalysis\Shared\ClassSorter;
10
11
class ByConstructorDependencySorter implements ClassSorter
12
{
13
14 1
    public function __invoke(\ReflectionClass $a, \ReflectionClass $b)
15
    {
16 1
        if ($this->doesClassDependsOnClass($a, $b)) {
17 1
            return 1;
18
        }
19
20 1
        if ($this->doesClassDependsOnClass($b, $a)) {
21 1
            return -1;
22
        }
23
24 1
        return strcmp($a->name, $b->name);
25
    }
26
27 1
    private function doesClassDependsOnClass(\ReflectionClass $consumerClass, \ReflectionClass $consumedClass)
28
    {
29 1
        $dependencies = $this->getClassDependencies($consumerClass);
30
31 1
        if ($this->isParentClassOfAny($consumedClass, $dependencies)) {
32
            //echo "{$consumerClass->name} depends on {$consumedClass->name}\n";
33 1
            return true;
34
        } else {
35
            //echo "{$a->name} does not depend on {$b->name}\n";
36 1
            return false;
37
        }
38
    }
39
40
    /**
41
     * @param \ReflectionClass $reflectionClass
42
     * @return string[]
43
     */
44 1
    private function getClassDependencies(\ReflectionClass $reflectionClass)
45
    {
46 1
        $constructor = $reflectionClass->getConstructor();
47 1
        if (!$constructor) {
48 1
            return [];
49
        }
50
51 1
        return $this->classNameFromParameters($constructor->getParameters());
52
    }
53
54
55 1
    private function isParentClassOfAny(\ReflectionClass $parentClass, $classes)
56
    {
57
        $isSubClassOf = function ($class) use ($parentClass) {
58 1
            return $parentClass->name === $class || is_subclass_of($class, $parentClass->name, true);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $parentClass->name can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
59 1
        };
60
61 1
        $filtered = array_filter($classes, $isSubClassOf);
62
63 1
        return count($filtered) > 0;
64
65
    }
66
67 1
    private function classNameFromParameter(\ReflectionParameter $parameter)
68
    {
69 1
        return $parameter->getClass()->name;
70
    }
71
72
    /**
73
     * @param \ReflectionParameter[] $parameters
74
     * @return string[]
75
     */
76 1
    private function classNameFromParameters(array $parameters)
77
    {
78
        $strings = array_map(function (\ReflectionParameter $parameter) {
79 1
            return $this->classNameFromParameter($parameter);
80 1
        }, $parameters);
81
82 1
        return array_filter($strings, function ($s) {
83 1
            return !!$s;
84 1
        });
85
    }
86
}