Conditions | 10 |
Paths | 36 |
Total Lines | 63 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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() . '".'); |
||
|
|||
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 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: