Conditions | 13 |
Paths | 168 |
Total Lines | 40 |
Code Lines | 25 |
Lines | 18 |
Ratio | 45 % |
Tests | 0 |
CRAP Score | 182 |
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 |
||
49 | protected function execute(InputInterface $input, OutputInterface $output) |
||
50 | { |
||
51 | foreach ($this->createOrder as $option) { |
||
52 | if ($input->getOption($option)) { |
||
53 | $create[] = $option; |
||
|
|||
54 | } |
||
55 | } |
||
56 | |||
57 | // Default to the full creation order if no options were specified |
||
58 | $create = empty($create) ? $this->createOrder : $create; |
||
59 | |||
60 | $class = $input->getOption('class'); |
||
61 | |||
62 | $timeout = $input->getOption('timeout'); |
||
63 | $this->timeout = isset($timeout) ? (int) $timeout : null; |
||
64 | |||
65 | $sm = $this->getSchemaManager(); |
||
66 | $isErrored = false; |
||
67 | |||
68 | View Code Duplication | foreach ($create as $option) { |
|
69 | try { |
||
70 | if (isset($class)) { |
||
71 | $this->{'processDocument' . ucfirst($option)}($sm, $class); |
||
72 | } else { |
||
73 | $this->{'process' . ucfirst($option)}($sm); |
||
74 | } |
||
75 | $output->writeln(sprintf( |
||
76 | 'Created <comment>%s%s</comment> for <info>%s</info>', |
||
77 | $option, |
||
78 | (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
||
79 | (isset($class) ? $class : 'all classes') |
||
80 | )); |
||
81 | } catch (\Exception $e) { |
||
82 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
83 | $isErrored = true; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | return ($isErrored) ? 255 : 0; |
||
88 | } |
||
89 | |||
130 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.