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 ( 59446a...77a017 )
by Constantin
02:32
created

MethodListenerDiscovery::isMethodAcccepted()   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;
7
8
9
use Gica\CodeAnalysis\MethodListenerDiscovery\ClassSorter;
10
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator;
11
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerMethod;
12
use Gica\CodeAnalysis\MethodListenerDiscovery\MessageClassDetector;
13
use Gica\CodeAnalysis\Traits\FilesInDirectoryExtracter;
14
15
class MethodListenerDiscovery
16
{
17
    use FilesInDirectoryExtracter;
18
19
    protected $eventToListenerMap = [];
20
    /**
21
     * @var MessageClassDetector
22
     */
23
    private $messageClassDetector;
24
25
    /** @var ListenerClassValidator */
26
    private $classValidator;
27
28
    /** @var ListenerMethod[] */
29
    private $allEventsListeners = [];
30
    /**
31
     * @var ClassSorter
32
     */
33
    private $classSorter;
34
    /**
35
     * @var PhpClassInFileInspector
36
     */
37
    private $phpClassInFileInspector;
38
39 1 View Code Duplication
    public function __construct(
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...
40
        MessageClassDetector $messageClassDetector,
41
        ListenerClassValidator $classValidator,
42
        ClassSorter $classSorter,
43
        PhpClassInFileInspector $phpClassInFileInspector = null
44
    )
45
    {
46 1
        $this->messageClassDetector = $messageClassDetector;
47 1
        $this->classValidator = $classValidator;
48 1
        $this->classSorter = $classSorter;
49 1
        $this->phpClassInFileInspector = $phpClassInFileInspector ?? new PhpClassInFileInspector();
50 1
    }
51
52
53 1
    public function discoverListeners($directory)
54
    {
55 1
        $files = $this->getFilesInDirectory($directory);
56
57 1
        $files = $this->filterFiles($files);
58
59 1
        foreach ($files as $file) {
60 1
            $fullFilePath = $file;
61
62 1
            $listenerEntries = $this->extractListenerMethodsFromFile($fullFilePath);
63
64 1
            if ($listenerEntries) {
65 1
                foreach ($listenerEntries as $entry) {
66 1
                    $this->addListenerToEvents($entry);
67
                }
68
            }
69
        }
70
71 1
        $this->allEventsListeners = $this->sortListeners($this->allEventsListeners);
72
73 1
        foreach ($this->eventToListenerMap as $eventClass => $listeners) {
74 1
            $this->eventToListenerMap[$eventClass] = $this->sortListeners($listeners);
75
        }
76 1
    }
77
78
    /**
79
     * @param string $filePath
80
     * @return bool
81
     */
82 1
    protected function isListenerFileName($filePath)
83
    {
84 1
        return preg_match('#\.php$#ims', $filePath);
85
    }
86
87
    /**
88
     * @param $fullFilePath
89
     * @return bool|\Gica\CodeAnalysis\MethodListenerDiscovery\ListenerMethod[]
90
     */
91 1
    protected function extractListenerMethodsFromFile($fullFilePath)
92
    {
93 1
        $fqn = $this->phpClassInFileInspector->getFullyQualifiedClassName($fullFilePath);
94
95 1
        if (!$fqn) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fqn of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
96 1
            return false;
97
        }
98
99 1
        return $this->findListenerMethodsInClass($fqn);
100
    }
101
102
    protected function readFile($fullFilePath)
103
    {
104
        return file_get_contents($fullFilePath);
105
    }
106
107 1
    protected function addListenerToEvents(ListenerMethod $listener)
108
    {
109 1
        $this->eventToListenerMap[$listener->getEventClassName()][] = $listener;
110 1
        $this->allEventsListeners[] = $listener;
111 1
    }
112
113
    /**
114
     * @param ListenerMethod[] $listeners
115
     * @return ListenerMethod[]
116
     */
117 1
    private function sortListeners($listeners)
118
    {
119
        usort($listeners, function (ListenerMethod $a, ListenerMethod $b) {
120 1
            return $this->classSorter->__invoke($a->getClass(), $b->getClass());
121 1
        });
122
123 1
        return $listeners;
124
    }
125
126 1
    public function getEventToListenerMap()
127
    {
128 1
        return $this->eventToListenerMap;
129
    }
130
131
    /**
132
     * @return ListenerMethod[]
133
     */
134 1
    public function getAllEventsListeners(): array
135
    {
136 1
        return $this->allEventsListeners;
137
    }
138
139
    protected function filterFiles(array $files)
140
    {
141 1
        return array_filter($files, function ($file) {
142 1
            return $this->isListenerFileName($file);
143 1
        });
144
    }
145
146
    /**
147
     * @param $className
148
     * @return ListenerMethod[]
149
     */
150 1
    private function findListenerMethodsInClass($className)
151
    {
152 1
        $result = [];
153
154 1
        $reflectionClass = new \ReflectionClass($className);
155
156 1
        if (!$this->classValidator->isClassAccepted($reflectionClass)) {
157 1
            return [];
158
        }
159
160 1
        foreach ($reflectionClass->getMethods() as $reflectionMethod) {
161
162 1
            if (!$this->isValidListenerMethod($reflectionMethod)) {
163 1
                continue;
164
            }
165
166 1
            $eventClass = $this->getMessageClassFromMethod($reflectionMethod);
167
168 1
            if ($eventClass) {
169 1
                $result[] = new ListenerMethod($reflectionClass, $reflectionMethod->name, $eventClass);
170
            }
171
        }
172
173 1
        return $result;
174
    }
175
176 1
    private function getMessageClassFromMethod(\ReflectionMethod $reflectionMethod)
177
    {
178 1
        if (!$this->isMethodAcccepted($reflectionMethod)) {
179 1
            return false;
180
        }
181
182 1
        foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
183 1
            $typeHintedClass = $reflectionParameter->getClass();
184
185 1
            if ($typeHintedClass) {
186 1
                if ($this->isOurMessageClass($typeHintedClass)) {
187 1
                    return $typeHintedClass->getName();
188
                }
189
            }
190
        }
191
192 1
        return false;
193
    }
194
195 1
    private function isValidListenerMethod(\ReflectionMethod $reflectionMethod)
196
    {
197 1
        if ($reflectionMethod->getNumberOfParameters() == 0)
198 1
            return false;
199
200 1
        return true;
201
    }
202
203 1
    private function isOurMessageClass(\ReflectionClass $typeHintedClass)
204
    {
205 1
        return $this->messageClassDetector->isMessageClass($typeHintedClass);
206
    }
207
208 1
    private function isMethodAcccepted(\ReflectionMethod $reflectionMethod)
209
    {
210 1
        return $this->messageClassDetector->isMethodAccepted($reflectionMethod);
211
    }
212
}