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 |
||
22 | class Repository |
||
23 | { |
||
24 | protected $storagePath; |
||
25 | |||
26 | protected $fileBasedSubsets = array('sitemap', 'applicationComponents', 'documentTypes', 'bricks', 'imageSet', 'images', 'files', 'users'); |
||
27 | |||
28 | protected $sitemap; |
||
29 | protected $sitemapChanges = false; |
||
30 | |||
31 | protected $applicationComponents; |
||
32 | protected $applicationComponentsChanges = false; |
||
33 | |||
34 | protected $documentTypes; |
||
35 | protected $documentTypesChanges = false; |
||
36 | |||
37 | protected $bricks; |
||
38 | protected $bricksChanges = false; |
||
39 | |||
40 | protected $imageSet; |
||
41 | protected $imageSetChanges = false; |
||
42 | |||
43 | protected $images; |
||
44 | protected $imagesChanges = false; |
||
45 | |||
46 | protected $files; |
||
47 | protected $filesChanges = false; |
||
48 | |||
49 | protected $users; |
||
50 | protected $usersChanges = false; |
||
51 | |||
52 | protected $contentDbHandle; |
||
53 | |||
54 | /** |
||
55 | * Repository constructor. |
||
56 | * @param $storagePath |
||
57 | * @throws \Exception |
||
58 | */ |
||
59 | public function __construct($storagePath) |
||
60 | { |
||
61 | $storagePath = realpath($storagePath); |
||
62 | if (is_dir($storagePath) && $storagePath !== false) { |
||
63 | $this->storagePath = $storagePath; |
||
64 | } else { |
||
65 | throw new \Exception('Repository not yet initialized.'); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Creates the folder in which to create all storage related files |
||
71 | * |
||
72 | * @param $storagePath |
||
73 | * @return bool |
||
74 | */ |
||
75 | public static function create($storagePath) |
||
76 | { |
||
77 | return mkdir($storagePath); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Initiates default storage |
||
82 | * @throws \Exception |
||
83 | */ |
||
84 | public function init() |
||
85 | { |
||
86 | $storageDefaultPath = realpath('../library/cc/install/_storage.json'); |
||
87 | $contentSqlPath = realpath('../library/cc/install/content.sql'); |
||
88 | |||
89 | $this->initConfigStorage($storageDefaultPath); |
||
90 | $this->initContentDb($contentSqlPath); |
||
91 | |||
92 | $this->save(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Load filebased subset and return it's contents |
||
97 | * |
||
98 | * @param $name |
||
99 | * @return mixed|string |
||
100 | * @throws \Exception |
||
101 | */ |
||
102 | public function __get($name) |
||
103 | { |
||
104 | if (isset($this->$name)) { |
||
105 | View Code Duplication | if (in_array($name, $this->fileBasedSubsets)) { |
|
|
|||
106 | return $this->$name; |
||
107 | } else { |
||
108 | throw new \Exception('Trying to get undefined property from Repository: ' . $name); |
||
109 | } |
||
110 | View Code Duplication | } else { |
|
111 | if (in_array($name, $this->fileBasedSubsets)) { |
||
112 | return $this->loadSubset($name); |
||
113 | } else { |
||
114 | throw new \Exception('Trying to get undefined property from Repository: ' . $name); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Set filebased subset contents |
||
121 | * @param $name |
||
122 | * @param $value |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | public function __set($name, $value) |
||
126 | { |
||
127 | if (in_array($name, $this->fileBasedSubsets)) { |
||
128 | $this->$name = $value; |
||
129 | $changes = $name . 'Changes'; |
||
130 | $this->$changes = true; |
||
131 | } else { |
||
132 | throw new \Exception('Trying to persist unknown subset in repository: ' . $name . ' <br /><pre>' . print_r($value, true) . '</pre>'); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Persist all subsets |
||
138 | */ |
||
139 | public function save() |
||
140 | { |
||
141 | array_map(function ($value) { |
||
142 | $this->saveSubset($value); |
||
143 | }, $this->fileBasedSubsets); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Persist subset to disk |
||
148 | * @param $subset |
||
149 | */ |
||
150 | protected function saveSubset($subset) |
||
151 | { |
||
152 | $changes = $subset . 'Changes'; |
||
153 | if ($this->$changes === true) { |
||
154 | $json = json_encode($this->$subset, JSON_PRETTY_PRINT); |
||
155 | $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json'; |
||
156 | file_put_contents($subsetStoragePath, $json); |
||
157 | |||
158 | $this->$changes = false; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Load subset from disk |
||
164 | * @param $subset |
||
165 | * @return mixed|string |
||
166 | */ |
||
167 | protected function loadSubset($subset) |
||
175 | |||
176 | /** |
||
177 | * @param $contentSqlPath |
||
178 | */ |
||
179 | protected function initContentDb($contentSqlPath) |
||
185 | |||
186 | /** |
||
187 | * @param $storageDefaultPath |
||
188 | */ |
||
189 | protected function initConfigStorage($storageDefaultPath) |
||
210 | |||
211 | /** |
||
212 | * @return \PDO |
||
213 | */ |
||
214 | public function getContentDbHandle() |
||
221 | |||
222 | /** |
||
223 | * Get all documents |
||
224 | * @return array |
||
225 | */ |
||
226 | public function getDocuments() |
||
227 | { |
||
228 | return $this->getDocumentsByPath('/'); |
||
230 | |||
231 | /** |
||
232 | * Get all documents and folders in a certain path |
||
233 | * @param $folderPath |
||
234 | * @return array |
||
235 | * @throws \Exception |
||
236 | */ |
||
237 | public function getDocumentsByPath($folderPath) |
||
260 | |||
261 | |||
262 | /** |
||
263 | * @param $path |
||
264 | * @return bool|Document |
||
265 | */ |
||
266 | public function getDocumentContainerByPath($path) |
||
280 | |||
281 | /** |
||
282 | * @param $path |
||
283 | * @return bool|Document |
||
284 | */ |
||
285 | public function getDocumentByPath($path) |
||
298 | |||
299 | /** |
||
300 | * Returns the count of all documents stored in the db |
||
301 | * @return int |
||
302 | */ |
||
303 | public function getTotalDocumentCount() |
||
316 | |||
317 | /** |
||
318 | * Return the results of the query as array of Documents |
||
319 | * @param $sql |
||
320 | * @return array |
||
321 | * @throws \Exception |
||
322 | */ |
||
323 | protected function fetchAllDocuments($sql) |
||
328 | |||
329 | /** |
||
330 | * Return the result of the query as Document |
||
331 | * @param $sql |
||
332 | * @return mixed |
||
333 | * @throws \Exception |
||
334 | */ |
||
335 | protected function fetchDocument($sql) |
||
340 | |||
341 | /** |
||
342 | * Prepare the sql statement |
||
343 | * @param $sql |
||
344 | * @return \PDOStatement |
||
345 | * @throws \Exception |
||
346 | */ |
||
347 | protected function getDbStatement($sql) |
||
358 | |||
359 | /** |
||
360 | * Create a (non-existent) root folder Document and return it |
||
361 | * @return Document |
||
362 | */ |
||
363 | protected function getRootFolder() |
||
370 | |||
371 | /** |
||
372 | * Save the document to the database |
||
373 | * @param Document $documentObject |
||
374 | * @return bool |
||
375 | * @throws \Exception |
||
376 | * @internal param $path |
||
377 | */ |
||
378 | public function saveDocument($documentObject) |
||
402 | |||
403 | /** |
||
404 | * Delete the document from the database |
||
405 | * If it's a folder, also delete it's contents |
||
406 | * @param $path |
||
407 | * @throws \Exception |
||
408 | */ |
||
409 | public function deleteDocumentByPath($path) |
||
432 | } |
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.