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 Repository 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 Repository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Repository |
||
26 | { |
||
27 | protected $storagePath; |
||
28 | |||
29 | protected $fileBasedSubsets = array('sitemap', 'applicationComponents', 'documentTypes', 'bricks', 'imageSet', 'images', 'files', 'users', 'valuelists', 'redirects'); |
||
30 | |||
31 | protected $sitemap; |
||
32 | protected $sitemapChanges = false; |
||
33 | |||
34 | protected $applicationComponents; |
||
35 | protected $applicationComponentsChanges = false; |
||
36 | |||
37 | protected $documentTypes; |
||
38 | protected $documentTypesChanges = false; |
||
39 | |||
40 | protected $bricks; |
||
41 | protected $bricksChanges = false; |
||
42 | |||
43 | protected $imageSet; |
||
44 | protected $imageSetChanges = false; |
||
45 | |||
46 | protected $images; |
||
47 | protected $imagesChanges = false; |
||
48 | |||
49 | protected $files; |
||
50 | protected $filesChanges = false; |
||
51 | |||
52 | protected $users; |
||
53 | protected $usersChanges = false; |
||
54 | |||
55 | protected $valuelists; |
||
56 | protected $valuelistsChanges = false; |
||
57 | |||
58 | protected $redirects; |
||
59 | protected $redirectsChanges = false; |
||
60 | |||
61 | protected $contentDbHandle; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Repository constructor. |
||
66 | * @param $storagePath |
||
67 | * @throws \Exception |
||
68 | */ |
||
69 | public function __construct($storagePath) |
||
78 | |||
79 | /** |
||
80 | * Creates the folder in which to create all storage related files |
||
81 | * |
||
82 | * @param $storagePath |
||
83 | * @return bool |
||
84 | */ |
||
85 | public static function create($storagePath) |
||
89 | |||
90 | /** |
||
91 | * Initiates default storage |
||
92 | * @throws \Exception |
||
93 | */ |
||
94 | public function init() |
||
104 | |||
105 | /** |
||
106 | * Load filebased subset and return it's contents |
||
107 | * |
||
108 | * @param $name |
||
109 | * @return mixed|string |
||
110 | * @throws \Exception |
||
111 | */ |
||
112 | public function __get($name) |
||
128 | |||
129 | /** |
||
130 | * Set filebased subset contents |
||
131 | * @param $name |
||
132 | * @param $value |
||
133 | * @throws \Exception |
||
134 | */ |
||
135 | public function __set($name, $value) |
||
145 | |||
146 | /** |
||
147 | * Persist all subsets |
||
148 | */ |
||
149 | public function save() |
||
156 | |||
157 | /** |
||
158 | * Persist subset to disk |
||
159 | * @param $subset |
||
160 | */ |
||
161 | public function saveSubset($subset) |
||
176 | |||
177 | /** |
||
178 | * Load subset from disk |
||
179 | * @param $subset |
||
180 | * @return mixed|string |
||
181 | */ |
||
182 | protected function loadSubset($subset) |
||
190 | |||
191 | /** |
||
192 | * @param $contentSqlPath |
||
193 | */ |
||
194 | protected function initContentDb($contentSqlPath) |
||
200 | |||
201 | /** |
||
202 | * @param $storageDefaultPath |
||
203 | */ |
||
204 | protected function initConfigStorage($storageDefaultPath) |
||
227 | |||
228 | /** |
||
229 | * @return \PDO |
||
230 | */ |
||
231 | public function getContentDbHandle() |
||
238 | |||
239 | /** |
||
240 | * Get all documents |
||
241 | * |
||
242 | * @param string $state |
||
243 | * |
||
244 | * @return array |
||
245 | * @throws \Exception |
||
246 | */ |
||
247 | public function getDocuments($state = 'published') |
||
254 | |||
255 | public function getDocumentsWithState($folderPath = '/') |
||
290 | |||
291 | /** |
||
292 | * Get all documents and folders in a certain path |
||
293 | * |
||
294 | * @param $folderPath |
||
295 | * @param string $state |
||
296 | * |
||
297 | * @return array |
||
298 | * @throws \Exception |
||
299 | */ |
||
300 | public function getDocumentsByPath($folderPath, $state = 'published') |
||
322 | |||
323 | |||
324 | /** |
||
325 | * @param $path |
||
326 | * @return bool|Document |
||
327 | */ |
||
328 | public function getDocumentContainerByPath($path) |
||
345 | |||
346 | /** |
||
347 | * @param $path |
||
348 | * @param string $state |
||
349 | * |
||
350 | * @return bool|\library\storage\Document |
||
351 | * @throws \Exception |
||
352 | */ |
||
353 | public function getDocumentByPath($path, $state = 'published') |
||
370 | |||
371 | /** |
||
372 | * Returns the count of all documents stored in the db |
||
373 | * |
||
374 | * @param string $state |
||
375 | * |
||
376 | * @return int |
||
377 | * @throws \Exception |
||
378 | */ |
||
379 | public function getTotalDocumentCount($state = 'published') |
||
396 | |||
397 | public function getPublishedDocumentsNoFolders() |
||
398 | { |
||
399 | $db = $this->getContentDbHandle(); |
||
400 | $sql = ' |
||
401 | SELECT * |
||
402 | FROM documents_published |
||
403 | WHERE `type` != "folder" |
||
404 | '; |
||
405 | $stmt = $db->query($sql); |
||
406 | $result = $stmt->fetchAll(\PDO::FETCH_CLASS, '\library\storage\Document'); |
||
407 | View Code Duplication | if ($stmt === false || !$stmt->execute()) { |
|
408 | $errorInfo = $db->errorInfo(); |
||
409 | $errorMsg = $errorInfo[2]; |
||
410 | throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
||
411 | } |
||
412 | return $result; |
||
413 | } |
||
414 | |||
415 | private function publishOrUnpublishDocumentByPath($path, $publish = true) { |
||
416 | if ($publish) { |
||
417 | $sql = ' |
||
418 | INSERT OR REPLACE INTO documents_published |
||
419 | (`id`,`path`,`title`,`slug`,`type`,`documentType`,`documentTypeSlug`,`state`,`lastModificationDate`,`creationDate`,`publicationDate`,`lastModifiedBy`,`fields`,`bricks`,`dynamicBricks`) |
||
420 | SELECT `id`,`path`,`title`,`slug`,`type`,`documentType`,`documentTypeSlug`,"published" as state,`lastModificationDate`,`creationDate`,' . time() . ' as publicationDate, `lastModifiedBy`,`fields`,`bricks`,`dynamicBricks` |
||
421 | FROM documents_unpublished |
||
422 | WHERE `path` = :path |
||
423 | '; |
||
424 | } else { |
||
425 | $sql = 'DELETE FROM documents_published |
||
426 | WHERE `path` = :path'; |
||
427 | } |
||
428 | $db = $this->getContentDbHandle(); |
||
429 | $stmt = $db->prepare($sql); |
||
430 | View Code Duplication | if ($stmt === false) { |
|
431 | $errorInfo = $db->errorInfo(); |
||
432 | $errorMsg = $errorInfo[2]; |
||
433 | throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
||
434 | } |
||
435 | $stmt->bindValue(':path', $path); |
||
436 | $stmt->execute(); |
||
437 | } |
||
438 | |||
439 | public function publishDocumentByPath($path) |
||
443 | |||
444 | public function unpublishDocumentByPath($path) |
||
448 | |||
449 | public function cleanPublishedDeletedDocuments() |
||
462 | |||
463 | /** |
||
464 | * Return the results of the query as array of Documents |
||
465 | * @param $sql |
||
466 | * @return array |
||
467 | * @throws \Exception |
||
468 | */ |
||
469 | protected function fetchAllDocuments($sql) |
||
474 | |||
475 | /** |
||
476 | * Return the result of the query as Document |
||
477 | * @param $sql |
||
478 | * @return mixed |
||
479 | * @throws \Exception |
||
480 | */ |
||
481 | protected function fetchDocument($sql) |
||
486 | |||
487 | /** |
||
488 | * Prepare the sql statement |
||
489 | * @param $sql |
||
490 | * @return \PDOStatement |
||
491 | * @throws \Exception |
||
492 | */ |
||
493 | protected function getDbStatement($sql) |
||
494 | { |
||
495 | $db = $this->getContentDbHandle(); |
||
496 | $stmt = $db->query($sql); |
||
497 | View Code Duplication | if ($stmt === false) { |
|
498 | $errorInfo = $db->errorInfo(); |
||
499 | $errorMsg = $errorInfo[2]; |
||
500 | throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
||
501 | } |
||
502 | return $stmt; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Create a (non-existent) root folder Document and return it |
||
507 | * @return Document |
||
508 | */ |
||
509 | protected function getRootFolder() |
||
516 | |||
517 | /** |
||
518 | * Save the document to the database |
||
519 | * |
||
520 | * @param Document $documentObject |
||
521 | * @param string $state |
||
522 | * |
||
523 | * @return bool |
||
524 | * @throws \Exception |
||
525 | * @internal param $path |
||
526 | */ |
||
527 | public function saveDocument($documentObject, $state = 'published') |
||
554 | |||
555 | /** |
||
556 | * Delete the document from the database |
||
557 | * If it's a folder, also delete it's contents |
||
558 | * |
||
559 | * @param $path |
||
560 | * |
||
561 | * @internal param string $state |
||
562 | * |
||
563 | */ |
||
564 | public function deleteDocumentByPath($path) |
||
587 | |||
588 | /** |
||
589 | * @param $document |
||
590 | * @param $db |
||
591 | * @param $documents |
||
592 | * @param $key |
||
593 | * |
||
594 | * @return mixed |
||
595 | */ |
||
596 | private function setAssetsToDocumentFolders($document, $db, $documents, $key) |
||
606 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.