Passed
Push — master ( 24f966...088d24 )
by Gabriel
22:29 queued 07:11
created

DiscoverEvents::getListenerEvents()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 21
ccs 0
cts 0
cp 0
rs 8.8333
cc 7
nc 15
nop 1
crap 56
1
<?php
2
3
namespace ByTIC\EventDispatcher\ListenerProviders\Discover;
4
5
use Nip\Utility\Oop;
6
use Nip\Utility\Str;
7
use ReflectionClass;
8
use Roave\BetterReflection\BetterReflection;
0 ignored issues
show
Bug introduced by
The type Roave\BetterReflection\BetterReflection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Roave\BetterReflection\Reflector\ClassReflector;
0 ignored issues
show
Bug introduced by
The type Roave\BetterReflection\Reflector\ClassReflector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Roave\BetterReflection\Reflector\DefaultReflector;
0 ignored issues
show
Bug introduced by
The type Roave\BetterReflection\Reflector\DefaultReflector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;
0 ignored issues
show
Bug introduced by
The type Roave\BetterReflection\S...irectoriesSourceLocator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\Finder\SplFileInfo;
14
15
/**
16
 * Class DiscoverEvents
17
 * @package ByTIC\EventDispatcher\ListenerProviders\Discover
18
 *
19
 * @internal
20
 */
21
class DiscoverEvents
22
{
23 2
    /**
24
     * Get all of the events and listeners by searching the given listener directory.
25 2
     *
26 2
     * @param array $listenerPaths
27
     * @return array
28
     */
29
    public static function within($listenerPaths): array
30
    {
31
        return static::getListenerEvents(
32
            static::getListenerClasses($listenerPaths)
33
        );
34 2
    }
35
36 2
    /**
37 2
     * @param array $paths
38 2
     * @return array
39 2
     */
40 2
    protected static function getListenerClasses($paths): iterable
41
    {
42
        $paths = is_array($paths) ? $paths : [$paths];
0 ignored issues
show
introduced by
The condition is_array($paths) is always true.
Loading history...
43
        $files = (new Finder)->files()->in($paths);
44
45
        foreach ($files as $file) {
46
            yield from static::classFromFile($file);
0 ignored issues
show
Bug Best Practice introduced by
The expression YieldFromNode returns the type Generator which is incompatible with the documented return type array.
Loading history...
47 2
        }
48
49 2
//        $astLocator = (new BetterReflection())->astLocator();
50 2
//        $directoriesSourceLocator = new DirectoriesSourceLocator($paths, $astLocator);
51
//        if (class_exists(\Roave\BetterReflection\Reflector\ClassReflector::class)) {
52 2
//            $reflector = new \Roave\BetterReflection\Reflector\ClassReflector($directoriesSourceLocator);
53 2
//            return $reflector->getAllClasses();
54 2
//        }
55
//
56
//        $reflector = new DefaultReflector($directoriesSourceLocator);
57 2
//        return $reflector->reflectAllClasses();
58 2
    }
59 2
60
    /**
61 2
     * Extract the class name from the given file path.
62 2
     *
63
     * @param  \SplFileInfo  $file
64
     * @param  string  $basePath
65
     * @return array
66
     */
67 2
    protected static function classFromFile(SplFileInfo $file)
68
    {
69
        return Oop::classesInFile($file);
0 ignored issues
show
Bug introduced by
The method classesInFile() does not exist on Nip\Utility\Oop. ( Ignorable by Annotation )

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

69
        return Oop::/** @scrutinizer ignore-call */ classesInFile($file);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
    }
71
72
    /**
73
     * @param \Roave\BetterReflection\Reflection\ReflectionClass[] $listeners
74
     * @return array
75
     */
76
    protected static function getListenerEvents(iterable $listeners): array
77
    {
78
        $listenerEvents = [];
79
        foreach ($listeners as $listener) {
80
            try {
81
                $listener = new ReflectionClass($listener);
82
                foreach ($listener->getMethods() as $method) {
83
                    if (!$method->isPublic()) {
84
                        continue;
85
                    }
86
                    if (!Str::is('handle*', $method->name) ||
87
                        !isset($method->getParameters()[0])) {
88
                        continue;
89
                    }
90
                    $eventName = $method->getParameters()[0]->getClass()->getName();
91
                    $listenerEvents[$eventName][] = [$listener->getName(), $method->getName()];
92
                }
93
            } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
94
            }
95
        }
96
        return array_filter($listenerEvents);
97
    }
98
}
99