| Conditions | 9 |
| Paths | 56 |
| Total Lines | 61 |
| Code Lines | 34 |
| 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 |
||
| 77 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 78 | { |
||
| 79 | $collection = $this->collection; |
||
| 80 | |||
| 81 | $searchPath = $input->getArgument('path'); |
||
| 82 | |||
| 83 | if (substr($input->getArgument('path'), -1, 1) === '/') { |
||
| 84 | $searchPath .= '*'; |
||
| 85 | } |
||
| 86 | |||
| 87 | $collection->addFieldToFilter('path', array( |
||
| 88 | 'like' => str_replace('*', '%', $searchPath), |
||
| 89 | )); |
||
| 90 | |||
| 91 | if ($scope = $input->getOption('scope')) { |
||
| 92 | $collection->addFieldToFilter('scope', array('eq' => $scope)); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($scopeId = $input->getOption('scope-id')) { |
||
| 96 | $collection->addFieldToFilter( |
||
| 97 | 'scope_id', |
||
| 98 | array('eq' => $scopeId) |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | |||
| 102 | $collection->addOrder('path', 'ASC'); |
||
| 103 | |||
| 104 | // sort according to the config overwrite order |
||
| 105 | // trick to force order default -> (f)website -> store , because f comes after d and before s |
||
| 106 | $collection->addOrder('REPLACE(scope, "website", "fwebsite")', 'ASC'); |
||
| 107 | |||
| 108 | $collection->addOrder('scope_id', 'ASC'); |
||
| 109 | |||
| 110 | if ($collection->count() == 0) { |
||
| 111 | $output->writeln(sprintf("Couldn't find a config value for \"%s\"", $input->getArgument('path'))); |
||
| 112 | |||
| 113 | return; |
||
| 114 | } |
||
| 115 | |||
| 116 | foreach ($collection as $item) { |
||
| 117 | $table[] = array( |
||
|
|
|||
| 118 | 'path' => $item->getPath(), |
||
| 119 | 'scope' => $item->getScope(), |
||
| 120 | 'scope_id' => $item->getScopeId(), |
||
| 121 | 'value' => $this->_formatValue( |
||
| 122 | $item->getValue(), |
||
| 123 | $input->getOption('decrypt') ? 'decrypt' : '' |
||
| 124 | ), |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 128 | ksort($table); |
||
| 129 | |||
| 130 | if ($input->getOption('update-script')) { |
||
| 131 | $this->renderAsUpdateScript($output, $table); |
||
| 132 | } elseif ($input->getOption('magerun-script')) { |
||
| 133 | $this->renderAsMagerunScript($output, $table); |
||
| 134 | } else { |
||
| 135 | $this->renderAsTable($output, $table, $input->getOption('format')); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 247 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.