Runner::task()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %
Metric Value
dl 11
loc 11
rs 9.4285
cc 3
eloc 6
nc 4
nop 2
1
<?php
2
/**
3
 * Junty
4
 *
5
 * @author Gabriel Jacinto aka. GabrielJMJ <[email protected]>
6
 * @license MIT License
7
 */
8
 
9
namespace Junty\TaskRunner\Runner;
10
11
use Junty\TaskRunner\Runner\RunnerInterface;
12
use Junty\TaskRunner\Task\{
13
    Task,
14
    TaskInterface,
15
    Group,
16
    GroupInterface,
17
    TasksCollection,
18
    GroupsCollection
19
};
20
21
class Runner implements RunnerInterface
22
{
23
    private $tasks;
24
25
    private $groups = [];
26
27
    private $order;
28
29
    public function __construct()
30
    {
31
        $this->tasks = new TasksCollection();
32
        $this->groups = new GroupsCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Junty\TaskRunner\Task\GroupsCollection() of type object<Junty\TaskRunner\Task\GroupsCollection> is incompatible with the declared type array of property $groups.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35 View Code Duplication
    public function group($group, callable $tasks = null)
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...
36
    {
37
        $_name = $group instanceof GroupInterface ? $group->getName() : $group;
38
39
        if ($this->tasks->containsKey($_name)) {
40
            throw new \Exception('Is not possible to register a group and a task with the same name.');
41
        }
42
43
        $this->groups->set($group, $tasks);
0 ignored issues
show
Bug introduced by
The method set cannot be called on $this->groups (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
44
        $this->order[] = 'group::' . $_name;
45
    }
46
47
    /**
48
     * Registres a task
49
     *
50
     * @param string|TaskInterface $task
51
     * @param callable             $callback
52
     */
53 View Code Duplication
    public function task($task, callable $callback = null)
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...
54
    {
55
        $_name = $task instanceof TaskInterface ? $task->getName() : $task;
56
57
        if ($this->groups->containsKey($_name)) {
0 ignored issues
show
Bug introduced by
The method containsKey cannot be called on $this->groups (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
58
            throw new \Exception('Is not possible to register a group and a task with the same name.');
59
        }
60
61
        $this->tasks->set($task, $callback);
0 ignored issues
show
Bug introduced by
It seems like $callback defined by parameter $callback on line 53 can also be of type null; however, Junty\TaskRunner\Task\TasksCollection::set() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
62
        $this->order[] = 'task::' . $_name;
63
    }
64
65
    /**
66
     * Allows create a task setting a property
67
     *
68
     * @param string                 $task
69
     * @param callable|TaskInterface $callback
70
     */
71
    public function __set(string $task, $callback)
72
    {
73
        if(!is_callable($callback) && !$callback instanceof TaskInterface) {
74
            throw new \InvalidArgumentException('Task must be callable or instanceof TaskInterface.');
75
        }
76
77
        $this->task($task, $callback);
78
    }
79
80
    /**
81
     * Organize tasks and groups order
82
     * If this method is executed and a task and a group is not in this list, it won't be executed
83
     *
84
     * @param string-variadic $tasks
0 ignored issues
show
Documentation introduced by
The doc-type string-variadic could not be parsed: Unknown type name "string-variadic" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $tasks. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
85
     */
86
    public function order(string ...$names)
87
    {
88
        foreach ($names as $key => $name) {
89
            if ($this->tasks->containsKey($name)) {
90
                $names[$key] = 'task::' . $name;
91
            } elseif ($this->groups->containsKey($name)) {
0 ignored issues
show
Bug introduced by
The method containsKey cannot be called on $this->groups (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
92
                $names[$key] = 'group::' . $name;
93
            } else {
94
                throw new \Exception('\'' . $name . '\' is not registred as task or group.');
95
            }
96
        }
97
98
        $this->order = $names;
99
    }
100
101
    /**
102
     * Returns all registred tasks
103
     *
104
     * @return TasksCollection
105
     */
106
    public function getTasks() : TasksCollection
107
    {
108
        return $this->tasks;
109
    }
110
111
    public function getGroups() : GroupsCollection
112
    {
113
        return $this->groups;
114
    }
115
116
    public function getOrder() : array
117
    {
118
        return $this->order;
119
    }
120
121
    /**
122
     * Runs all tasks
123
     */
124
    public function run()
125
    {
126
        $all = $this->order;
127
128
        foreach ($all as $el) {
129
            $data = $this->getFromOrderData($el);
130
131
            switch ($data['type']) {
132
                case 'group':
133
                    $this->runGroup($data['name']);
134
                    break;
135
                case 'task':
136
                    $this->runTask($data['name']);
137
                    break;
138
            }
139
        }
140
    }
141
142
    /**
143
     * Runs one single task
144
     *
145
     * @param string|TaskInterface $task
146
     */
147
    public function runTask($task)
148
    {
149
        if (!is_string($task) && !$task instanceof TaskInterface) {
150
            throw new \Exception('Invalid task type: ' + gettype($task));
151
        }
152
153
        if (is_string($task)) {
154
            if (!isset($this->tasks[$task])) {
155
                throw new \Exception('\'' . $task . '\' is not a registred task.');
156
            }
157
        }
158
159
        $task = $task instanceof TaskInterface ? $task : $this->tasks[$task];
160
        $cb = $task->getCallback();
161
        $cb();
162
    }
163
164
    /**
165
     * Runs a group of tasks
166
     *
167
     * @param string|GroupInterface $group
168
     */
169
    public function runGroup($group)
170
    {
171
        $group = $group instanceof GroupInterface ? $group : $this->groups[$group];
172
        $tasks = $group->getTasks();
173
174
        foreach ($tasks as $task) {
175
            $this->runTask($task);
176
        }
177
    }
178
179 View Code Duplication
    private function getFromOrderData($name)
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...
180
    {
181
        $parts = explode('::', $name);
182
        $type = $parts[0];
183
        unset($parts[0]);
184
185
        return ['type' => $type, 'name' => implode('::', $parts)];
186
    }
187
}