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.
Passed
Push — master ( 8a5544...b68e37 )
by SignpostMarv
02:37
created

StaticMethodCollector::shouldContainInterfaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
* @author SignpostMarv
6
*/
7
8
namespace SignpostMarv\DaftInterfaceCollector;
9
10
use Generator;
11
use ReflectionMethod;
12
use Traversable;
13
14
class StaticMethodCollector
15
{
16
    /**
17
    * @param array<string, array<int, string>> $staticMethods
18
    */
19
    private $staticMethods;
20
21
    /**
22
    * @var array<int, string>
23
    */
24
    private $processedSources = [];
25
26
    /**
27
    * @var bool
28
    */
29
    private $autoResetProcessedSources;
30
31 4
    public function __construct(array $staticMethods, bool $autoResetProcessedSources = true)
32
    {
33 4
        $filtered = array_map(
34
            /**
35
            * @return string[]
36
            */
37
            function (array $shouldContainInterfaces) : array {
38
                /**
39
                * @var string[] $out
40
                */
41 4
                $out = array_filter($shouldContainInterfaces, [$this, 'shouldContainInterfaces']);
42
43 4
                return array_values($out);
44 4
            },
45 4
            array_filter($staticMethods, 'is_array')
46
        );
47
48 4
        $this->staticMethods = $filtered;
49 4
        $this->autoResetProcessedSources = $autoResetProcessedSources;
50 4
    }
51
52 4
    public function Collect(string ...$implementations) : Generator
53
    {
54 4
        if ($this->autoResetProcessedSources) {
55 2
            $this->processedSources = [];
56
        }
57
58 4
        yield from $this->CollectInterfaces(...$implementations);
59 4
    }
60
61 4
    protected function CollectInterfaces(string ...$implementations) : Generator
62
    {
63 4
        foreach (array_filter($implementations, 'class_exists') as $implementation) {
64 4
            if (in_array($implementation, $this->processedSources, true)) {
65 2
                continue;
66
            }
67 4
            $this->processedSources[] = $implementation;
68
69
            /**
70
            * @var string $method
71
            * @var array<int, string> $interfaces
72
            */
73 4
            foreach ($this->staticMethods as $method => $interfaces) {
74 4
                if ( ! method_exists($implementation, $method)) {
75 4
                    continue;
76
                }
77
78 4
                $ref = new ReflectionMethod($implementation, $method);
79
80
                if (
81 4
                    ! $ref->isStatic() ||
82 4
                    ! $ref->isPublic() ||
83 4
                    0 < $ref->getNumberOfRequiredParameters() ||
84 4
                    ! $ref->hasReturnType()
85
                ) {
86
                    continue;
87
                }
88
89
                /**
90
                * @var \ReflectionType $refReturn
91
                */
92 4
                $refReturn = $ref->getReturnType();
93
94
                if (
95
                    ! (
96 4
                        'array' === $refReturn->__toString() ||
0 ignored issues
show
Deprecated Code introduced by
The function ReflectionType::__toString() has been deprecated: 7.1.0:8.0.0 Please use getName() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

96
                        'array' === /** @scrutinizer ignore-deprecated */ $refReturn->__toString() ||

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
97 4
                        is_a((string) $refReturn->__toString(), Traversable::class, true)
0 ignored issues
show
Deprecated Code introduced by
The function ReflectionType::__toString() has been deprecated: 7.1.0:8.0.0 Please use getName() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
                        is_a((string) /** @scrutinizer ignore-deprecated */ $refReturn->__toString(), Traversable::class, true)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98
                    )
99
                ) {
100
                    continue;
101
                }
102
103
                /**
104
                * @var array|Traversable $methodResult
105
                */
106 4
                $methodResult = $implementation::$method();
107
108 4
                if (is_iterable($methodResult)) {
109
                    /**
110
                    * @var string $perhapsYield
111
                    */
112
                    foreach (
113 4
                        array_filter(
114
                            (
115 4
                                is_array($methodResult)
116 4
                                    ? $methodResult
117 4
                                    : iterator_to_array($methodResult)
118
                            ),
119 4
                            'is_string'
120
                        ) as $perhapsYield
121
                    ) {
122 4
                        foreach ($interfaces as $interface) {
123 4
                            if (is_a($perhapsYield, $interface, true)) {
124 4
                                yield $perhapsYield;
125 4
                                break;
126
                            }
127
                        }
128
129
                        if (
130 4
                            ! in_array($perhapsYield, $this->processedSources, true)
131
                        ) {
132 4
                            yield from $this->CollectInterfaces($perhapsYield);
133
                        }
134
                    }
135
                }
136
            }
137
        }
138 4
    }
139
140
    /**
141
    * @param mixed $maybe
142
    */
143 4
    protected function shouldContainInterfaces($maybe) : bool
144
    {
145 4
        return is_string($maybe) && interface_exists($maybe);
146
    }
147
}
148