| Total Complexity | 40 |
| Total Lines | 251 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AppRequirementsFactory 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.
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 AppRequirementsFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | final class AppRequirementsFactory |
||
| 29 | { |
||
| 30 | private const SELF_PACKAGE = '__APPLICATION__'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param array $composerJsonDecodedContents Decoded JSON contents of the `composer.json` file |
||
| 34 | * @param array $composerLockDecodedContents Decoded JSON contents of the `composer.lock` file |
||
| 35 | * |
||
| 36 | * @return array Serialized configured requirements |
||
| 37 | */ |
||
| 38 | public static function create(array $composerJsonDecodedContents, array $composerLockDecodedContents, ?int $compressionAlgorithm): array |
||
| 39 | { |
||
| 40 | return self::configureExtensionRequirements( |
||
| 41 | self::retrievePhpVersionRequirements([], $composerJsonDecodedContents, $composerLockDecodedContents), |
||
| 42 | $composerJsonDecodedContents, |
||
| 43 | $composerLockDecodedContents, |
||
| 44 | $compressionAlgorithm |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | |||
| 48 | private static function retrievePhpVersionRequirements( |
||
| 61 | } |
||
| 62 | |||
| 63 | private static function retrievePlatformPhpRequirement( |
||
| 64 | array $requirements, |
||
| 65 | array $composerJsonContents, |
||
| 66 | array $composerLockContents |
||
| 67 | ): array { |
||
| 68 | $requiredPhpVersion = [] === $composerLockContents |
||
| 69 | ? $composerJsonContents['require']['php'] |
||
| 70 | : $composerLockContents['platform']['php']; |
||
| 71 | |||
| 72 | $requirements[] = self::generatePhpCheckRequirement((string) $requiredPhpVersion, null); |
||
| 73 | |||
| 74 | return $requirements; |
||
| 75 | } |
||
| 76 | |||
| 77 | private static function retrievePackagesPhpRequirement(array $requirements, array $composerLockContents): array |
||
| 78 | { |
||
| 79 | $packages = $composerLockContents['packages'] ?? []; |
||
| 80 | |||
| 81 | foreach ($packages as $packageInfo) { |
||
| 82 | $requiredPhpVersion = $packageInfo['require']['php'] ?? null; |
||
| 83 | |||
| 84 | if (null === $requiredPhpVersion) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | $requirements[] = self::generatePhpCheckRequirement((string) $requiredPhpVersion, $packageInfo['name']); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $requirements; |
||
| 92 | } |
||
| 93 | |||
| 94 | private static function configureExtensionRequirements( |
||
| 95 | array $requirements, |
||
| 96 | array $composerJsonContents, |
||
| 97 | array $composerLockContents, |
||
| 98 | ?int $compressionAlgorithm |
||
| 99 | ): array { |
||
| 100 | $extensionRequirements = self::collectExtensionRequirements($composerJsonContents, $composerLockContents, $compressionAlgorithm); |
||
| 101 | |||
| 102 | foreach ($extensionRequirements as $extension => $packages) { |
||
| 103 | foreach ($packages as $package) { |
||
| 104 | if (self::SELF_PACKAGE === $package) { |
||
| 105 | $message = sprintf( |
||
| 106 | 'The application requires the extension "%s". Enable it or install a polyfill.', |
||
| 107 | $extension |
||
| 108 | ); |
||
| 109 | $helpMessage = sprintf( |
||
| 110 | 'The application requires the extension "%s".', |
||
| 111 | $extension |
||
| 112 | ); |
||
| 113 | } else { |
||
| 114 | $message = sprintf( |
||
| 115 | 'The package "%s" requires the extension "%s". Enable it or install a polyfill.', |
||
| 116 | $package, |
||
| 117 | $extension |
||
| 118 | ); |
||
| 119 | $helpMessage = sprintf( |
||
| 120 | 'The package "%s" requires the extension "%s".', |
||
| 121 | $package, |
||
| 122 | $extension |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | $requirements[] = [ |
||
| 127 | 'type' => 'extension', |
||
| 128 | 'condition' => $extension, |
||
| 129 | 'message' => $message, |
||
| 130 | 'helpMessage' => $helpMessage, |
||
| 131 | ]; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | return $requirements; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Collects the extension required. It also accounts for the polyfills, i.e. if the polyfill `symfony/polyfill-mbstring` is provided |
||
| 140 | * then the extension `ext-mbstring` will not be required. |
||
| 141 | * |
||
| 142 | * @return array Associative array containing the list of extensions required |
||
| 143 | */ |
||
| 144 | private static function collectExtensionRequirements( |
||
| 145 | array $composerJsonContents, |
||
| 146 | array $composerLockContents, |
||
| 147 | ?int $compressionAlgorithm |
||
| 148 | ): array { |
||
| 149 | $requirements = []; |
||
| 150 | $polyfills = []; |
||
| 151 | |||
| 152 | if (Phar::BZ2 === $compressionAlgorithm) { |
||
| 153 | $requirements['bz2'] = [self::SELF_PACKAGE]; |
||
| 154 | } |
||
| 155 | |||
| 156 | if (Phar::GZ === $compressionAlgorithm) { |
||
| 157 | $requirements['zlib'] = [self::SELF_PACKAGE]; |
||
| 158 | } |
||
| 159 | |||
| 160 | $platform = $composerLockContents['platform'] ?? []; |
||
| 161 | |||
| 162 | foreach ($platform as $package => $constraint) { |
||
| 163 | if (preg_match('/^ext-(?<extension>.+)$/', $package, $matches)) { |
||
| 164 | $extension = $matches['extension']; |
||
| 165 | |||
| 166 | $requirements[$extension] = [self::SELF_PACKAGE]; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | [$polyfills, $requirements] = [] === $composerLockContents |
||
| 171 | ? self::collectComposerJsonExtensionRequirements($composerJsonContents, $polyfills, $requirements) |
||
| 172 | : self::collectComposerLockExtensionRequirements($composerLockContents, $polyfills, $requirements) |
||
| 173 | ; |
||
| 174 | |||
| 175 | return array_diff_key($requirements, $polyfills); |
||
| 176 | } |
||
| 177 | |||
| 178 | private static function collectComposerJsonExtensionRequirements(array $composerJsonContents, $polyfills, $requirements): array |
||
| 179 | { |
||
| 180 | $packages = $composerJsonContents['require'] ?? []; |
||
| 181 | |||
| 182 | foreach ($packages as $packageName => $constraint) { |
||
| 183 | if (1 === preg_match('/symfony\/polyfill-(?<extension>.+)/', $packageName, $matches)) { |
||
| 184 | $extension = $matches['extension']; |
||
| 185 | |||
| 186 | if ('php' !== substr($extension, 0, 3)) { |
||
| 187 | $polyfills[$extension] = true; |
||
| 188 | |||
| 189 | continue; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | if ('paragonie/sodium_compat' === $packageName) { |
||
| 194 | $polyfills['libsodium'] = true; |
||
| 195 | |||
| 196 | continue; |
||
| 197 | } |
||
| 198 | |||
| 199 | if ('phpseclib/mcrypt_compat' === $packageName) { |
||
| 200 | $polyfills['mcrypt'] = true; |
||
| 201 | |||
| 202 | continue; |
||
| 203 | } |
||
| 204 | |||
| 205 | if ('php' !== $packageName && preg_match('/^ext-(?<extension>.+)$/', $packageName, $matches)) { |
||
| 206 | $requirements[$matches['extension']] = [self::SELF_PACKAGE]; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | return [$polyfills, $requirements]; |
||
| 211 | } |
||
| 212 | |||
| 213 | private static function collectComposerLockExtensionRequirements(array $composerLockContents, $polyfills, $requirements): array |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string[] |
||
| 254 | */ |
||
| 255 | private static function generatePhpCheckRequirement(string $requiredPhpVersion, ?string $packageName): array |
||
| 283 |