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