| Conditions | 17 |
| Paths | 86 |
| Total Lines | 87 |
| Code Lines | 47 |
| 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 |
||
| 141 | private function generateResponder($actionName) { |
||
| 142 | $this->logger->info('Generate Responder for: ' . $actionName); |
||
| 143 | $module = $this->packageService->getModule(); |
||
| 144 | |||
| 145 | if (!$module->hasAction($actionName)) { |
||
| 146 | throw new \RuntimeException(sprintf('action (%s) not found', $actionName)); |
||
| 147 | } |
||
| 148 | |||
| 149 | $input = $this->io->getInput(); |
||
| 150 | $force = $input->getOption('force'); |
||
| 151 | $format = $input->getOption('format'); |
||
| 152 | $template = $input->getOption('template'); |
||
| 153 | $action = $module->getAction($actionName); |
||
| 154 | |||
| 155 | // check if relationship response |
||
| 156 | if (Text::create($actionName)->contains('relationship') && $format == 'json') { |
||
| 157 | return $this->generateRelationshipResponder($action); |
||
| 158 | } |
||
| 159 | |||
| 160 | // responder class name |
||
| 161 | if (!$action->hasResponder($format)) { |
||
| 162 | $namespaceGenerator = $this->factory->getNamespaceGenerator(); |
||
| 163 | $actionNamespace = $namespaceGenerator->getActionNamespace(); |
||
| 164 | $className = Text::create($action->getClass()) |
||
| 165 | ->replace($actionNamespace, '') |
||
| 166 | ->prepend($namespaceGenerator->getResponderNamespaceByFormat($format)) |
||
| 167 | ->toString(); |
||
| 168 | $className = preg_replace('/Action$/', ucwords($format) . 'Responder', $className); |
||
| 169 | $action->setResponder($format, $className); |
||
| 170 | } |
||
| 171 | |||
| 172 | // action information |
||
| 173 | $parsed = $this->factory->getActionNameGenerator()->parseName($actionName); |
||
| 174 | $type = $parsed['type']; |
||
| 175 | $isModel = $type && $this->modelService->hasModel($parsed['modelName']); |
||
| 176 | |||
| 177 | // find generator |
||
| 178 | $generator = null; |
||
| 179 | |||
| 180 | // model given and format is json |
||
| 181 | if ($isModel && $format == 'json') { |
||
| 182 | $force = true; |
||
| 183 | $generator = $this->factory->createModelJsonResponderGenerator($type); |
||
| 184 | } |
||
| 185 | |||
| 186 | // payload |
||
| 187 | else if ($template == 'payload') { |
||
| 188 | $generator = $this->factory->createPayloadResponderGenerator($format); |
||
| 189 | } |
||
| 190 | |||
| 191 | // json + dump |
||
| 192 | else if ($format == 'json' && $template == 'api') { |
||
| 193 | $generator = new ApiJsonResponderGenerator($this->service); |
||
| 194 | $generator->setSerializer($this->getSerializer()); |
||
| 195 | } |
||
| 196 | |||
| 197 | // blank json |
||
| 198 | else if ($format == 'json') { |
||
| 199 | $generator = new SkeletonJsonResponderGenerator($this->service); |
||
| 200 | } |
||
| 201 | |||
| 202 | // html + twig |
||
| 203 | else if ($format == 'html' && $template == 'twig') { |
||
| 204 | $generator = new TwigHtmlResponderGenerator($this->service); |
||
| 205 | |||
| 206 | // create twig template |
||
| 207 | $path = $this->project->getRootPath() . '/res/templates/' . $actionName . '.twig'; |
||
| 208 | $file = new File($path); |
||
| 209 | $file->touch(); |
||
| 210 | } |
||
| 211 | |||
| 212 | // blank html as default |
||
| 213 | else if ($format == 'html') { |
||
| 214 | $generator = new SkeletonHtmlResponderGenerator($this->service); |
||
| 215 | } |
||
| 216 | |||
| 217 | // run generation, if generator was chosen |
||
| 218 | if ($generator !== null) { |
||
| 219 | /* @var $class PhpClass */ |
||
| 220 | $class = $generator->generate($action); |
||
| 221 | |||
| 222 | // write to file |
||
| 223 | $file = $this->codeService->getFile($class); |
||
| 224 | $overwrite = !$file->exists() || $force; |
||
| 225 | $this->codeService->dumpStruct($class, $overwrite); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 280 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: