| Conditions | 5 |
| Paths | 10 |
| Total Lines | 53 |
| Code Lines | 35 |
| 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 |
||
| 106 | protected function generate(InputInterface $input, OutputInterface $output) |
||
| 107 | { |
||
| 108 | $model = ucfirst($input->getArgument('model')); |
||
| 109 | |||
| 110 | // generate CRUD |
||
| 111 | $crudFile = $this->getApplication()->getModelPath($model) .DS. 'Crud.php'; |
||
| 112 | |||
| 113 | if (file_exists($crudFile)) { |
||
| 114 | $this->comment("Crud file <info>$model/Crud.php</info> already exists"); |
||
| 115 | } else { |
||
| 116 | $template = $this->getTemplate('CrudTemplate'); |
||
| 117 | $template->setFilePath($crudFile); |
||
| 118 | $template->setTemplateData([ |
||
| 119 | 'model' => $model |
||
| 120 | ]); |
||
| 121 | |||
| 122 | $generator = new Generator\Generator($template); |
||
| 123 | $generator->make(); |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($module = $input->getArgument('module')) { |
||
| 127 | $this->write("Generate <info>$module/controllers/crud.php</info>"); |
||
| 128 | |||
| 129 | $controllerFile = $this->getControllerPath($module, 'crud'); |
||
| 130 | if (file_exists($controllerFile)) { |
||
| 131 | $this->comment("Controller file <info>$module/crud</info> already exists"); |
||
| 132 | } else { |
||
| 133 | $template = new Generator\Template\CrudControllerTemplate(); |
||
| 134 | $template->setFilePath($controllerFile); |
||
| 135 | $template->setTemplateData(['model' => $model]); |
||
| 136 | |||
| 137 | $generator = new Generator\Generator($template); |
||
| 138 | $generator->make(); |
||
| 139 | } |
||
| 140 | |||
| 141 | $this->write("Generate <info>$module/views/crud.phtml</info>"); |
||
| 142 | |||
| 143 | $viewFile = $this->getViewPath($module, 'crud'); |
||
| 144 | if (file_exists($viewFile)) { |
||
| 145 | $this->comment("View file <info>$module/crud</info> already exists"); |
||
| 146 | } else { |
||
| 147 | $template = new Generator\Template\CrudViewTemplate(); |
||
| 148 | $template->setFilePath($viewFile); |
||
| 149 | $template->setTemplateData([ |
||
| 150 | 'model' => $model, |
||
| 151 | 'module' => $module |
||
| 152 | ]); |
||
| 153 | |||
| 154 | $generator = new Generator\Generator($template); |
||
| 155 | $generator->make(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 181 |
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: