| Conditions | 10 |
| Paths | 57 |
| Total Lines | 67 |
| Code Lines | 37 |
| 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 |
||
| 65 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 66 | { |
||
| 67 | $this->detectMagento($output, true); |
||
| 68 | if (!$this->initMagento()) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | /* @var $collection \Mage_Core_Model_Resource_Db_Collection_Abstract */ |
||
| 73 | $collection = $this->_getConfigDataModel()->getCollection(); |
||
| 74 | |||
| 75 | $searchPath = $input->getArgument('path'); |
||
| 76 | |||
| 77 | if (substr($input->getArgument('path'), -1, 1) === '/') { |
||
| 78 | $searchPath .= '*'; |
||
| 79 | } |
||
| 80 | |||
| 81 | $collection->addFieldToFilter('path', array( |
||
| 82 | 'like' => str_replace('*', '%', $searchPath), |
||
| 83 | )); |
||
| 84 | |||
| 85 | if ($scope = $input->getOption('scope')) { |
||
| 86 | $collection->addFieldToFilter('scope', array('eq' => $scope)); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($scopeId = $input->getOption('scope-id')) { |
||
| 90 | $collection->addFieldToFilter( |
||
| 91 | 'scope_id', |
||
| 92 | array('eq' => $scopeId) |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | $collection->addOrder('path', 'ASC'); |
||
| 97 | |||
| 98 | // sort according to the config overwrite order |
||
| 99 | // trick to force order default -> (f)website -> store , because f comes after d and before s |
||
| 100 | $collection->addOrder('REPLACE(scope, "website", "fwebsite")', 'ASC'); |
||
| 101 | |||
| 102 | $collection->addOrder('scope_id', 'ASC'); |
||
| 103 | |||
| 104 | if ($collection->count() == 0) { |
||
| 105 | $output->writeln(sprintf("Couldn't find a config value for \"%s\"", $input->getArgument('path'))); |
||
| 106 | |||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | foreach ($collection as $item) { |
||
| 111 | $table[] = array( |
||
|
|
|||
| 112 | 'path' => $item->getPath(), |
||
| 113 | 'scope' => $item->getScope(), |
||
| 114 | 'scope_id' => $item->getScopeId(), |
||
| 115 | 'value' => $this->_formatValue( |
||
| 116 | $item->getValue(), |
||
| 117 | $input->getOption('decrypt') ? 'decrypt' : false |
||
| 118 | ), |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | ksort($table); |
||
| 123 | |||
| 124 | if ($input->getOption('update-script')) { |
||
| 125 | $this->renderAsUpdateScript($output, $table); |
||
| 126 | } elseif ($input->getOption('magerun-script')) { |
||
| 127 | $this->renderAsMagerunScript($output, $table); |
||
| 128 | } else { |
||
| 129 | $this->renderAsTable($output, $table, $input->getOption('format')); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 241 |
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.