Conditions | 12 |
Paths | 84 |
Total Lines | 36 |
Code Lines | 23 |
Lines | 18 |
Ratio | 50 % |
Tests | 0 |
CRAP Score | 156 |
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 |
||
46 | protected function execute(InputInterface $input, OutputInterface $output) |
||
47 | { |
||
48 | foreach ($this->dropOrder as $option) { |
||
49 | if ($input->getOption($option)) { |
||
50 | $drop[] = $option; |
||
|
|||
51 | } |
||
52 | } |
||
53 | |||
54 | // Default to the full drop order if no options were specified |
||
55 | $drop = empty($drop) ? $this->dropOrder : $drop; |
||
56 | |||
57 | $class = $input->getOption('class'); |
||
58 | $sm = $this->getSchemaManager(); |
||
59 | $isErrored = false; |
||
60 | |||
61 | View Code Duplication | foreach ($drop as $option) { |
|
62 | try { |
||
63 | if (isset($class)) { |
||
64 | $this->{'processDocument' . ucfirst($option)}($sm, $class); |
||
65 | } else { |
||
66 | $this->{'process' . ucfirst($option)}($sm); |
||
67 | } |
||
68 | $output->writeln(sprintf( |
||
69 | 'Dropped <comment>%s%s</comment> for <info>%s</info>', |
||
70 | $option, |
||
71 | (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
||
72 | (isset($class) ? $class : 'all classes') |
||
73 | )); |
||
74 | } catch (\Exception $e) { |
||
75 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
76 | $isErrored = true; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | return ($isErrored) ? 255 : 0; |
||
81 | } |
||
82 | |||
113 |
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.