| Conditions | 12 |
| Paths | 240 |
| Total Lines | 65 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 |
||
| 100 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 101 | { |
||
| 102 | $this->detectMagento($output); |
||
| 103 | if ($this->initMagento()) { |
||
| 104 | $runOnStoreView = false; |
||
| 105 | if ($this->scope == self::SCOPE_STORE_VIEW |
||
| 106 | || ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL && !$input->getOption('global')) |
||
| 107 | ) { |
||
| 108 | $runOnStoreView = true; |
||
| 109 | } |
||
| 110 | |||
| 111 | if ($runOnStoreView) { |
||
| 112 | $store = $this->_initStore($input, $output); |
||
| 113 | } else { |
||
| 114 | $storeManager = $this->getObjectManager()->get('Magento\Store\Model\StoreManagerInterface'); |
||
| 115 | /* @var $storeManager \Magento\Store\Model\StoreManagerInterface */ |
||
| 116 | $store = $storeManager->getStore(\Magento\Store\Model\Store::DEFAULT_STORE_ID); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($input->getOption('on')) { |
||
| 121 | $isFalse = true; |
||
| 122 | } elseif ($input->getOption('off')) { |
||
| 123 | $isFalse = false; |
||
| 124 | } else { |
||
| 125 | $scopeConfig = $this->getObjectManager()->get('\Magento\Framework\App\Config\ScopeConfigInterface'); |
||
| 126 | /* @var $scopeConfig \Magento\Framework\App\Config\ScopeConfigInterface */ |
||
| 127 | $isFalse = !$scopeConfig->isSetFlag( |
||
| 128 | $this->configPath, |
||
| 129 | \Magento\Store\Model\ScopeInterface::SCOPE_STORE, |
||
| 130 | $store->getCode() |
||
|
|
|||
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | $this->_beforeSave($store, $isFalse); |
||
| 135 | |||
| 136 | |||
| 137 | if ($store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID) { |
||
| 138 | $scope = 'default'; // @TODO Constant was removed in Magento2 ? |
||
| 139 | } else { |
||
| 140 | $scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES; |
||
| 141 | } |
||
| 142 | |||
| 143 | $configSetCommands = [ |
||
| 144 | 'command' => 'config:set', |
||
| 145 | 'path' => $this->configPath, |
||
| 146 | 'value' => $isFalse ? 1 : 0, |
||
| 147 | '--scope' => $scope, |
||
| 148 | '--scope-id' => $store->getId(), |
||
| 149 | ]; |
||
| 150 | |||
| 151 | $input = new ArrayInput($configSetCommands); |
||
| 152 | $this->getApplication()->setAutoExit(false); |
||
| 153 | $this->getApplication()->run($input, new NullOutput()); |
||
| 154 | |||
| 155 | $comment = '<comment>' . $this->toggleComment . '</comment> ' |
||
| 156 | . '<info>' . (!$isFalse ? $this->falseName : $this->trueName) . '</info>' |
||
| 157 | . ($runOnStoreView ? ' <comment>for store</comment> <info>' . $store->getCode() . '</info>' : ''); |
||
| 158 | $output->writeln($comment); |
||
| 159 | |||
| 160 | $this->_afterSave($store, $isFalse); |
||
| 161 | |||
| 162 | $input = new StringInput('cache:flush'); |
||
| 163 | $this->getApplication()->run($input, new NullOutput()); |
||
| 164 | } |
||
| 165 | |||
| 193 |
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: