| Conditions | 15 |
| Paths | 4098 |
| Total Lines | 50 |
| 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 |
||
| 37 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 38 | { |
||
| 39 | $this->detectMagento($output); |
||
| 40 | if (!$this->initMagento()) { |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | |||
| 44 | $entityType = $input->getArgument('entityType'); |
||
| 45 | $attributeCode = $input->getArgument('attributeCode'); |
||
| 46 | |||
| 47 | $attribute = $this->getAttribute($entityType, $attributeCode); |
||
| 48 | if (!$attribute) { |
||
| 49 | throw new InvalidArgumentException('Attribute was not found.'); |
||
| 50 | } |
||
| 51 | |||
| 52 | $table = array( |
||
| 53 | array('ID', $attribute->getId()), |
||
| 54 | array('Code', $attribute->getName()), |
||
| 55 | array('Attribute-Set-ID', $attribute->getAttributeSetId()), |
||
| 56 | array('Visible-On-Front', $attribute->getIsVisibleOnFront() ? 'yes' : 'no'), |
||
| 57 | array('Attribute-Model', $attribute->getAttributeModel() ? $attribute->getAttributeModel() : ''), |
||
| 58 | array('Backend-Model', $attribute->getBackendModel() ? $attribute->getBackendModel() : ''), |
||
| 59 | array('Backend-Table', $attribute->getBackendTable() ? $attribute->getBackendTable() : ''), |
||
| 60 | array('Backend-Type', $attribute->getBackendType() ? $attribute->getBackendType() : ''), |
||
| 61 | array('Source-Model', $attribute->getSourceModel() ? $attribute->getSourceModel() : ''), |
||
| 62 | array('Cache-ID-Tags', $attribute->getCacheIdTags() ? implode(',', $attribute->getCacheIdTags()) : ''), |
||
| 63 | array('Cache-Tags', $attribute->getCacheTags() ? implode(',', $attribute->getCacheTags()) : ''), |
||
| 64 | array('Default-Value', $attribute->getDefaultValue() ? $attribute->getDefaultValue() : ''), |
||
| 65 | array( |
||
| 66 | 'Flat-Columns', |
||
| 67 | $attribute->getFlatColumns() ? implode(',', array_keys($attribute->getFlatColumns())) : '', |
||
| 68 | ), |
||
| 69 | array('Flat-Indexes', $attribute->getFlatIndexes() ? implode(',', $attribute->getFlatIndexes()) : ''), |
||
| 70 | ); |
||
| 71 | |||
| 72 | if ($attribute->getFrontend()) { |
||
| 73 | $table[] = array('Frontend-Label', $attribute->getFrontend()->getLabel()); |
||
| 74 | $table[] = array('Frontend-Class', trim($attribute->getFrontend()->getClass())); |
||
| 75 | $table[] = array('Frontend-Input', trim($attribute->getFrontend()->getInputType())); |
||
| 76 | $table[] = array( |
||
| 77 | 'Frontend-Input-Renderer-Class', |
||
| 78 | trim($attribute->getFrontend()->getInputRendererClass()), |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | $this |
||
|
|
|||
| 83 | ->getHelper('table') |
||
| 84 | ->setHeaders(array('Type', 'Value')) |
||
| 85 | ->renderByFormat($output, $table, $input->getOption('format')); |
||
| 86 | } |
||
| 87 | |||
| 99 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: