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 Website 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 Website, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Website |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * The location of where the compiled website will be written to. |
||
| 36 | * |
||
| 37 | * @var Folder |
||
| 38 | */ |
||
| 39 | private $outputDirectory; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The main configuration to be used to build the specified website. |
||
| 43 | * |
||
| 44 | * @var Configuration |
||
| 45 | */ |
||
| 46 | private $configuration; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * When set to true, the Stakx website will be built without a configuration file. |
||
| 50 | * |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | private $confLess; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var StakxLogger |
||
| 57 | */ |
||
| 58 | private $output; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var AssetManager |
||
| 62 | */ |
||
| 63 | private $am; |
||
|
|
|||
| 64 | |||
| 65 | /** |
||
| 66 | * @var CollectionManager |
||
| 67 | */ |
||
| 68 | private $cm; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var DataManager |
||
| 72 | */ |
||
| 73 | private $dm; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var Filesystem |
||
| 77 | */ |
||
| 78 | private $fs; |
||
| 79 | |||
| 80 | /** @var MenuManager */ |
||
| 81 | private $mm; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var PageManager |
||
| 85 | */ |
||
| 86 | private $pm; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var ThemeManager |
||
| 90 | */ |
||
| 91 | private $tm; |
||
| 92 | |||
| 93 | /** @var Compiler */ |
||
| 94 | private $compiler; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Website constructor. |
||
| 98 | * |
||
| 99 | * @param OutputInterface $output |
||
| 100 | */ |
||
| 101 | public function __construct(OutputInterface $output) |
||
| 102 | { |
||
| 103 | $this->output = new StakxLogger($output); |
||
| 104 | $this->cm = new CollectionManager(); |
||
| 105 | $this->dm = new DataManager(); |
||
| 106 | $this->mm = new MenuManager(); |
||
| 107 | $this->pm = new PageManager(); |
||
| 108 | $this->fs = new Filesystem(); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Compile the website. |
||
| 113 | * |
||
| 114 | * @param bool $tracking Whether or not to keep track of files as they're compiled to save time in 'watch' |
||
| 115 | */ |
||
| 116 | public function build($tracking = false) |
||
| 117 | { |
||
| 118 | Service::setParameter(BuildableCommand::WATCHING, $tracking); |
||
| 119 | |||
| 120 | // Configure the environment |
||
| 121 | $this->createFolderStructure(); |
||
| 122 | $this->configureHighlighter(); |
||
| 123 | |||
| 124 | // Our output directory |
||
| 125 | $this->outputDirectory = new Folder($this->getConfiguration()->getTargetFolder()); |
||
| 126 | $this->outputDirectory->setTargetDirectory($this->getConfiguration()->getBaseUrl()); |
||
| 127 | |||
| 128 | // Parse DataItems |
||
| 129 | $this->dm->setLogger($this->output); |
||
| 130 | $this->dm->enableTracking($tracking); |
||
| 131 | $this->dm->parseDataItems($this->getConfiguration()->getDataFolders()); |
||
| 132 | $this->dm->parseDataSets($this->getConfiguration()->getDataSets()); |
||
| 133 | |||
| 134 | // Prepare Collections |
||
| 135 | $this->cm->setLogger($this->output); |
||
| 136 | $this->cm->enableTracking($tracking); |
||
| 137 | $this->cm->parseCollections($this->getConfiguration()->getCollectionsFolders()); |
||
| 138 | |||
| 139 | // Handle PageViews |
||
| 140 | $this->pm->setLogger($this->output); |
||
| 141 | $this->pm->enableTracking($tracking); |
||
| 142 | $this->pm->setCollections($this->cm->getCollections()); |
||
| 143 | $this->pm->setDatasets($this->dm->getDataItems()); |
||
| 144 | $this->pm->parsePageViews($this->getConfiguration()->getPageViewFolders()); |
||
| 145 | |||
| 146 | // Handle the site's menu |
||
| 147 | $this->mm->setLogger($this->output); |
||
| 148 | $this->mm->buildFromPageViews($this->pm->getStaticPageViews()); |
||
| 149 | |||
| 150 | // Configure our Twig environment |
||
| 151 | $theme = $this->configuration->getTheme(); |
||
| 152 | $twigEnv = new TwigManager(); |
||
| 153 | $twigEnv->configureTwig($this->getConfiguration(), array( |
||
| 154 | 'safe' => Service::getParameter(BuildableCommand::SAFE_MODE), |
||
| 155 | 'globals' => array( |
||
| 156 | array('name' => 'site', 'value' => $this->getConfiguration()->getConfiguration()), |
||
| 157 | array('name' => 'collections', 'value' => $this->cm->getJailedCollections()), |
||
| 158 | array('name' => 'menu', 'value' => $this->mm->getSiteMenu()), |
||
| 159 | array('name' => 'pages', 'value' => $this->pm->getJailedStaticPageViews()), |
||
| 160 | array('name' => 'data', 'value' => $this->dm->getJailedDataItems()), |
||
| 161 | ), |
||
| 162 | )); |
||
| 163 | |||
| 164 | $profiler = null; |
||
| 165 | |||
| 166 | if (Service::getParameter(BuildableCommand::BUILD_PROFILE)) |
||
| 167 | { |
||
| 168 | $profiler = new \Twig_Profiler_Profile(); |
||
| 169 | TwigManager::getInstance()->addExtension(new \Twig_Extension_Profiler($profiler)); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Compile everything |
||
| 173 | $this->compiler = new Compiler(); |
||
| 174 | $this->compiler->setLogger($this->output); |
||
| 175 | $this->compiler->setRedirectTemplate($this->getConfiguration()->getRedirectTemplate()); |
||
| 176 | $this->compiler->setPageViews($this->pm->getPageViews(), $this->pm->getPageViewsFlattened()); |
||
| 177 | $this->compiler->setTargetFolder($this->outputDirectory); |
||
| 178 | $this->compiler->setThemeName($theme); |
||
| 179 | $this->compiler->compileAll(); |
||
| 180 | |||
| 181 | if (Service::getParameter(BuildableCommand::BUILD_PROFILE)) |
||
| 182 | { |
||
| 183 | $dumper = new StakxTwigTextProfiler(); |
||
| 184 | $dumper->setTemplateMappings($this->compiler->getTemplateMappings()); |
||
| 185 | $text = $dumper->dump($profiler); |
||
| 186 | $this->output->writeln($text); |
||
| 187 | } |
||
| 188 | |||
| 189 | // At this point, we are looking at static files to copy over meaning we need to ignore all of the files that |
||
| 190 | // make up the source of a stakx website |
||
| 191 | $assetsToIgnore = array_merge( |
||
| 192 | Configuration::$stakxSourceFiles, |
||
| 193 | $this->getConfiguration()->getExcludes() |
||
| 194 | ); |
||
| 195 | |||
| 196 | // |
||
| 197 | // Theme Management |
||
| 198 | // |
||
| 199 | if (!is_null($theme)) |
||
| 200 | { |
||
| 201 | $this->output->notice("Looking for '${theme}' theme..."); |
||
| 202 | |||
| 203 | $this->tm = new ThemeManager($theme); |
||
| 204 | $this->tm->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore); |
||
| 205 | $this->tm->setLogger($this->output); |
||
| 206 | $this->tm->enableTracking($tracking); |
||
| 207 | $this->tm->setFolder($this->outputDirectory); |
||
| 208 | $this->tm->copyFiles(); |
||
| 209 | } |
||
| 210 | |||
| 211 | // |
||
| 212 | // Static file management |
||
| 213 | // |
||
| 214 | $this->am = new AssetManager(); |
||
| 215 | $this->am->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore); |
||
| 216 | $this->am->setLogger($this->output); |
||
| 217 | $this->am->setFolder($this->outputDirectory); |
||
| 218 | $this->am->enableTracking($tracking); |
||
| 219 | $this->am->copyFiles(); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function watch() |
||
| 223 | { |
||
| 224 | $this->output->writeln('Building website...'); |
||
| 225 | $this->build(true); |
||
| 226 | $this->output->writeln(sprintf('Watching %s', getcwd())); |
||
| 227 | |||
| 228 | $exclusions = array_merge($this->getConfiguration()->getExcludes(), array( |
||
| 229 | $this->getConfiguration()->getTargetFolder() |
||
| 230 | )); |
||
| 231 | $fileExplorer = FileExplorer::create( |
||
| 232 | getcwd(), $exclusions, $this->getConfiguration()->getIncludes() |
||
| 233 | ); |
||
| 234 | |||
| 235 | $newWatcher = Watcher::create(getcwd()); |
||
| 236 | $newWatcher |
||
| 237 | ->setLogger($this->output) |
||
| 238 | ->setExcludePatterns(array_merge( |
||
| 239 | $exclusions, FileExplorer::$vcsPatterns, array(Configuration::CACHE_FOLDER) |
||
| 240 | )) |
||
| 241 | ->setIterator($fileExplorer->getExplorer()) |
||
| 242 | ->addListener(Create::NAME, function ($e) { $this->watchListenerFunction($e); }) |
||
| 243 | ->addListener(Modify::NAME, function ($e) { $this->watchListenerFunction($e); }) |
||
| 244 | ->addListener(Move::NAME, function ($e) { $this->watchListenerFunction($e); }) |
||
| 245 | ; |
||
| 246 | |||
| 247 | $this->output->writeln('Watch started successfully'); |
||
| 248 | |||
| 249 | $newWatcher->start(); |
||
| 250 | } |
||
| 251 | |||
| 252 | private function watchListenerFunction(AbstractEvent $event) |
||
| 253 | { |
||
| 254 | $filePath = $this->fs->getRelativePath($event->filename); |
||
| 255 | |||
| 256 | try |
||
| 257 | { |
||
| 258 | switch ($event::getEventName()) |
||
| 259 | { |
||
| 260 | case Create::NAME: |
||
| 261 | $this->creationWatcher($filePath); |
||
| 262 | break; |
||
| 263 | |||
| 264 | case Modify::NAME: |
||
| 265 | $this->modificationWatcher($filePath); |
||
| 266 | break; |
||
| 267 | |||
| 268 | case Move::NAME: |
||
| 269 | $newFile = $this->fs->getRelativePath($event->destFilename); |
||
| 270 | |||
| 271 | $this->deletionWatcher($filePath); |
||
| 272 | $this->creationWatcher($newFile); |
||
| 273 | break; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | catch (FileAwareException $e) |
||
| 277 | { |
||
| 278 | $this->output->writeln(sprintf("Your website failed to build with the following error in file '%s': %s", |
||
| 279 | $e->getPath(), |
||
| 280 | $e->getMessage() |
||
| 281 | )); |
||
| 282 | } |
||
| 283 | catch (\Exception $e) |
||
| 284 | { |
||
| 285 | $this->output->writeln(sprintf('Your website failed to build with the following error: %s', |
||
| 286 | $e->getMessage() |
||
| 287 | )); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return Configuration |
||
| 293 | */ |
||
| 294 | public function getConfiguration() |
||
| 295 | { |
||
| 296 | return $this->configuration; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $configFile |
||
| 301 | * |
||
| 302 | * @throws \LogicException |
||
| 303 | */ |
||
| 304 | public function setConfiguration($configFile) |
||
| 305 | { |
||
| 306 | if (!$this->fs->exists($configFile) && !$this->isConfLess()) |
||
| 307 | { |
||
| 308 | $this->output->error('You are trying to build a website in a directory without a configuration file. Is this what you meant to do?'); |
||
| 309 | $this->output->error("To build a website without a configuration, use the '--no-conf' option"); |
||
| 310 | |||
| 311 | throw new \LogicException('Cannot build a website without a configuration when not in Configuration-less mode'); |
||
| 312 | } |
||
| 313 | |||
| 314 | if ($this->isConfLess()) |
||
| 315 | { |
||
| 316 | $configFile = ''; |
||
| 317 | } |
||
| 318 | |||
| 319 | $this->configuration = new Configuration(); |
||
| 320 | $this->configuration->setLogger($this->output); |
||
| 321 | $this->configuration->parse($configFile); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get whether or not the website is being built in Configuration-less mode. |
||
| 326 | * |
||
| 327 | * @return bool True when being built with no configuration file |
||
| 328 | */ |
||
| 329 | public function isConfLess() |
||
| 330 | { |
||
| 331 | return $this->confLess; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set whether or not the website should be built with a configuration. |
||
| 336 | * |
||
| 337 | * @param bool $status True when a website should be built without a configuration |
||
| 338 | */ |
||
| 339 | public function setConfLess($status) |
||
| 340 | { |
||
| 341 | $this->confLess = $status; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $filePath |
||
| 346 | */ |
||
| 347 | private function creationWatcher($filePath) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $filePath |
||
| 390 | */ |
||
| 391 | private function modificationWatcher($filePath) |
||
| 392 | { |
||
| 393 | $this->output->writeln(sprintf('File change detected: %s', $filePath)); |
||
| 394 | |||
| 395 | if ($this->compiler->isParentTemplate($filePath)) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param string $filePath |
||
| 440 | */ |
||
| 441 | private function deletionWatcher($filePath) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Prepare the Stakx environment by creating necessary cache folders. |
||
| 447 | */ |
||
| 448 | private function createFolderStructure() |
||
| 465 | |||
| 466 | private function configureHighlighter() |
||
| 493 | } |
||
| 494 |
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.