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 |
||
27 | class Repository |
||
28 | { |
||
29 | protected $storagePath; |
||
30 | |||
31 | protected $fileBasedSubsets = array('sitemap', 'applicationComponents', 'documentTypes', 'bricks', 'imageSet', 'images', 'files', 'users', 'valuelists', 'redirects', 'activityLog'); |
||
32 | |||
33 | protected $sitemap; |
||
34 | protected $sitemapChanges = false; |
||
35 | |||
36 | protected $applicationComponents; |
||
37 | protected $applicationComponentsChanges = false; |
||
38 | |||
39 | protected $documentTypes; |
||
40 | protected $documentTypesChanges = false; |
||
41 | |||
42 | protected $bricks; |
||
43 | protected $bricksChanges = false; |
||
44 | |||
45 | protected $imageSet; |
||
46 | protected $imageSetChanges = false; |
||
47 | |||
48 | protected $images; |
||
49 | protected $imagesChanges = false; |
||
50 | |||
51 | protected $files; |
||
52 | protected $filesChanges = false; |
||
53 | |||
54 | protected $users; |
||
55 | protected $usersChanges = false; |
||
56 | |||
57 | protected $valuelists; |
||
58 | protected $valuelistsChanges = false; |
||
59 | |||
60 | protected $redirects; |
||
61 | protected $redirectsChanges = false; |
||
62 | |||
63 | protected $activityLog; |
||
64 | protected $activityLogChanges = false; |
||
65 | |||
66 | protected $contentDbHandle; |
||
67 | |||
68 | /** |
||
69 | * @var ContentRepository |
||
70 | */ |
||
71 | protected $contentRepository; |
||
72 | |||
73 | |||
74 | /** |
||
75 | * Repository constructor. |
||
76 | * @param $storagePath |
||
77 | * @throws \Exception |
||
78 | */ |
||
79 | public function __construct($storagePath) |
||
80 | { |
||
81 | $storagePath = realpath($storagePath); |
||
82 | if (is_dir($storagePath) && $storagePath !== false) { |
||
83 | $this->storagePath = $storagePath; |
||
84 | $this->contentRepository = new ContentRepository($storagePath); |
||
85 | } else { |
||
86 | throw new \Exception('Repository not yet initialized.'); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Initiates default storage |
||
92 | * @param $baseStorageDefaultPath |
||
93 | * @param $baseStorageSqlPath |
||
94 | */ |
||
95 | public function init($baseStorageDefaultPath, $baseStorageSqlPath) |
||
105 | |||
106 | /** |
||
107 | * Load filebased subset and return it's contents |
||
108 | * |
||
109 | * @param $name |
||
110 | * @return mixed|string |
||
111 | * @throws \Exception |
||
112 | */ |
||
113 | public function __get($name) |
||
129 | |||
130 | /** |
||
131 | * Set filebased subset contents |
||
132 | * @param $name |
||
133 | * @param $value |
||
134 | * @throws \Exception |
||
135 | */ |
||
136 | public function __set($name, $value) |
||
146 | |||
147 | /** |
||
148 | * Persist all subsets |
||
149 | */ |
||
150 | public function save() |
||
157 | |||
158 | /** |
||
159 | * Persist subset to disk |
||
160 | * @param $subset |
||
161 | */ |
||
162 | public function saveSubset($subset) |
||
177 | |||
178 | /** |
||
179 | * Load subset from disk |
||
180 | * @param $subset |
||
181 | * @return mixed|string |
||
182 | */ |
||
183 | protected function loadSubset($subset) |
||
191 | |||
192 | /** |
||
193 | * @param $contentSqlPath |
||
194 | */ |
||
195 | protected function initContentDb($contentSqlPath) |
||
201 | |||
202 | /** |
||
203 | * @param $storageDefaultPath |
||
204 | */ |
||
205 | protected function initConfigStorage($storageDefaultPath) |
||
221 | |||
222 | /** |
||
223 | * @return \PDO |
||
224 | */ |
||
225 | View Code Duplication | protected function getContentDbHandle() |
|
232 | |||
233 | private function initConfigIfNotExists($json, $subsetName) |
||
244 | |||
245 | /** |
||
246 | * @return ContentRepository |
||
247 | */ |
||
248 | public function getContentRepository() |
||
252 | } |
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.