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:
| 1 | <?php |
||
| 28 | class Website |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * The location of where the compiled website will be written to. |
||
| 32 | * |
||
| 33 | * @var Folder |
||
| 34 | */ |
||
| 35 | private $outputDirectory; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The main configuration to be used to build the specified website. |
||
| 39 | * |
||
| 40 | * @var Configuration |
||
| 41 | */ |
||
| 42 | private $configuration; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * When set to true, the Stakx website will be built without a configuration file. |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private $confLess; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * When set to true, Twig templates will not have access to filters or functions which provide access to the |
||
| 53 | * filesystem. |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | private $safeMode; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * When set to true, Stakx will not clean the _site folder after a rebuild. |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | private $noClean; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var StakxLogger |
||
| 68 | */ |
||
| 69 | private $output; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var AssetManager |
||
| 73 | */ |
||
| 74 | private $am; |
||
|
|
|||
| 75 | |||
| 76 | /** |
||
| 77 | * @var CollectionManager |
||
| 78 | */ |
||
| 79 | private $cm; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var DataManager |
||
| 83 | */ |
||
| 84 | private $dm; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var Filesystem |
||
| 88 | */ |
||
| 89 | private $fs; |
||
| 90 | |||
| 91 | /** @var MenuManager */ |
||
| 92 | private $mm; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var PageManager |
||
| 96 | */ |
||
| 97 | private $pm; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var ThemeManager |
||
| 101 | */ |
||
| 102 | private $tm; |
||
| 103 | |||
| 104 | /** @var Compiler */ |
||
| 105 | private $compiler; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Website constructor. |
||
| 109 | * |
||
| 110 | * @param OutputInterface $output |
||
| 111 | */ |
||
| 112 | public function __construct(OutputInterface $output) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Compile the website. |
||
| 124 | * |
||
| 125 | * @param bool $tracking Whether or not to keep track of files as they're compiled to save time in 'watch' |
||
| 126 | */ |
||
| 127 | public function build($tracking = false) |
||
| 212 | |||
| 213 | public function watch() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return Configuration |
||
| 263 | */ |
||
| 264 | public function getConfiguration() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $configFile |
||
| 271 | * |
||
| 272 | * @throws \LogicException |
||
| 273 | */ |
||
| 274 | public function setConfiguration($configFile) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get whether or not the website is being built in Configuration-less mode. |
||
| 296 | * |
||
| 297 | * @return bool True when being built with no configuration file |
||
| 298 | */ |
||
| 299 | public function isConfLess() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Set whether or not the website should be built with a configuration. |
||
| 306 | * |
||
| 307 | * @param bool $status True when a website should be built without a configuration |
||
| 308 | */ |
||
| 309 | public function setConfLess($status) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get whether or not the website is being built in safe mode. |
||
| 316 | * |
||
| 317 | * Safe mode is defined as disabling file system access from Twig and disabling user Twig extensions |
||
| 318 | * |
||
| 319 | * @return bool True when the website is being built in safe mode |
||
| 320 | */ |
||
| 321 | public function isSafeMode() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set whether a website should be built in safe mode. |
||
| 328 | * |
||
| 329 | * @param bool $bool True if a website should be built in safe mode |
||
| 330 | */ |
||
| 331 | public function setSafeMode($bool) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | public function isNoClean() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param bool $noClean |
||
| 346 | */ |
||
| 347 | public function setNoClean($noClean) |
||
| 351 | |||
| 352 | private function creationWatcher($filePath) |
||
| 353 | { |
||
| 354 | $this->output->writeln(sprintf('File creation detected: %s', $filePath)); |
||
| 355 | |||
| 356 | if ($this->pm->isHandled($filePath)) |
||
| 357 | { |
||
| 358 | $this->pm->createNewItem($filePath); |
||
| 359 | $this->pm->refreshItem($filePath); |
||
| 360 | } |
||
| 361 | elseif ($this->cm->isHandled($filePath)) |
||
| 362 | { |
||
| 363 | $contentItem = $this->cm->createNewItem($filePath); |
||
| 364 | TwigManager::getInstance()->addGlobal('collections', $this->cm->getCollections()); |
||
| 365 | |||
| 366 | $this->pm->trackNewContentItem($contentItem); |
||
| 367 | $this->compiler->compileContentItem($contentItem); |
||
| 368 | $this->compiler->compileSome(array( |
||
| 369 | 'namespace' => 'collections', |
||
| 370 | 'dependency' => $contentItem->getCollection() |
||
| 371 | )); |
||
| 372 | } |
||
| 373 | View Code Duplication | elseif ($this->dm->isHandled($filePath)) |
|
| 374 | { |
||
| 375 | $change = $this->dm->createNewItem($filePath); |
||
| 376 | TwigManager::getInstance()->addGlobal('data', $this->dm->getDataItems()); |
||
| 377 | |||
| 378 | $this->compiler->compileSome(array( |
||
| 379 | 'namespace' => 'data', |
||
| 380 | 'dependency' => $change |
||
| 381 | )); |
||
| 382 | } |
||
| 383 | elseif (!is_null($this->tm) && $this->tm->isHandled($filePath)) |
||
| 384 | { |
||
| 385 | $this->tm->createNewItem($filePath); |
||
| 386 | } |
||
| 387 | elseif ($this->am->isHandled($filePath)) |
||
| 388 | { |
||
| 389 | $this->am->createNewItem($filePath); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | private function modificationWatcher($filePath) |
||
| 394 | { |
||
| 395 | $this->output->writeln(sprintf('File change detected: %s', $filePath)); |
||
| 396 | |||
| 397 | if ($this->pm->isTracked($filePath)) |
||
| 398 | { |
||
| 399 | $this->pm->refreshItem($filePath); |
||
| 400 | } |
||
| 401 | elseif ($this->cm->isTracked($filePath)) |
||
| 402 | { |
||
| 403 | $contentItem = &$this->cm->getContentItem($filePath); |
||
| 404 | $contentItem->refreshFileContent(); |
||
| 405 | |||
| 406 | $this->compiler->compileContentItem($contentItem); |
||
| 407 | $this->compiler->compileSome(array( |
||
| 408 | 'namespace' => 'collections', |
||
| 409 | 'dependency' => $contentItem->getCollection() |
||
| 410 | )); |
||
| 411 | } |
||
| 412 | View Code Duplication | elseif ($this->dm->isTracked($filePath)) |
|
| 413 | { |
||
| 414 | $change = $this->dm->refreshItem($filePath); |
||
| 415 | TwigManager::getInstance()->addGlobal('data', $this->dm->getDataItems()); |
||
| 416 | |||
| 417 | $this->compiler->compileSome(array( |
||
| 418 | 'namespace' => 'data', |
||
| 419 | 'dependency' => $change |
||
| 420 | )); |
||
| 421 | } |
||
| 422 | elseif (!is_null($this->tm) && $this->tm->isTracked($filePath)) |
||
| 423 | { |
||
| 424 | $this->tm->refreshItem($filePath); |
||
| 425 | } |
||
| 426 | elseif ($this->am->isTracked($filePath)) |
||
| 427 | { |
||
| 428 | $this->am->refreshItem($filePath); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Prepare the Stakx environment by creating necessary cache folders. |
||
| 434 | * |
||
| 435 | * @param bool $cleanDirectory Clean the target directory |
||
| 436 | */ |
||
| 437 | private function createFolderStructure($cleanDirectory) |
||
| 450 | } |
||
| 451 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.