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 |
||
| 23 | final class Verify implements PluginInterface, EventSubscriberInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * {@inheritDoc} |
||
| 27 | */ |
||
| 28 | public static function getSubscribedEvents() : array |
||
| 29 | { |
||
| 30 | return [ |
||
| 31 | ScriptEvents::PRE_AUTOLOAD_DUMP => 'verify', |
||
| 32 | ]; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * {@inheritDoc} |
||
| 37 | * |
||
| 38 | * @codeCoverageIgnore |
||
| 39 | */ |
||
| 40 | public function activate(Composer $composer, IOInterface $io) : void |
||
| 41 | { |
||
| 42 | // Nothing to do here, as all features are provided through event listeners |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param Event $composerEvent |
||
| 47 | * |
||
| 48 | * @throws \Roave\ComposerGpgVerify\Exception\PackagesTrustCheckFailed |
||
| 49 | * @throws \RuntimeException |
||
| 50 | * @throws \Roave\ComposerGpgVerify\Exception\PreferredInstallIsNotSource |
||
| 51 | */ |
||
| 52 | public static function verify(Event $composerEvent) : void |
||
|
|
|||
| 53 | { |
||
| 54 | $originalLanguage = getenv('LANGUAGE'); |
||
| 55 | $io = $composerEvent->getIO(); |
||
| 56 | $composer = $composerEvent->getComposer(); |
||
| 57 | $config = $composer->getConfig(); |
||
| 58 | |||
| 59 | $io->write('<info>roave/composer-gpg-verify:</info> Analysing downloaded packages...'); |
||
| 60 | |||
| 61 | self::assertSourceInstallation($config); |
||
| 62 | |||
| 63 | // prevent output changes caused by locale settings on the system where this script is running |
||
| 64 | putenv(sprintf('LANGUAGE=%s', 'en_US')); |
||
| 65 | |||
| 66 | $installationManager = $composer->getInstallationManager(); |
||
| 67 | /* @var $checkedPackages PackageVerification[] */ |
||
| 68 | $checkedPackages = array_map( |
||
| 69 | function (PackageInterface $package) use ($installationManager) : PackageVerification { |
||
| 70 | return self::verifyPackage($installationManager, $package); |
||
| 71 | }, |
||
| 72 | $composer->getRepositoryManager()->getLocalRepository()->getPackages() |
||
| 73 | ); |
||
| 74 | |||
| 75 | putenv(sprintf('LANGUAGE=%s', (string) $originalLanguage)); |
||
| 76 | |||
| 77 | $escapes = array_filter( |
||
| 78 | $checkedPackages, |
||
| 79 | function (PackageVerification $verification) : bool { |
||
| 80 | return ! $verification->isVerified(); |
||
| 81 | } |
||
| 82 | ); |
||
| 83 | |||
| 84 | if (! $escapes) { |
||
| 85 | $io->write('<info>roave/composer-gpg-verify:</info> All installed packages passed GPG validation!'); |
||
| 86 | |||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | throw PackagesTrustCheckFailed::fromFailedPackageVerifications(...$escapes); |
||
| 91 | } |
||
| 92 | |||
| 93 | private static function verifyPackage( |
||
| 109 | |||
| 110 | View Code Duplication | private static function checkCurrentCommitSignature( |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $gitDirectory |
||
| 126 | * |
||
| 127 | * @return string[] |
||
| 128 | */ |
||
| 129 | private static function getTagsForCurrentCommit(string $gitDirectory) : array |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $gitDirectory |
||
| 145 | * @param PackageInterface $package |
||
| 146 | * @param string[] $tags |
||
| 147 | * |
||
| 148 | * @return GitSignatureCheck[] |
||
| 149 | */ |
||
| 150 | private static function checkTagSignatures(string $gitDirectory, PackageInterface $package, string ...$tags) : array |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param Config $config |
||
| 175 | * |
||
| 176 | * |
||
| 177 | * @throws \RuntimeException |
||
| 178 | * @throws \Roave\ComposerGpgVerify\Exception\PreferredInstallIsNotSource |
||
| 179 | */ |
||
| 180 | private static function assertSourceInstallation(Config $config) : void |
||
| 188 | } |
||
| 189 |