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 Api 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 Api, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Api implements \Potherca\Flysystem\Github\ApiInterface |
||
17 | { |
||
18 | ////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
||
19 | const ERROR_NO_NAME = 'Could not set name for entry'; |
||
20 | const ERROR_NOT_FOUND = 'Not Found'; |
||
21 | |||
22 | const API_GIT_DATA = 'gitData'; |
||
23 | const API_REPOSITORY = 'repo'; |
||
24 | const API_REPOSITORY_COMMITS = 'commits'; |
||
25 | const API_REPOSITORY_CONTENTS = 'contents'; |
||
26 | |||
27 | const KEY_BLOB = 'blob'; |
||
28 | const KEY_DIRECTORY = 'dir'; |
||
29 | const KEY_FILE = 'file'; |
||
30 | const KEY_FILENAME = 'basename'; |
||
31 | const KEY_MODE = 'mode'; |
||
32 | const KEY_NAME = 'name'; |
||
33 | const KEY_PATH = 'path'; |
||
34 | const KEY_SHA = 'sha'; |
||
35 | const KEY_SIZE = 'size'; |
||
36 | const KEY_STREAM = 'stream'; |
||
37 | const KEY_TIMESTAMP = 'timestamp'; |
||
38 | const KEY_TREE = 'tree'; |
||
39 | const KEY_TYPE = 'type'; |
||
40 | const KEY_VISIBILITY = 'visibility'; |
||
41 | |||
42 | const GITHUB_API_URL = 'https://api.github.com'; |
||
43 | const GITHUB_URL = 'https://github.com'; |
||
44 | |||
45 | const MIME_TYPE_DIRECTORY = 'directory'; // or application/x-directory |
||
46 | |||
47 | const NOT_RECURSIVE = false; |
||
48 | const RECURSIVE = true; |
||
49 | |||
50 | /** @var ApiInterface[] */ |
||
51 | private $apiCollection = []; |
||
52 | /** @var Client */ |
||
53 | private $client; |
||
54 | /** @var array */ |
||
55 | private $commits = []; |
||
56 | /** @var bool */ |
||
57 | private $isAuthenticationAttempted = false; |
||
58 | /** @var array */ |
||
59 | private $metadata = []; |
||
60 | /** @var SettingsInterface */ |
||
61 | private $settings; |
||
62 | |||
63 | //////////////////////////// SETTERS AND GETTERS \\\\\\\\\\\\\\\\\\\\\\\\\\\ |
||
64 | /** |
||
65 | * @param string $name |
||
66 | * |
||
67 | * @return \Github\Api\ApiInterface |
||
68 | * |
||
69 | * @throws \Github\Exception\InvalidArgumentException |
||
70 | */ |
||
71 | View Code Duplication | private function getApi($name) |
|
81 | |||
82 | /** |
||
83 | * @param $name |
||
84 | * @param $api |
||
85 | * @return ApiInterface |
||
86 | */ |
||
87 | View Code Duplication | private function getApiFrom($name, $api) |
|
94 | |||
95 | /** |
||
96 | * @return \Github\Api\Repository\Commits |
||
97 | * |
||
98 | * @throws \Github\Exception\InvalidArgumentException |
||
99 | */ |
||
100 | private function getCommitsApi() |
||
104 | |||
105 | /** |
||
106 | * @return \Github\Api\Repository\Contents |
||
107 | * |
||
108 | * @throws \Github\Exception\InvalidArgumentException |
||
109 | */ |
||
110 | private function getContentApi() |
||
114 | |||
115 | /** |
||
116 | * @return GitData |
||
117 | * |
||
118 | * @throws \Github\Exception\InvalidArgumentException |
||
119 | */ |
||
120 | private function getGitDataApi() |
||
124 | |||
125 | /** |
||
126 | * @return Repo |
||
127 | * |
||
128 | * @throws \Github\Exception\InvalidArgumentException |
||
129 | */ |
||
130 | private function getRepositoryApi() |
||
134 | |||
135 | //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
||
136 | final public function __construct(Client $client, SettingsInterface $settings) |
||
145 | |||
146 | /** |
||
147 | * @param string $path |
||
148 | * |
||
149 | * @return bool |
||
150 | * |
||
151 | * @throws \Github\Exception\InvalidArgumentException |
||
152 | */ |
||
153 | View Code Duplication | final public function exists($path) |
|
164 | |||
165 | /** |
||
166 | * @param $path |
||
167 | * |
||
168 | * @return null|string |
||
169 | * @throws \Github\Exception\InvalidArgumentException |
||
170 | * |
||
171 | * @throws \Github\Exception\ErrorException |
||
172 | */ |
||
173 | View Code Duplication | final public function getFileContents($path) |
|
184 | |||
185 | /** |
||
186 | * @param string $path |
||
187 | * |
||
188 | * @return array |
||
189 | * |
||
190 | * @throws \Github\Exception\InvalidArgumentException |
||
191 | */ |
||
192 | final public function getLastUpdatedTimestamp($path) |
||
196 | |||
197 | /** |
||
198 | * @param string $path |
||
199 | * |
||
200 | * @return array |
||
201 | * |
||
202 | * @throws \Github\Exception\InvalidArgumentException |
||
203 | */ |
||
204 | final public function getCreatedTimestamp($path) |
||
208 | |||
209 | /** |
||
210 | * @param string $path |
||
211 | * |
||
212 | * @return array|bool |
||
213 | * |
||
214 | * @throws \Github\Exception\InvalidArgumentException |
||
215 | * @throws \Github\Exception\RuntimeException |
||
216 | * @throws \League\Flysystem\NotSupportedException |
||
217 | */ |
||
218 | final public function getMetaData($path) |
||
247 | |||
248 | /** |
||
249 | * @param string $path |
||
250 | * @param bool $recursive |
||
251 | * |
||
252 | * @return array |
||
253 | * |
||
254 | * @throws \Github\Exception\InvalidArgumentException |
||
255 | */ |
||
256 | final public function getDirectoryContents($path, $recursive) |
||
277 | |||
278 | /** |
||
279 | * @param string $path |
||
280 | * |
||
281 | * @return null|string |
||
282 | * |
||
283 | * @throws \Github\Exception\ErrorException |
||
284 | * @throws \Github\Exception\InvalidArgumentException |
||
285 | * @throws \Github\Exception\RuntimeException |
||
286 | */ |
||
287 | final public function guessMimeType($path) |
||
304 | |||
305 | ////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
||
306 | /** |
||
307 | * |
||
308 | * @throws \Github\Exception\InvalidArgumentException If no authentication method was given |
||
309 | */ |
||
310 | private function assureAuthenticated() |
||
330 | |||
331 | /** |
||
332 | * @param array $tree |
||
333 | * @param string $path |
||
334 | * @param bool $recursive |
||
335 | * |
||
336 | * @return array |
||
337 | */ |
||
338 | private function filterTreeData(array $tree, $path, $recursive) |
||
358 | |||
359 | /** |
||
360 | * @param $permissions |
||
361 | * @return string |
||
362 | */ |
||
363 | private function guessVisibility($permissions) |
||
373 | |||
374 | /** |
||
375 | * @param array $treeData |
||
376 | * |
||
377 | * @return array |
||
378 | * |
||
379 | * @throws \Github\Exception\InvalidArgumentException |
||
380 | */ |
||
381 | private function normalizeTreeData($treeData) |
||
400 | |||
401 | /** |
||
402 | * @param $path |
||
403 | * |
||
404 | * @return array |
||
405 | * |
||
406 | * @throws \Github\Exception\InvalidArgumentException |
||
407 | */ |
||
408 | private function commitsForFile($path) |
||
423 | |||
424 | /** |
||
425 | * @param array $entry |
||
426 | * @param string $key |
||
427 | * @param bool $default |
||
428 | * |
||
429 | * @return mixed |
||
430 | */ |
||
431 | private function setDefaultValue(array &$entry, $key, $default = false) |
||
437 | |||
438 | /** |
||
439 | * @param $entry |
||
440 | */ |
||
441 | private function setEntryType(&$entry) |
||
458 | |||
459 | /** |
||
460 | * @param $entry |
||
461 | */ |
||
462 | private function setEntryVisibility(&$entry) |
||
471 | |||
472 | /** |
||
473 | * @param $entry |
||
474 | */ |
||
475 | private function setEntryName(&$entry) |
||
487 | |||
488 | /** |
||
489 | * @param $metadata |
||
490 | * @return bool |
||
491 | */ |
||
492 | private function isMetadataForDirectory($metadata) |
||
506 | |||
507 | /** |
||
508 | * @param $subject |
||
509 | * @param $key |
||
510 | * @return mixed |
||
511 | */ |
||
512 | private function hasKey(&$subject, $key) |
||
523 | |||
524 | /** |
||
525 | * @param array $treeMetadata |
||
526 | * @param $path |
||
527 | * |
||
528 | * @return int |
||
529 | * |
||
530 | * @throws \Github\Exception\InvalidArgumentException |
||
531 | */ |
||
532 | private function getDirectoryTimestamp(array $treeMetadata, $path) |
||
553 | |||
554 | private function normalizePathName($path) |
||
558 | |||
559 | /** |
||
560 | * @param $path |
||
561 | * @return array |
||
562 | * @throws \League\Flysystem\NotSupportedException |
||
563 | * @throws \Github\Exception\RuntimeException |
||
564 | * |
||
565 | * @throws \Github\Exception\InvalidArgumentException |
||
566 | */ |
||
567 | private function metadataForDirectory($path) |
||
608 | |||
609 | /** |
||
610 | * @param array $treeData |
||
611 | * |
||
612 | * @return array |
||
613 | * |
||
614 | * @throws \Github\Exception\InvalidArgumentException |
||
615 | */ |
||
616 | private function addTimestamps(array $treeData) |
||
630 | |||
631 | /** |
||
632 | * @param $path |
||
633 | * @param $function |
||
634 | * @return array |
||
635 | */ |
||
636 | private function filterCommits($path, callable $function) |
||
648 | } |
||
649 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: