Conditions | 11 |
Paths | 32 |
Total Lines | 82 |
Code Lines | 52 |
Lines | 26 |
Ratio | 31.71 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
137 | private function askForArguments($input, $output) |
||
138 | { |
||
139 | $helper = $this->getHelper('question'); |
||
140 | $_argument = array(); |
||
141 | |||
142 | // Store ID |
||
143 | if (is_null($input->getArgument('store-id'))) { |
||
144 | $store_id = Mage::getModel('core/store')->getCollection() |
||
145 | ->addFieldToSelect('*') |
||
146 | ->addFieldToFilter('store_id', array('gt' => 0)) |
||
147 | ->setOrder('store_id', 'ASC'); |
||
148 | $_store_ids = array(); |
||
149 | |||
150 | foreach ($store_id as $item) { |
||
151 | $_store_ids[$item['store_id']] = $item['store_id'] . "|" . $item['code']; |
||
152 | } |
||
153 | |||
154 | $question = new ChoiceQuestion('Please select Store ID (default: 1)', $_store_ids, self::DEFAULT_STORE_ID); |
||
155 | $question->setErrorMessage('Store ID "%s" is invalid.'); |
||
156 | $response = explode("|", $helper->ask($input, $output, $question)); |
||
157 | $input->setArgument('store-id', $response[0]); |
||
158 | } |
||
159 | $output->writeln('<info>Store ID selected: ' . $input->getArgument('store-id') . "</info>"); |
||
160 | $_argument['store-id'] = $input->getArgument('store-id'); |
||
161 | |||
162 | // Number of Categories |
||
163 | View Code Duplication | if (is_null($input->getArgument('category-number'))) { |
|
164 | $question = new Question("Please enter the number of categories to create (default 1): ", 1); |
||
165 | $question->setValidator(function ($answer) { |
||
166 | $answer = (int) $answer; |
||
167 | if (!is_int($answer) || $answer <= 0) { |
||
168 | throw new RuntimeException('Please enter an integer value or > 0'); |
||
169 | } |
||
170 | |||
171 | return $answer; |
||
172 | }); |
||
173 | $input->setArgument('category-number', $helper->ask($input, $output, $question)); |
||
174 | } |
||
175 | $output->writeln( |
||
176 | '<info>Number of categories to create: ' . $input->getArgument('category-number') . "</info>" |
||
177 | ); |
||
178 | $_argument['category-number'] = $input->getArgument('category-number'); |
||
179 | |||
180 | // Number of child categories |
||
181 | View Code Duplication | if (is_null($input->getArgument('children-categories-number'))) { |
|
182 | $question = new Question( |
||
183 | "Number of children for each category created (default: 0 - use '-1' for random from 0 to 5): ", |
||
184 | 0 |
||
185 | ); |
||
186 | $question->setValidator(function ($answer) { |
||
187 | $answer = (int) $answer; |
||
188 | if (!is_int($answer) || $answer < -1) { |
||
189 | throw new RuntimeException("Please enter an integer value or >= -1"); |
||
190 | } |
||
191 | |||
192 | return $answer; |
||
193 | }); |
||
194 | $input->setArgument('children-categories-number', $helper->ask($input, $output, $question)); |
||
195 | } |
||
196 | if ($input->getArgument('children-categories-number') == -1) { |
||
197 | $input->setArgument('children-categories-number', rand(0, 5)); |
||
198 | } |
||
199 | |||
200 | $output->writeln( |
||
201 | '<info>Number of categories children to create: ' . $input->getArgument('children-categories-number') . |
||
202 | "</info>" |
||
203 | ); |
||
204 | $_argument['children-categories-number'] = $input->getArgument('children-categories-number'); |
||
205 | |||
206 | // Category name prefix |
||
207 | if (is_null($input->getArgument('category-name-prefix'))) { |
||
208 | $question = new Question( |
||
209 | "Please enter the category name prefix (default '" . self::DEFAULT_CATEGORY_NAME . "'): ", |
||
210 | self::DEFAULT_CATEGORY_NAME |
||
211 | ); |
||
212 | $input->setArgument('category-name-prefix', $helper->ask($input, $output, $question)); |
||
213 | } |
||
214 | $output->writeln('<info>CATEGORY NAME PREFIX: ' . $input->getArgument('category-name-prefix') . "</info>"); |
||
215 | $_argument['category-name-prefix'] = $input->getArgument('category-name-prefix'); |
||
216 | |||
217 | return $_argument; |
||
218 | } |
||
219 | } |
||
220 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.