Completed
Push — master ( 68f30c...a95a96 )
by Christian
02:25
created

Run   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 98
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 10 1
C execute() 0 63 10
1
<?php
2
3
namespace uuf6429\ElderBrother\Console\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use uuf6429\ElderBrother\Config;
11
use uuf6429\ElderBrother\Exception\RecoverableException;
12
13
class Run extends Command
14
{
15
    /**
16
     * @var Config
17
     */
18
    protected $config;
19
20
    /**
21
     * @param Config $config
22
     */
23
    public function __construct(Config $config)
24
    {
25
        parent::__construct(null);
26
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('run')
37
            ->setDescription('Runs configured actions for an event.')
38
            ->setHelp('This command runs all actions defined in configuration for the specified event.')
39
            ->addOption('event', 'e', InputOption::VALUE_REQUIRED, 'The event whose actions will be run.')
40
            ->addOption('no-progress', null, InputOption::VALUE_NONE, 'Disables progress bar.')
41
        ;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        if ($output->isDebug()) {
50
            $output->write(['Running from: <info>', PROJECT_ROOT, '</info>']);
51
            $output->writeln('');
52
        }
53
54
        if (extension_loaded('xdebug')) {
55
            $output->writeln(
56
                sprintf(
57
                    '<bg=yellow;fg=black;>%s</>',
58
                    'Xdebug is enabled; performance will likely be affected.'
59
                )
60
            );
61
        }
62
63
        $event = $input->getOption('event');
64
        $withProgress = !$input->hasOption('no-progress');
65
        $actions = $this->config->get($event);
66
67
        if (!empty($actions)) {
68
            if ($withProgress) {
69
                // See https://github.com/symfony/symfony/pull/10356 for multiple bars
70
                $progress = new ProgressBar($output, count($actions));
71
                $progress->start();
72
                $output->write("\n");
73
            }
74
75
            foreach ($actions as $action) {
76
                if ($withProgress) {
77
                    $output->write("\033[1A");
78
                    $progress->setMessage('Running "' . $action->getName() . '".');
0 ignored issues
show
Bug introduced by
The variable $progress does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
79
                    $progress->advance();
80
                    $output->write("\n");
81
                } else {
82
                    $output->writeln('Running <info>"' . $action->getName() . '"</info>...');
83
                }
84
85
                try {
86
                    $action->execute($input, $output);
87
                } catch (\Exception $ex) {
88
                    $this->getApplication()->renderException($ex, $output);
89
                    if (!($ex instanceof RecoverableException)) {
90
                        return 1;
91
                    }
92
                }
93
            }
94
95
            if ($withProgress) {
96
                $progress->finish();
97
            }
98
            $output->writeln('Done.');
99
        } else {
100
            $output->writeln(
101
                sprintf(
102
                    '<bg=yellow;fg=black;>%s</>',
103
                    'No actions have been set up yet!'
104
                )
105
            );
106
        }
107
108
        return 0;
109
    }
110
}
111