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 |
||
8 | class Helper |
||
9 | { |
||
10 | /** |
||
11 | * @var object |
||
12 | */ |
||
13 | protected $config; |
||
14 | |||
15 | /** |
||
16 | * Rinvex constructor. |
||
17 | * |
||
18 | * @param object $config |
||
19 | */ |
||
20 | 34 | public function __construct($config) |
|
21 | { |
||
22 | 34 | $this->config = $config; |
|
23 | 34 | } |
|
24 | |||
25 | /** |
||
26 | * Load a file from disk. |
||
27 | * |
||
28 | * @param $file |
||
29 | * @return null|string |
||
30 | */ |
||
31 | 34 | public function loadFile($file) |
|
32 | { |
||
33 | 34 | if (file_exists($file)) { |
|
34 | 34 | return $this->sanitizeFile(file_get_contents($file)); |
|
35 | } |
||
36 | 9 | } |
|
37 | |||
38 | /** |
||
39 | * Loads a json file. |
||
40 | * |
||
41 | * @param $file |
||
42 | * @param string $dir |
||
43 | * @return \PragmaRX\Coollection\Package\Coollection |
||
44 | * @throws Exception |
||
45 | */ |
||
46 | 34 | public function loadJson($file, $dir = null) |
|
47 | { |
||
48 | 34 | if (empty($file)) { |
|
49 | throw new Exception('loadJson Error: File name not set'); |
||
50 | } |
||
51 | |||
52 | 34 | if (! file_exists($file) && ! file_exists($file = $this->dataDir("/$dir/".strtolower($file).'.json'))) { |
|
53 | 9 | return coollect(); |
|
54 | } |
||
55 | |||
56 | 34 | $decoded = json5_decode($this->loadFile($file), true); |
|
57 | |||
58 | 34 | if (\is_null($decoded)) { |
|
59 | throw new Exception("Error decoding json file: $file"); |
||
60 | } |
||
61 | |||
62 | 34 | return coollect($decoded); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Load json files from dir. |
||
67 | * |
||
68 | * @param $dir |
||
69 | * @return \PragmaRX\Coollection\Package\Coollection |
||
70 | */ |
||
71 | 34 | View Code Duplication | public function loadJsonFiles($dir) |
|
|||
72 | { |
||
73 | return coollect(glob("$dir/*.json*"))->mapWithKeys(function ($file) { |
||
74 | 34 | $key = str_replace(['.json5', '.json'], '', basename($file)); |
|
75 | |||
76 | 34 | return [$key => $this->loadJson($file)]; |
|
77 | 34 | }); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Move files using wildcard filter. |
||
82 | * |
||
83 | * @param $from |
||
84 | * @param $to |
||
85 | */ |
||
86 | public function moveFilesWildcard($from, $to) |
||
94 | |||
95 | /** |
||
96 | * Get data directory. |
||
97 | * |
||
98 | * @param $path |
||
99 | * @return string |
||
100 | */ |
||
101 | 34 | public function dataDir($path = '') |
|
107 | |||
108 | /** |
||
109 | * @param $contents |
||
110 | * @return string |
||
111 | */ |
||
112 | 34 | public function sanitizeFile($contents) |
|
116 | |||
117 | /** |
||
118 | * Check if array is multidimensional. |
||
119 | * |
||
120 | * @param $string |
||
121 | * @return string |
||
122 | */ |
||
123 | 34 | public function toDir($string) |
|
127 | } |
||
128 |
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.