Completed
Push — master ( 0c0f71...37b42b )
by Gabriel
02:29
created

Runner::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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|TaskInterface $task
69
     * @param callable             $callback
70
     */
71
    public function __set(string $task, callable $callback)
72
    {
73
        $this->task($task, $callback);
74
    }
75
76
    /**
77
     * Organize tasks and groups order
78
     * If this method is executed and a task and a group is not in this list, it won't be executed
79
     *
80
     * @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...
81
     */
82
    public function order(string ...$names)
83
    {
84
        foreach ($names as $key => $name) {
85
            if ($this->tasks->containsKey($name)) {
86
                $names[$key] = 'task::' . $name;
87
            } 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...
88
                $names[$key] = 'group::' . $name;
89
            } else {
90
                throw new \Exception('\'' . $name . '\' is not registred as task or group.');
91
            }
92
        }
93
94
        $this->order = $names;
95
    }
96
97
    /**
98
     * Returns all registred tasks
99
     *
100
     * @return TasksCollection
101
     */
102
    public function getTasks() : TasksCollection
103
    {
104
        return $this->tasks;
105
    }
106
107
    public function getGroups() : GroupsCollection
108
    {
109
        return $this->groups;
110
    }
111
112
    public function getOrder() : array
113
    {
114
        return $this->order;
115
    }
116
117
    /**
118
     * Runs all tasks
119
     */
120
    public function run()
121
    {
122
        $all = $this->order;
123
124
        foreach ($all as $el) {
125
            $data = $this->getFromOrderData($el);
126
127
            switch ($data['type']) {
128
                case 'group':
129
                    $this->runGroup($data['name']);
130
                    break;
131
                case 'task':
132
                    $this->runTask($data['name']);
133
                    break;
134
            }
135
        }
136
    }
137
138
    /**
139
     * Runs one single task
140
     *
141
     * @param string|TaskInterface $task
142
     */
143
    public function runTask($task)
144
    {
145
        if (!is_string($task) && !$task instanceof TaskInterface) {
146
            throw new \Exception('Invalid task type: ' + gettype($task));
147
        }
148
149
        if (is_string($task)) {
150
            if (!isset($this->tasks[$task])) {
151
                throw new \Exception('\'' . $task . '\' is not a registred task.');
152
            }
153
        }
154
155
        $task = $task instanceof TaskInterface ? $task : $this->tasks[$task];
156
        $cb = $task->getCallback();
157
        $cb();
158
    }
159
160
    /**
161
     * Runs a group of tasks
162
     *
163
     * @param string|GroupInterface $group
164
     */
165
    public function runGroup($group)
166
    {
167
        $group = $group instanceof GroupInterface ? $group : $this->groups[$group];
168
        $tasks = $group->getTasks();
169
170
        foreach ($tasks as $task) {
171
            $this->runTask($task);
172
        }
173
    }
174
175 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...
176
    {
177
        $parts = explode('::', $name);
178
        $type = $parts[0];
179
        unset($parts[0]);
180
181
        return ['type' => $type, 'name' => implode('::', $parts)];
182
    }
183
}