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 Apps 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 Apps, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Apps |
||
15 | { |
||
16 | public static function getAll() |
||
40 | |||
41 | public static function getModules($type, $filter_vendor_app = null, $filter = null) |
||
42 | { |
||
43 | $result = []; |
||
44 | |||
45 | if (!Registry::exists('ModuleType' . $type)) { |
||
46 | $class = 'OSC\OM\Modules\\' . $type; |
||
47 | |||
48 | if (!class_exists($class)) { |
||
49 | trigger_error('OSC\OM\Apps::getModules(): ' . $type . ' module class not found in OSC\OM\Modules\\'); |
||
50 | |||
51 | return $result; |
||
52 | } |
||
53 | |||
54 | Registry::set('ModuleType' . $type, new $class()); |
||
55 | } |
||
56 | |||
57 | $OSCOM_Type = Registry::get('ModuleType' . $type); |
||
58 | |||
59 | $filter_vendor = $filter_app = null; |
||
60 | |||
61 | View Code Duplication | if (isset($filter_vendor_app)) { |
|
|
|||
62 | if (strpos($filter_vendor_app, '\\') !== false) { |
||
63 | list($filter_vendor, $filter_app) = explode('\\', $filter_vendor_app, 2); |
||
64 | } else { |
||
65 | $filter_vendor = $filter_vendor_app; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | $vendor_directory = OSCOM::BASE_DIR . 'Apps'; |
||
70 | |||
71 | if (is_dir($vendor_directory)) { |
||
72 | if ($vdir = new \DirectoryIterator($vendor_directory)) { |
||
73 | foreach ($vdir as $vendor) { |
||
74 | if (!$vendor->isDot() && $vendor->isDir() && (!isset($filter_vendor) || ($vendor->getFilename() == $filter_vendor))) { |
||
75 | if ($adir = new \DirectoryIterator($vendor->getPath() . '/' . $vendor->getFilename())) { |
||
76 | foreach ($adir as $app) { |
||
77 | if (!$app->isDot() && $app->isDir() && (!isset($filter_app) || ($app->getFilename() == $filter_app)) && static::exists($vendor->getFilename() . '\\' . $app->getFilename()) && (($json = static::getInfo($vendor->getFilename() . '\\' . $app->getFilename())) !== false)) { |
||
78 | if (isset($json['modules'][$type])) { |
||
79 | $modules = $json['modules'][$type]; |
||
80 | |||
81 | if (isset($filter)) { |
||
82 | $modules = $OSCOM_Type->filter($modules, $filter); |
||
83 | } |
||
84 | |||
85 | foreach ($modules as $key => $data) { |
||
86 | $result = array_merge($result, $OSCOM_Type->getInfo($vendor->getFilename() . '\\' . $app->getFilename(), $key, $data)); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return $result; |
||
98 | } |
||
99 | |||
100 | public static function exists($app) |
||
118 | |||
119 | public static function getModuleClass($module, $type) |
||
120 | { |
||
121 | if (!Registry::exists('ModuleType' . $type)) { |
||
122 | $class = 'OSC\OM\Modules\\' . $type; |
||
123 | |||
124 | if (!class_exists($class)) { |
||
125 | trigger_error('OSC\OM\Apps::getModuleClass(): ' . $type . ' module class not found in OSC\OM\Modules\\'); |
||
126 | |||
127 | return false; |
||
128 | } |
||
129 | |||
130 | Registry::set('ModuleType' . $type, new $class()); |
||
131 | } |
||
132 | |||
133 | $OSCOM_Type = Registry::get('ModuleType' . $type); |
||
134 | |||
135 | return $OSCOM_Type->getClass($module); |
||
136 | } |
||
137 | |||
138 | public static function getInfo($app) |
||
156 | |||
157 | public static function getRouteDestination($route = null, $filter_vendor_app = null) |
||
204 | } |
||
205 |
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.