Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SplashCreateControllerService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SplashCreateControllerService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class SplashCreateControllerService |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Generates a controller, view, and sets the instance up. |
||
| 20 | * |
||
| 21 | * @param string $controllerName |
||
| 22 | * @param string $instanceName |
||
| 23 | * @param string $namespace |
||
| 24 | * @param string $injectLogger |
||
| 25 | * @param string $injectTemplate |
||
| 26 | * @param string $injectDaoFactory |
||
| 27 | * @param array $actions |
||
| 28 | */ |
||
| 29 | public function generate(MoufManager $moufManager, $controllerName, $instanceName, $namespace, $injectLogger = false, |
||
| 30 | $injectTemplate = false, $injectDaoFactory = false, $actions = array()) |
||
| 31 | { |
||
| 32 | $namespace = rtrim($namespace, '\\').'\\'; |
||
| 33 | |||
| 34 | $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json'); |
||
| 35 | $possibleFileNames = $classNameMapper->getPossibleFileNames($namespace.$controllerName); |
||
| 36 | if (!isset($possibleFileNames[0])) { |
||
| 37 | throw new SplashException("The class '".$namespace.$controllerName."' cannot be loaded using rules defined in composer autoload section"); |
||
| 38 | } |
||
| 39 | $fileName = $possibleFileNames[0]; |
||
| 40 | $controllerPhpDirectory = dirname($fileName); |
||
| 41 | $errors = array(); |
||
| 42 | if (!preg_match('/^[a-z_]\w*$/i', $controllerName)) { |
||
| 43 | $errors['controllerNameError'] = 'This is not a valid PHP class name.'; |
||
| 44 | } |
||
| 45 | if (!preg_match('/^[a-z_][\w\\\\]*$/i', $namespace)) { |
||
| 46 | $errors['namespaceError'] = 'This is not a valid PHP namespace.'; |
||
| 47 | } |
||
| 48 | |||
| 49 | $namespace = trim($namespace, '\\'); |
||
| 50 | |||
| 51 | if (!file_exists(ROOT_PATH.'../database.tdbm') && $injectDaoFactory) { |
||
|
|
|||
| 52 | $injectDaoFactory = false; |
||
| 53 | } |
||
| 54 | |||
| 55 | // Check that instance does not already exists |
||
| 56 | if ($moufManager->has($instanceName)) { |
||
| 57 | $errors['instanceError'] = 'This instance already exists.'; |
||
| 58 | } |
||
| 59 | |||
| 60 | $injectTwig = false; |
||
| 61 | $importJsonResponse = false; |
||
| 62 | $importHtmlResponse = false; |
||
| 63 | $importRedirectResponse = false; |
||
| 64 | |||
| 65 | foreach ($actions as $key => $action) { |
||
| 66 | // Check if the view file exists |
||
| 67 | View Code Duplication | if ($injectTemplate && $action['view'] == 'twig') { |
|
| 68 | $injectTwig = true; |
||
| 69 | $importHtmlResponse = true; |
||
| 70 | $twigFile = ltrim($action['twigFile'], '/\\'); |
||
| 71 | |||
| 72 | $viewDirName = ROOT_PATH.'../../../'.dirname($twigFile); |
||
| 73 | $result = $this->createDirectory($viewDirName); |
||
| 74 | if (!$result) { |
||
| 75 | $errors['actions'][$key]['twigTemplateFileError'] = 'Unable to create directory "'.$viewDirName.'"'; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (file_exists(ROOT_PATH.'../../../'.$twigFile)) { |
||
| 79 | $errors['actions'][$key]['twigTemplateFileError'] = 'This file already exists.'; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | View Code Duplication | if ($injectTemplate && $action['view'] == 'php') { |
|
| 83 | $importHtmlResponse = true; |
||
| 84 | |||
| 85 | $phpFile = ltrim($action['phpFile'], '/\\'); |
||
| 86 | |||
| 87 | $viewDirName = ROOT_PATH.'../../../'.dirname($phpFile); |
||
| 88 | $result = $this->createDirectory($viewDirName); |
||
| 89 | if (!$result) { |
||
| 90 | $errors['actions'][$key]['phpTemplateFileError'] = 'Unable to create directory "'.$viewDirName.'"'; |
||
| 91 | } |
||
| 92 | |||
| 93 | if (file_exists(ROOT_PATH.'../../../'.$phpFile)) { |
||
| 94 | $errors['actions'][$key]['phpTemplateFileError'] = 'This file already exists.'; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | if ($action['view'] == 'redirect') { |
||
| 98 | if (!isset($action['redirect']) || empty($action['redirect'])) { |
||
| 99 | $errors['actions'][$key]['redirectError'] = 'Redirection URL cannot be empty.'; |
||
| 100 | } |
||
| 101 | $importRedirectResponse = true; |
||
| 102 | } |
||
| 103 | if ($action['view'] == 'json') { |
||
| 104 | $importJsonResponse = true; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // TODO: check that URLs are not in error. |
||
| 109 | |||
| 110 | |||
| 111 | if (!$errors) { |
||
| 112 | $result = $this->createDirectory(ROOT_PATH.'../../../'.$controllerPhpDirectory); |
||
| 113 | if (!$result) { |
||
| 114 | $errors['namespaceError'] = 'Unable to create directory: "'.$controllerPhpDirectory.'"'; |
||
| 115 | } elseif (file_exists(ROOT_PATH.'../../../'.$controllerPhpDirectory.$controllerName.'.php')) { |
||
| 116 | $errors['namespaceError'] = 'The file "'.$controllerPhpDirectory.$controllerName.'.php already exists."'; |
||
| 117 | } elseif (!is_writable(ROOT_PATH.'../../../'.$controllerPhpDirectory)) { |
||
| 118 | $errors['namespaceError'] = 'Unable to write file in directory: "'.$controllerPhpDirectory.'"'; |
||
| 119 | } |
||
| 120 | |||
| 121 | if (!$errors) { |
||
| 122 | ob_start(); |
||
| 123 | echo '<?php |
||
| 124 | '; |
||
| 125 | ?> |
||
| 126 | namespace <?= $namespace ?>; |
||
| 127 | |||
| 128 | use Mouf\Mvc\Splash\Annotations\Get; |
||
| 129 | use Mouf\Mvc\Splash\Annotations\Post; |
||
| 130 | use Mouf\Mvc\Splash\Annotations\Put; |
||
| 131 | use Mouf\Mvc\Splash\Annotations\Delete; |
||
| 132 | use Mouf\Mvc\Splash\Annotations\URL; |
||
| 133 | <?php if ($injectTemplate) { |
||
| 134 | ?> |
||
| 135 | use Mouf\Html\Template\TemplateInterface; |
||
| 136 | use Mouf\Html\HtmlElement\HtmlBlock; |
||
| 137 | <?php |
||
| 138 | |||
| 139 | } |
||
| 140 | ?> |
||
| 141 | <?php if ($injectLogger) { |
||
| 142 | ?> |
||
| 143 | use Psr\Log\LoggerInterface; |
||
| 144 | <?php |
||
| 145 | |||
| 146 | } |
||
| 147 | ?> |
||
| 148 | <?php if ($injectDaoFactory) { |
||
| 149 | ?> |
||
| 150 | use <?= $moufManager->getVariable('tdbmDefaultDaoNamespace').'\\Generated\\'.$moufManager->getVariable('tdbmDefaultDaoFactoryName') ?>; |
||
| 151 | <?php |
||
| 152 | |||
| 153 | } |
||
| 154 | ?> |
||
| 155 | <?php if ($injectTwig) { |
||
| 156 | ?> |
||
| 157 | use \Twig_Environment; |
||
| 158 | use Mouf\Html\Renderer\Twig\TwigTemplate; |
||
| 159 | <?php |
||
| 160 | |||
| 161 | } |
||
| 162 | ?> |
||
| 163 | <?php if ($importJsonResponse) { |
||
| 164 | ?> |
||
| 165 | use Zend\Diactoros\Response\JsonResponse; |
||
| 166 | <?php |
||
| 167 | |||
| 168 | } |
||
| 169 | ?> |
||
| 170 | <?php if ($importRedirectResponse) { |
||
| 171 | ?> |
||
| 172 | use Zend\Diactoros\Response\RedirectResponse; |
||
| 173 | <?php |
||
| 174 | |||
| 175 | } |
||
| 176 | ?> |
||
| 177 | <?php if ($importHtmlResponse) { |
||
| 178 | ?> |
||
| 179 | use Mouf\Mvc\Splash\HtmlResponse; |
||
| 180 | <?php |
||
| 181 | |||
| 182 | } |
||
| 183 | ?> |
||
| 184 | |||
| 185 | /** |
||
| 186 | * TODO: write controller comment |
||
| 187 | */ |
||
| 188 | class <?= $controllerName ?> { |
||
| 189 | |||
| 190 | <?php if ($injectLogger) { |
||
| 191 | ?> |
||
| 192 | /** |
||
| 193 | * The logger used by this controller. |
||
| 194 | * @var LoggerInterface |
||
| 195 | */ |
||
| 196 | private $logger; |
||
| 197 | |||
| 198 | <?php |
||
| 199 | |||
| 200 | } |
||
| 201 | ?> |
||
| 202 | <?php if ($injectTemplate) { |
||
| 203 | ?> |
||
| 204 | /** |
||
| 205 | * The template used by this controller. |
||
| 206 | * @var TemplateInterface |
||
| 207 | */ |
||
| 208 | private $template; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * The main content block of the page. |
||
| 212 | * @var HtmlBlock |
||
| 213 | */ |
||
| 214 | private $content; |
||
| 215 | |||
| 216 | <?php |
||
| 217 | |||
| 218 | } |
||
| 219 | ?> |
||
| 220 | <?php if ($injectDaoFactory) { |
||
| 221 | ?> |
||
| 222 | /** |
||
| 223 | * The DAO factory object. |
||
| 224 | * @var DaoFactory |
||
| 225 | */ |
||
| 226 | private $daoFactory; |
||
| 227 | |||
| 228 | <?php |
||
| 229 | |||
| 230 | } |
||
| 231 | ?> |
||
| 232 | <?php if ($injectTwig) { |
||
| 233 | ?> |
||
| 234 | /** |
||
| 235 | * The Twig environment (used to render Twig templates). |
||
| 236 | * @var Twig_Environment |
||
| 237 | */ |
||
| 238 | private $twig; |
||
| 239 | |||
| 240 | <?php |
||
| 241 | |||
| 242 | } |
||
| 243 | ?> |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Controller's constructor. |
||
| 247 | <?php |
||
| 248 | if ($injectLogger) { |
||
| 249 | echo " * @param LoggerInterface \$logger The logger\n"; |
||
| 250 | } |
||
| 251 | if ($injectTemplate) { |
||
| 252 | echo " * @param TemplateInterface \$template The template used by this controller\n"; |
||
| 253 | echo " * @param HtmlBlock \$content The main content block of the page\n"; |
||
| 254 | } |
||
| 255 | if ($injectDaoFactory) { |
||
| 256 | echo " * @param DaoFactory \$daoFactory The object in charge of retrieving DAOs\n"; |
||
| 257 | } |
||
| 258 | if ($injectTwig) { |
||
| 259 | echo " * @param Twig_Environment \$twig The Twig environment (used to render Twig templates)\n"; |
||
| 260 | } |
||
| 261 | ?> |
||
| 262 | */ |
||
| 263 | public function __construct(<?php |
||
| 264 | $parameters = array(); |
||
| 265 | if ($injectLogger) { |
||
| 266 | $parameters[] = 'LoggerInterface $logger'; |
||
| 267 | } |
||
| 268 | if ($injectTemplate) { |
||
| 269 | $parameters[] = 'TemplateInterface $template'; |
||
| 270 | $parameters[] = 'HtmlBlock $content'; |
||
| 271 | } |
||
| 272 | if ($injectDaoFactory) { |
||
| 273 | $parameters[] = 'DaoFactory $daoFactory'; |
||
| 274 | } |
||
| 275 | if ($injectTwig) { |
||
| 276 | $parameters[] = 'Twig_Environment $twig'; |
||
| 277 | } |
||
| 278 | echo implode(', ', $parameters); |
||
| 279 | ?>) { |
||
| 280 | <?php if ($injectLogger) { |
||
| 281 | ?> |
||
| 282 | $this->logger = $logger; |
||
| 283 | <?php |
||
| 284 | |||
| 285 | } |
||
| 286 | if ($injectTemplate) { |
||
| 287 | ?> |
||
| 288 | $this->template = $template; |
||
| 289 | $this->content = $content; |
||
| 290 | <?php |
||
| 291 | |||
| 292 | } |
||
| 293 | if ($injectDaoFactory) { |
||
| 294 | ?> |
||
| 295 | $this->daoFactory = $daoFactory; |
||
| 296 | <?php |
||
| 297 | |||
| 298 | } |
||
| 299 | if ($injectTwig) { |
||
| 300 | ?> |
||
| 301 | $this->twig = $twig; |
||
| 302 | <?php |
||
| 303 | |||
| 304 | } |
||
| 305 | ?> |
||
| 306 | } |
||
| 307 | |||
| 308 | <?php foreach ($actions as $action): |
||
| 309 | // First step, let's detect the {parameters} in the URL and add them if necessary |
||
| 310 | // TODO |
||
| 311 | // TODO |
||
| 312 | // TODO |
||
| 313 | // TODO |
||
| 314 | |||
| 315 | ?> |
||
| 316 | /** |
||
| 317 | * @URL("<?= addslashes($action['url']) ?>") |
||
| 318 | |||
| 319 | <?php if ($action['anyMethod'] == 'false') { |
||
| 320 | if ($action['getMethod'] == 'true') { |
||
| 321 | echo " * @Get\n"; |
||
| 322 | } |
||
| 323 | if ($action['postMethod'] == 'true') { |
||
| 324 | echo " * @Post\n"; |
||
| 325 | } |
||
| 326 | if ($action['putMethod'] == 'true') { |
||
| 327 | echo " * @Put\n"; |
||
| 328 | } |
||
| 329 | if ($action['deleteMethod'] == 'true') { |
||
| 330 | echo " * @Delete\n"; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | if (isset($action['parameters'])) { |
||
| 334 | $parameters = $action['parameters']; |
||
| 335 | foreach ($parameters as $parameter) { |
||
| 336 | echo ' * @param '.$parameter['type'].' $'.$parameter['name']."\n"; |
||
| 337 | } |
||
| 338 | } else { |
||
| 339 | $parameters = array(); |
||
| 340 | } |
||
| 341 | ?> |
||
| 342 | */ |
||
| 343 | public function <?= $action['method'] ?>(<?php |
||
| 344 | $parametersCode = array(); |
||
| 345 | foreach ($parameters as $parameter) { |
||
| 346 | $parameterCode = '$'.$parameter['name']; |
||
| 347 | if ($parameter['optionnal'] == 'true') { |
||
| 348 | if ($parameter['type'] == 'int') { |
||
| 349 | $defaultValue = (int) $parameter['defaultValue']; |
||
| 350 | } elseif ($parameter['type'] == 'number') { |
||
| 351 | $defaultValue = (float) $parameter['defaultValue']; |
||
| 352 | } else { |
||
| 353 | $defaultValue = $parameter['defaultValue']; |
||
| 354 | } |
||
| 355 | $parameterCode .= ' = '.var_export($defaultValue, true); |
||
| 356 | } |
||
| 357 | $parametersCode[] = $parameterCode; |
||
| 358 | } |
||
| 359 | echo implode(', ', $parametersCode); |
||
| 360 | ?>) { |
||
| 361 | // TODO: write content of action here |
||
| 362 | |||
| 363 | <?php if ($injectTemplate && $action['view'] == 'twig'): ?> |
||
| 364 | // Let's add the twig file to the template. |
||
| 365 | $this->content->addHtmlElement(new TwigTemplate($this->twig, <?php var_export($action['twigFile']); |
||
| 366 | ?>, array("message"=>"world"))); |
||
| 367 | |||
| 368 | return new HtmlResponse($this->template); |
||
| 369 | <?php elseif ($injectTemplate && $action['view'] == 'php'): ?> |
||
| 370 | // Let's add the view to the content. |
||
| 371 | // Note: $this is passed as the scope, so in the view file, you can refer to protected |
||
| 372 | // and public variables and methods of this constructor using "$this". |
||
| 373 | $this->content->addFile(ROOT_PATH.<?php var_export($action['phpFile']) ?>, $this); |
||
| 374 | |||
| 375 | return new HtmlResponse($this->template); |
||
| 376 | <?php elseif ($action['view'] == 'json'): ?> |
||
| 377 | |||
| 378 | return new JsonResponse([ "status"=>"ok" ]); |
||
| 379 | <?php elseif ($action['view'] == 'redirect'): ?> |
||
| 380 | |||
| 381 | return new RedirectResponse(<?php var_export($action['redirect']); |
||
| 382 | ?>); |
||
| 383 | <?php endif; |
||
| 384 | ?> |
||
| 385 | } |
||
| 386 | <?php endforeach; |
||
| 387 | ?> |
||
| 388 | } |
||
| 389 | <?php |
||
| 390 | $file = ob_get_clean(); |
||
| 391 | |||
| 392 | file_put_contents(ROOT_PATH.'../../../'.$fileName, $file); |
||
| 393 | chmod(ROOT_PATH.'../../../'.$fileName, 0664); |
||
| 394 | |||
| 395 | // Now, let's create the views files |
||
| 396 | foreach ($actions as $action) { |
||
| 397 | if ($injectTemplate && $action['view'] == 'twig') { |
||
| 398 | $twigTemplateFile = $this->generateTwigView(); |
||
| 399 | |||
| 400 | $twigFile = ltrim($action['twigFile'], '/\\'); |
||
| 401 | |||
| 402 | file_put_contents(ROOT_PATH.'../../../'.$twigFile, $twigTemplateFile); |
||
| 403 | chmod(ROOT_PATH.'../../../'.$twigFile, 0664); |
||
| 404 | } elseif ($injectTemplate && $action['view'] == 'php') { |
||
| 405 | $phpTemplateFile = $this->generatePhpView($namespace.'\\'.$controllerName); |
||
| 406 | |||
| 407 | $phpFile = ltrim($action['phpFile'], '/\\'); |
||
| 408 | |||
| 409 | file_put_contents(ROOT_PATH.'../../../'.$phpFile, $phpTemplateFile); |
||
| 410 | chmod(ROOT_PATH.'../../../'.$phpFile, 0664); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | // Now, let's create the instance |
||
| 415 | $controllerInstance = $moufManager->createInstance($namespace.'\\'.$controllerName); |
||
| 416 | $controllerInstance->setName($instanceName); |
||
| 417 | if ($injectLogger) { |
||
| 418 | if ($moufManager->has('psr.errorLogLogger')) { |
||
| 419 | $controllerInstance->getProperty('logger')->setValue($moufManager->getInstanceDescriptor('psr.errorLogLogger')); |
||
| 420 | } |
||
| 421 | } |
||
| 422 | if ($injectTemplate) { |
||
| 423 | if ($moufManager->has('bootstrapTemplate')) { |
||
| 424 | $controllerInstance->getProperty('template')->setValue($moufManager->getInstanceDescriptor('bootstrapTemplate')); |
||
| 425 | } |
||
| 426 | if ($moufManager->has('block.content')) { |
||
| 427 | $controllerInstance->getProperty('content')->setValue($moufManager->getInstanceDescriptor('block.content')); |
||
| 428 | } |
||
| 429 | } |
||
| 430 | if ($injectDaoFactory) { |
||
| 431 | if ($moufManager->has('daoFactory')) { |
||
| 432 | $controllerInstance->getProperty('daoFactory')->setValue($moufManager->getInstanceDescriptor('daoFactory')); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | if ($injectTwig) { |
||
| 436 | if ($moufManager->has('twigEnvironment')) { |
||
| 437 | $controllerInstance->getProperty('twig')->setValue($moufManager->getInstanceDescriptor('twigEnvironment')); |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | $moufManager->rewriteMouf(); |
||
| 442 | |||
| 443 | // There is a new class, let's purge the cache |
||
| 444 | $moufCache = new MoufCache(); |
||
| 445 | $moufCache->purgeAll(); |
||
| 446 | |||
| 447 | // TODO: purge cache |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | if ($errors) { |
||
| 452 | $exception = new SplashCreateControllerServiceException('Errors detected'); |
||
| 453 | $exception->setErrors($errors); |
||
| 454 | throw $exception; |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | private function generateTwigView() |
||
| 462 | |||
| 463 | private function generatePhpView($controllerFQCN) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string $directory |
||
| 471 | * |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | private function createDirectory($directory) |
||
| 487 | } |
||
| 488 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: