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 PluginApiService 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 PluginApiService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class PluginApiService |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Url for Api |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $apiUrl; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var EccubeConfig |
||
| 36 | */ |
||
| 37 | private $eccubeConfig; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var RequestStack |
||
| 41 | */ |
||
| 42 | private $requestStack; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var BaseInfo |
||
| 46 | */ |
||
| 47 | private $BaseInfo; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var PluginRepository |
||
| 51 | */ |
||
| 52 | private $pluginRepository; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * PluginApiService constructor. |
||
| 56 | * |
||
| 57 | * @param EccubeConfig $eccubeConfig |
||
| 58 | * @param RequestStack $requestStack |
||
| 59 | * @param BaseInfoRepository $baseInfoRepository |
||
| 60 | * @param PluginRepository $pluginRepository |
||
| 61 | * |
||
| 62 | * @throws \Doctrine\ORM\NoResultException |
||
| 63 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 64 | */ |
||
| 65 | public function __construct(EccubeConfig $eccubeConfig, RequestStack $requestStack, BaseInfoRepository $baseInfoRepository, PluginRepository $pluginRepository) |
||
| 66 | { |
||
| 67 | $this->eccubeConfig = $eccubeConfig; |
||
| 68 | $this->requestStack = $requestStack; |
||
| 69 | $this->BaseInfo = $baseInfoRepository->get(); |
||
| 70 | $this->pluginRepository = $pluginRepository; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return mixed |
||
| 75 | */ |
||
| 76 | public function getApiUrl() |
||
| 77 | { |
||
| 78 | if (empty($this->apiUrl)) { |
||
| 79 | return $this->eccubeConfig->get('eccube_package_api_url'); |
||
| 80 | } |
||
| 81 | |||
| 82 | return $this->apiUrl; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param mixed $apiUrl |
||
| 87 | */ |
||
| 88 | public function setApiUrl($apiUrl) |
||
| 89 | { |
||
| 90 | $this->apiUrl = $apiUrl; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get master data: category |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | public function getCategory() |
||
| 99 | { |
||
| 100 | try { |
||
| 101 | $urlCategory = $this->getApiUrl().'/category'; |
||
| 102 | |||
| 103 | return $this->requestApi($urlCategory); |
||
| 104 | } catch (PluginApiException $e) { |
||
| 105 | return []; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get plugins list |
||
| 111 | * |
||
| 112 | * @param $data |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | * |
||
| 116 | * @throws PluginApiException |
||
| 117 | */ |
||
| 118 | public function getPlugins($data) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get purchased plugins list |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | * |
||
| 143 | * @throws PluginApiException |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function getPurchased() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Get recommended plugins list |
||
| 157 | * |
||
| 158 | * @return array($result, $info) |
||
| 159 | * |
||
| 160 | * @throws PluginApiException |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function getRecommended() |
|
| 171 | |||
| 172 | private function buildPlugins(&$plugins) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Is update |
||
| 203 | * |
||
| 204 | * @param string $pluginVersion |
||
| 205 | * @param string $remoteVersion |
||
| 206 | * |
||
| 207 | * @return boolean |
||
| 208 | */ |
||
| 209 | private function isUpdate($pluginVersion, $remoteVersion) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get a plugin |
||
| 216 | * |
||
| 217 | * @param int|string $id Id or plugin code |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | * |
||
| 221 | * @throws PluginApiException |
||
| 222 | */ |
||
| 223 | View Code Duplication | public function getPlugin($id) |
|
| 232 | |||
| 233 | public function pluginInstalled(Plugin $Plugin) |
||
| 237 | |||
| 238 | public function pluginEnabled(Plugin $Plugin) |
||
| 242 | |||
| 243 | public function pluginDisabled(Plugin $Plugin) |
||
| 247 | |||
| 248 | public function pluginUninstalled(Plugin $Plugin) |
||
| 252 | |||
| 253 | private function updatePluginStatus($url, Plugin $Plugin) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * API request processing |
||
| 265 | * |
||
| 266 | * @param string $url |
||
| 267 | * @param array $data |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | * |
||
| 271 | * @throws PluginApiException |
||
| 272 | */ |
||
| 273 | public function requestApi($url, $data = [], $post = false) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get plugin information |
||
| 330 | * |
||
| 331 | * @param array $plugin |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function buildInfo(&$plugin) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Check support version |
||
| 344 | * |
||
| 345 | * @param $plugin |
||
| 346 | */ |
||
| 347 | public function supportedVersion(&$plugin) |
||
| 356 | } |
||
| 357 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.