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 |
||
| 29 | class Plugin implements PluginInterface, EventSubscriberInterface, Capable { |
||
| 30 | protected $composer; |
||
| 31 | protected $io; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Apply plugin modifications to Composer |
||
| 35 | * |
||
| 36 | * @param Composer $composer |
||
| 37 | * @param IOInterface $io |
||
| 38 | */ |
||
| 39 | public function activate(Composer $composer, IOInterface $io) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return array |
||
|
|
|||
| 49 | */ |
||
| 50 | public function getCapabilities() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * |
||
| 58 | * Events |
||
| 59 | * |
||
| 60 | * Command Events Composer\Script\Event |
||
| 61 | * |
||
| 62 | * pre-install-cmd occurs before the install command is executed with a lock file present. |
||
| 63 | * post-install-cmd occurs after the install command has been executed with a lock file present. |
||
| 64 | * pre-update-cmd occurs before the update command is executed, or before the install command is executed without a lock file present. |
||
| 65 | * post-update-cmd occurs after the update command has been executed, or after the install command has been executed without a lock file present. |
||
| 66 | * post-status-cmd occurs after the status command has been executed. |
||
| 67 | * pre-archive-cmd occurs before the archive command is executed. |
||
| 68 | * post-archive-cmd occurs after the archive command has been executed. |
||
| 69 | * pre-autoload-dump occurs before the autoloader is dumped, either during install/update, or via the dump-autoload command. |
||
| 70 | * post-autoload-dump occurs after the autoloader has been dumped, either during install/update, or via the dump-autoload command. |
||
| 71 | * post-root-package-install occurs after the root package has been installed, during the create-project command. |
||
| 72 | * post-create-project-cmd occurs after the create-project command has been executed. |
||
| 73 | * |
||
| 74 | * Installer Events Composer\Installer\InstallerEvent |
||
| 75 | * |
||
| 76 | * pre-dependencies-solving occurs before the dependencies are resolved. |
||
| 77 | * post-dependencies-solving occurs after the dependencies have been resolved. |
||
| 78 | * |
||
| 79 | * Package Events Composer\Installer\PackageEvent |
||
| 80 | * |
||
| 81 | * pre-package-install occurs before a package is installed. |
||
| 82 | * post-package-install occurs after a package has been installed. |
||
| 83 | * pre-package-update occurs before a package is updated. |
||
| 84 | * post-package-update occurs after a package has been updated. |
||
| 85 | * pre-package-uninstall occurs before a package is uninstalled. |
||
| 86 | * post-package-uninstall occurs after a package has been uninstalled. |
||
| 87 | * |
||
| 88 | * Plugin Events Composer\Plugin\PluginEvents |
||
| 89 | * |
||
| 90 | * init occurs after a Composer instance is done being initialized. |
||
| 91 | * command occurs before any Composer Command is executed on the CLI. It provides you with access to the input and output objects of the program. |
||
| 92 | * pre-file-download occurs before files are downloaded and allows you to manipulate the RemoteFilesystem object prior to downloading files based on the URL to be downloaded. |
||
| 93 | */ |
||
| 94 | public static function getSubscribedEvents() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param PreFileDownloadEvent $event |
||
| 104 | */ |
||
| 105 | public function onPreFileDownload(PreFileDownloadEvent $event) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * An event that triggers setting writable permissions on any directories specified in the writable-dirs composer extra options |
||
| 116 | * |
||
| 117 | * @param Event $event |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public static function setPermissions(Event $event) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * returns a list of writeable directories specified in the writeable-dirs composer extra options |
||
| 143 | * |
||
| 144 | * @param Event $event |
||
| 145 | * @return array an array of directory paths |
||
| 146 | */ |
||
| 147 | View Code Duplication | public static function getWritableDirs(Event $event) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * returns a list of writeable files specified in the writeable-files composer extra options |
||
| 158 | * |
||
| 159 | * @param Event $event |
||
| 160 | * @return array an array of file paths |
||
| 161 | */ |
||
| 162 | View Code Duplication | public static function getWritableFiles(Event $event) { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Sets Writrable Directory permissions for any directories listed in the writeable-dirs option using setfacl |
||
| 173 | * |
||
| 174 | * @param Event $event |
||
| 175 | */ |
||
| 176 | View Code Duplication | public static function setPermissionsSetfacl(Event $event) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Sets Writrable Directory permissions for any directories listed in the writeable-dirs option using chmod |
||
| 187 | * |
||
| 188 | * @param Event $event |
||
| 189 | */ |
||
| 190 | View Code Duplication | public static function setPermissionsChmod(Event $event) { |
|
| 197 | |||
| 198 | /** |
||
| 199 | * returns the user the webserver is running as |
||
| 200 | * |
||
| 201 | * @param Event $event |
||
| 202 | * @return string the webserver username |
||
| 203 | */ |
||
| 204 | public static function getHttpdUser(Event $event) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * sets the needed permissions for the $http_user and the running user on $path using setfacl |
||
| 216 | * |
||
| 217 | * @param Event $event |
||
| 218 | * @param string $http_user the webserver username |
||
| 219 | * @param string $path the directory to set permissions on |
||
| 220 | * @param string $type optional type of entry, defaults to dir, can be dir or file |
||
| 221 | */ |
||
| 222 | public static function SetfaclPermissionsSetter(Event $event, $http_user, $path, $type = 'dir') { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * sets the needed permissions for the $http_user and the running user on $path using chmod |
||
| 233 | * |
||
| 234 | * @param Event $event |
||
| 235 | * @param string $http_user the webserver username |
||
| 236 | * @param string $path the directory to set permissions on |
||
| 237 | * @param string $type optional type of entry, defaults to dir, can be dir or file |
||
| 238 | */ |
||
| 239 | public static function ChmodPermissionsSetter(Event $event, $http_user, $path, $type = 'dir') { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * checks if the given directory exists and if not tries to create it. |
||
| 250 | * |
||
| 251 | * @param Event $event |
||
| 252 | * @param string $path the directory |
||
| 253 | * @throws \Exception |
||
| 254 | */ |
||
| 255 | public static function EnsureDirExists(Event $event, $path) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * checks if the given file exists and if not tries to create it. |
||
| 267 | * |
||
| 268 | * @param Event $event |
||
| 269 | * @param string $path the directory |
||
| 270 | * @throws \Exception |
||
| 271 | */ |
||
| 272 | public static function EnsureFileExists(Event $event, $path) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * runs a command process returning the output and checking return code |
||
| 285 | * |
||
| 286 | * @param Event $event |
||
| 287 | * @param string $commandline the command line to run |
||
| 288 | * @return string the output |
||
| 289 | * @throws \Exception |
||
| 290 | */ |
||
| 291 | public static function runProcess(Event $event, $commandline) { |
||
| 299 | } |
||
| 300 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.