RunCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 9.2 %

Coupling/Cohesion

Components 1
Dependencies 7
Metric Value
wmc 14
lcom 1
cbo 7
dl 8
loc 87
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 9 1
C execute() 0 43 8
A executeGroup() 0 12 3
A getFromOrderData() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Junty
4
 *
5
 * @author Gabriel Jacinto aka. GabrielJMJ <[email protected]>
6
 * @license MIT License
7
 */
8
 
9
namespace Junty\TaskRunner\Console\Command;
10
11
use Junty\TaskRunner\Runner\RunnerInterface;
12
use Junty\TaskRunner\Task\{GroupInterface, Task};
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\{InputArgument, InputInterface};
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class RunCommand extends Command
18
{
19
    private $runner;
20
21
    public function __construct(RunnerInterface $runner, $name = null)
22
    {
23
        parent::__construct($name);
24
25
        $this->runner = $runner;
26
    }
27
28
    protected function configure()
29
    {
30
        $this->setName('run')
31
            ->setDescription('Run tasks')
32
            ->addArgument(
33
                'group_or_task',
34
                InputArgument::OPTIONAL
35
            );
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        if ($input->hasArgument('group_or_task') && $task = $input->getArgument('group_or_task') !== null) {
41
            $el = $input->getArgument('group_or_task');
42
43
            if ($this->runner->getGroups()->containsKey($el)) {
44
                $group = $this->runner->getGroups()->get($el);
45
                $output->writeln('Executing group \'' . $group->getName() . '\'');
46
47
                $this->executeGroup($group, $output);
48
            } elseif ($this->runner->getTasks()->containsKey($el)) {
49
                $task = $this->runner->getTasks()->get($el);
50
                $output->writeln('Executing task \'' . $task->getName() . '\'');
51
52
                $this->runner->runTask($task);
53
            }
54
        } else {
55
            $els = $this->runner->getOrder();
56
57
            $output->writeln('Executing tasks');
58
            
59
            foreach ($els as $el) {
60
                $data = $this->getFromOrderData($el);
61
62
                $output->writeln('Executing ' . $data['type'] . ' \'' . $data['name'] . '\'');
63
64
                try {
65
                    if ($data['type'] == 'group') {
66
                        $group = $this->runner->getGroups()->toArray()[$data['name']];
67
                        
68
                        $this->executeGroup($group, $output);
69
                    } else {
70
                        $this->runner->runTask($data['name']);
71
                    }
72
                } catch (\Exception $e) {
73
                    $output->writeln('Error on ' . $data['type'] . ' \'' . $data['name'] . '\': ' . $e->getMessage());
74
                }
75
            }
76
        }
77
78
        $time = round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 10000);
79
        $output->writeln('Finished! Time: ' . $time . 'ms');
80
    }
81
82
    private function executeGroup(GroupInterface $group, OutputInterface $output)
83
    {
84
        foreach ($group->getTasks() as $task) {
85
            $output->writeln('--Executing task \'' . $task->getName() . '\'');
86
87
            try {
88
                $this->runner->runTask($task);
89
            } catch (\Exception $e) {
90
                $output->writeln('--Error on task \'' . $task->getName() . '\': ' . $e->getMessage());
91
            }
92
        }
93
    }
94
95 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...
96
    {
97
        $parts = explode('::', $name);
98
        $type = $parts[0];
99
        unset($parts[0]);
100
101
        return ['type' => $type, 'name' => implode('::', $parts)];
102
    }
103
}