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 |
||
| 30 | class SwagConnect extends Plugin |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var SubscriberRegistration |
||
| 34 | */ |
||
| 35 | private $subscriberRegistration; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var ConnectFactory |
||
| 39 | */ |
||
| 40 | private $connectFactory; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Returns the current version of the plugin. |
||
| 44 | * |
||
| 45 | * @throws \Exception |
||
| 46 | * @return string|void |
||
| 47 | */ |
||
| 48 | public function getVersion() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns a nice name for plugin manager list |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public function getLabel() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | public function getInfo() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Install plugin method |
||
| 83 | * |
||
| 84 | * @param $context InstallContext |
||
| 85 | * @throws \RuntimeException |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | public function install(InstallContext $context) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param $context UpdateContext |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function update(UpdateContext $context) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Uninstall plugin method |
||
| 118 | * @param $context UninstallContext |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function uninstall(UninstallContext $context) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Performs the default setup of the system. |
||
| 130 | * |
||
| 131 | * This can be used by the update as well as by the install method |
||
| 132 | * |
||
| 133 | * @param InstallContext $context |
||
| 134 | * @param bool $fullSetup |
||
| 135 | * @throws RuntimeException |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function doSetup($context, $fullSetup = true) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Performs the update of the system |
||
| 154 | * |
||
| 155 | * @param $version |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function doUpdate($version) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Uninstall the plugin |
||
| 172 | * @param $context UninstallContext |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function doUninstall($context) |
|
| 190 | |||
| 191 | public static function getSubscribedEvents() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Will dynamically register all needed events |
||
| 202 | * |
||
| 203 | * @param Enlight_Event_EventArgs $args |
||
| 204 | */ |
||
| 205 | public function onStartDispatch(\Enlight_Event_EventArgs $args) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return ArrayCollection |
||
| 214 | */ |
||
| 215 | public function onConsoleAddCommand() |
||
| 225 | |||
| 226 | public function onInitResourceSDK() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Lazy getter for the connectFactory |
||
| 233 | * |
||
| 234 | * @return ConnectFactory |
||
| 235 | */ |
||
| 236 | public function getConnectFactory() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return \Shopware\Connect\SDK |
||
| 247 | */ |
||
| 248 | public function getSDK() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return \ShopwarePlugins\Connect\Components\Helper |
||
| 255 | */ |
||
| 256 | public function getHelper() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return BasketHelper |
||
| 263 | */ |
||
| 264 | public function getBasketHelper() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return \ShopwarePlugins\Connect\Components\Config |
||
| 271 | */ |
||
| 272 | public function getConfigComponents() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | private function isInstalled() |
||
| 298 | |||
| 299 | private function registerSubscribers() |
||
| 317 | } |
||
| 318 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.