Completed
Pull Request — master (#1)
by Jim
08:57
created

Discovery::getTasks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
4
namespace Jarobe\TaskRunner\Hydrator;
5
6
use Doctrine\Common\Annotations\Reader;
7
use Jarobe\TaskRunner\TaskType\TaskTypeInterface;
8
use Jarobe\TaskRUnner\Annotation\TaskType;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Finder\SplFileInfo;
11
12
13
class Discovery implements DiscoveryInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $namespace;
19
20
    /**
21
     * @var string
22
     */
23
    private $directory;
24
25
    /**
26
     * @var Reflector
27
     */
28
    private $reflector;
29
30
    /**
31
     * The Kernel root directory
32
     * @var string
33
     */
34
    private $rootDir;
35
36
    /**
37
     * @var array
38
     */
39
    private $tasks = [];
40
41
42
    /**
43
     * WorkerDiscovery constructor.
44
     *
45
     * @param $namespace
46
     *   The namespace of the workers
47
     * @param $directory
48
     *   The directory of the workers
49
     * @param $rootDir
50
     * @param Reflector $reflector
51
     */
52
    public function __construct($namespace, $directory, $rootDir, Reflector $reflector)
53
    {
54
        $this->namespace = $namespace;
55
        $this->reflector = $reflector;
56
        $this->directory = $directory;
57
        $this->rootDir = $rootDir;
58
    }
59
60
    /**
61
     * Returns all the tasks
62
     * @return array
63
     */
64
    public function getTasks()
65
    {
66
        if (!$this->tasks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->tasks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
67
            $this->discoverTaskTypes();
68
        }
69
70
        return $this->tasks;
71
    }
72
73
    /**
74
     * Discovers tasks
75
     */
76
    private function discoverTaskTypes()
77
    {
78
        $path = $this->rootDir . '/../src/' . $this->directory;
79
        $finder = new Finder();
80
        $finder->files()->in($path);
81
82
        /** @var SplFileInfo $file */
83
        foreach ($finder as $file) {
84
            /** @var TaskTypeInterface $class */
85
            $class = $this->namespace . '\\' . $file->getBasename('.php');
86
87
            $name = $this->reflector->getNameForClass($class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $name is correct as $this->reflector->getNameForClass($class) (which targets Jarobe\TaskRunner\Hydrat...ctor::getNameForClass()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
            if (!$name) {
89
                continue;
90
            }
91
            /** @var TaskType $annotation */
92
            $this->tasks[$name] = $class;
93
        }
94
    }
95
}