| Conditions | 5 |
| Paths | 9 |
| Total Lines | 55 |
| Code Lines | 34 |
| 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 |
||
| 85 | protected function generate(InputInterface $input, OutputInterface $output) |
||
| 86 | { |
||
| 87 | $modelName = ucfirst($input->getArgument('model')); |
||
| 88 | $tableName = $input->getArgument('table'); |
||
| 89 | |||
| 90 | if ($this->getApplication()->isModelExists($modelName)) { |
||
| 91 | $helper = $this->getHelperSet()->get("question"); |
||
| 92 | $question = new ConfirmationQuestion( |
||
| 93 | "\n<question>Model $modelName would be overwritten. y/N?</question>:\n> ", |
||
| 94 | false |
||
| 95 | ); |
||
| 96 | |||
| 97 | if (!$helper->ask($input, $output, $question)) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | // generate table |
||
| 103 | $tableFile = $this->getApplication()->getModelPath($modelName) .DS. 'Table.php'; |
||
| 104 | |||
| 105 | if (file_exists($tableFile)) { |
||
| 106 | $this->comment("Table file <info>$modelName/Table.php</info> already exists"); |
||
| 107 | } else { |
||
| 108 | $template = $this->getTemplate('TableTemplate'); |
||
| 109 | $template->setFilePath($tableFile); |
||
| 110 | $template->setTemplateData([ |
||
| 111 | 'model' => $modelName, |
||
| 112 | 'table' => $tableName, |
||
| 113 | 'primaryKey' => $this->getPrimaryKey($tableName) |
||
| 114 | ]); |
||
| 115 | |||
| 116 | $generator = new Generator\Generator($template); |
||
| 117 | $generator->make(); |
||
| 118 | } |
||
| 119 | |||
| 120 | // generate row |
||
| 121 | $rowFile = $this->getApplication()->getModelPath($modelName) .DS. 'Row.php'; |
||
| 122 | |||
| 123 | if (file_exists($rowFile)) { |
||
| 124 | $this->comment("Row file <info>$modelName/Row.php</info> already exists"); |
||
| 125 | } else { |
||
| 126 | $template = $this->getTemplate('RowTemplate'); |
||
| 127 | $template->setFilePath($rowFile); |
||
| 128 | $template->setTemplateData( |
||
| 129 | [ |
||
| 130 | 'model' => $modelName, |
||
| 131 | 'table' => $tableName, |
||
| 132 | 'columns' => $this->getColumns($tableName) |
||
| 133 | ] |
||
| 134 | ); |
||
| 135 | |||
| 136 | $generator = new Generator\Generator($template); |
||
| 137 | $generator->make(); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 204 |
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 sub-classes 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 parent class: