GraphQLAPI /
graphql-api-for-wp
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | use Isolated\Symfony\Component\Finder\Finder; |
||
|
0 ignored issues
–
show
|
|||
| 6 | |||
| 7 | /** |
||
| 8 | * Must only scope the packages in vendor/, because: |
||
| 9 | * |
||
| 10 | * - config/ references only local configuration |
||
| 11 | * - src/ references only local classes (with one exception), which need not be scoped |
||
| 12 | * - vendor/ references external classes (excluding local -wp packages), which need be scoped |
||
| 13 | * |
||
| 14 | * In addition, we must exclude all the local WordPress packages, |
||
| 15 | * because scoping WordPress functions does NOT work. |
||
| 16 | * @see https://github.com/humbug/php-scoper/issues/303 |
||
| 17 | * |
||
| 18 | * Excluding the WordPress packages is feasible, because they do |
||
| 19 | * not reference any external library. |
||
| 20 | * |
||
| 21 | * The only exception is getpop/routing-wp, which uses Brain\Cortex: |
||
| 22 | * |
||
| 23 | * in getpop/routing-wp/src/Component.php: |
||
| 24 | * use Brain\Cortex; |
||
| 25 | * |
||
| 26 | * in getpop/routing-wp/src/Hooks/SetupCortexHookSet.php: |
||
| 27 | * use Brain\Cortex\Route\RouteCollectionInterface; |
||
| 28 | * use Brain\Cortex\Route\RouteInterface; |
||
| 29 | * use Brain\Cortex\Route\QueryRoute; |
||
| 30 | * |
||
| 31 | * Then, manually add these 2 files to scope Brain\Cortex. |
||
| 32 | * This works without side effects, because there are no WordPress stubs in them. |
||
| 33 | */ |
||
| 34 | function convertRelativeToFullPath(string $relativePath): string { |
||
| 35 | return __DIR__ . '/vendor/' . $relativePath; |
||
| 36 | } |
||
| 37 | return [ |
||
| 38 | 'prefix' => 'PrefixedByPoP', |
||
| 39 | 'finders' => [ |
||
| 40 | // Scope packages under vendor/, excluding local WordPress packages |
||
| 41 | Finder::create() |
||
| 42 | ->files() |
||
| 43 | ->ignoreVCS(true) |
||
| 44 | ->notName('/.*\\.md|.*\\.dist|composer\\.lock/') |
||
| 45 | ->exclude([ |
||
| 46 | 'tests', |
||
| 47 | ]) |
||
| 48 | ->notPath([ |
||
| 49 | // Exclude libraries ending in "-wp" |
||
| 50 | '#getpop/[a-zA-Z0-9_-]*-wp/#', |
||
| 51 | '#pop-schema/[a-zA-Z0-9_-]*-wp/#', |
||
| 52 | '#graphql-by-pop/[a-zA-Z0-9_-]*-wp/#', |
||
| 53 | // Exclude all composer.json from own libraries (they get broken!) |
||
| 54 | '#[getpop|pop\-schema|graphql\-by\-pop|graphql\-api]/*/composer.json#', |
||
| 55 | // Exclude libraries |
||
| 56 | '#symfony/deprecation-contracts/#', |
||
| 57 | '#ralouphie/getallheaders/#', |
||
| 58 | // Exclude tests from libraries |
||
| 59 | '#nikic/fast-route/test/#', |
||
| 60 | '#psr/log/Psr/Log/Test/#', |
||
| 61 | '#symfony/service-contracts/Test/#', |
||
| 62 | ]) |
||
| 63 | ->in('vendor'), |
||
| 64 | Finder::create()->append([ |
||
| 65 | 'vendor/getpop/routing-wp/src/Component.php', |
||
| 66 | 'vendor/getpop/routing-wp/src/Hooks/SetupCortexHookSet.php', |
||
| 67 | ]) |
||
| 68 | ], |
||
| 69 | 'whitelist' => array_values(array_unique([ |
||
| 70 | // Own namespaces |
||
| 71 | // Watch out! Do NOT alter the order of PoPSchema and PoP! |
||
| 72 | // If PoP comes first, then PoPSchema is still scoped! |
||
| 73 | 'PoPSchema\*', |
||
| 74 | 'PoP\*', |
||
| 75 | 'GraphQLByPoP\*', |
||
| 76 | 'GraphQLAPI\*', |
||
| 77 | // Own container cache namespace |
||
| 78 | // Watch out! This value is being hardcoded! |
||
| 79 | // In the application, it can be overriden via code: |
||
| 80 | // - ContainerBuilderFactory::getContainerNamespace() |
||
| 81 | // - SystemContainerBuilderFactory::getContainerNamespace() |
||
| 82 | // But can't reference these classes here, since they are not found |
||
| 83 | // (unless adding the files to the autoload path) |
||
| 84 | 'PoPContainer\*', |
||
| 85 | ])), |
||
| 86 | 'files-whitelist' => [ |
||
| 87 | // Class Composer\InstalledVersions will be regenerated without scope when |
||
| 88 | // doing `composer dumpautoload`, so skip it |
||
| 89 | 'vendor/composer/InstalledVersions.php', |
||
| 90 | ], |
||
| 91 | 'patchers' => [ |
||
| 92 | function (string $filePath, string $prefix, string $content): string { |
||
| 93 | /** |
||
| 94 | * File vendor/nikic/fast-route/src/bootstrap.php has this code: |
||
| 95 | * |
||
| 96 | * if (\strpos($class, 'FastRoute\\') === 0) { |
||
| 97 | * $name = \substr($class, \strlen('FastRoute')); |
||
| 98 | * |
||
| 99 | * Fix it |
||
| 100 | */ |
||
| 101 | if ($filePath === __DIR__ . DIRECTORY_SEPARATOR . 'vendor/nikic/fast-route/src/bootstrap.php') { |
||
| 102 | return str_replace( |
||
| 103 | ["'FastRoute\\\\'", "'FastRoute'"], |
||
| 104 | ["'${prefix}\\\\FastRoute\\\\'", "'${prefix}\\\\FastRoute'"], |
||
| 105 | $content |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | /** |
||
| 109 | * Brain/Cortex is prefixing classes \WP and \WP_Rewrite |
||
| 110 | * Avoid it! |
||
| 111 | */ |
||
| 112 | if (str_starts_with($filePath, __DIR__ . DIRECTORY_SEPARATOR . 'vendor/brain/cortex/')) { |
||
| 113 | return str_replace( |
||
| 114 | "\\${prefix}\\WP", |
||
| 115 | "\\WP", |
||
| 116 | $content |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | /** |
||
| 120 | * Symfony Polyfill packages. |
||
| 121 | * The bootstrap*.php files must register functions under the global namespace, |
||
| 122 | * and the other ones must register constants on the global namespace, |
||
| 123 | * so remove the namespaced after it's added. |
||
| 124 | * These files can't be whitelisted, because they may reference a prefixed class |
||
| 125 | */ |
||
| 126 | // Pattern to identify Symfony Polyfill bootstrap files |
||
| 127 | // - vendor/symfony/polyfill-mbstring/bootstrap80.php |
||
| 128 | // - vendor/symfony/polyfill-php72/bootstrap.php |
||
| 129 | // - etc |
||
| 130 | $pattern = '#' . __DIR__ . '/vendor/symfony/polyfill-[a-zA-Z0-9_-]*/bootstrap.*\.php#'; |
||
| 131 | $symfonyPolyfillFilesWithGlobalClass = array_map( |
||
| 132 | 'convertRelativeToFullPath', |
||
| 133 | [ |
||
| 134 | 'symfony/polyfill-intl-normalizer/Resources/stubs/Normalizer.php', |
||
| 135 | 'symfony/polyfill-php73/Resources/stubs/JsonException.php', |
||
| 136 | 'symfony/polyfill-php80/Resources/stubs/Attribute.php', |
||
| 137 | 'symfony/polyfill-php80/Resources/stubs/Stringable.php', |
||
| 138 | 'symfony/polyfill-php80/Resources/stubs/UnhandledMatchError.php', |
||
| 139 | 'symfony/polyfill-php80/Resources/stubs/ValueError.php', |
||
| 140 | ] |
||
| 141 | ); |
||
| 142 | $isSymfonyPolyfillFileWithGlobalClass = in_array($filePath, $symfonyPolyfillFilesWithGlobalClass); |
||
| 143 | if ( |
||
| 144 | $isSymfonyPolyfillFileWithGlobalClass |
||
| 145 | || preg_match($pattern, $filePath) |
||
| 146 | ) { |
||
| 147 | // Remove the namespace |
||
| 148 | $content = str_replace( |
||
| 149 | "namespace ${prefix};", |
||
| 150 | '', |
||
| 151 | $content |
||
| 152 | ); |
||
| 153 | |||
| 154 | // Comment out the class_alias too |
||
| 155 | if ($isSymfonyPolyfillFileWithGlobalClass) { |
||
| 156 | $content = str_replace( |
||
| 157 | "\class_alias('${prefix}\\\\", |
||
| 158 | "//\class_alias('${prefix}\\\\", |
||
| 159 | $content |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $content; |
||
| 164 | } |
||
| 165 | /** |
||
| 166 | * In these files, it prefixes the return type `parent`. |
||
| 167 | * Undo it! |
||
| 168 | */ |
||
| 169 | $symfonyPolyfillFilesWithParentReturnType = array_map( |
||
| 170 | 'convertRelativeToFullPath', |
||
| 171 | [ |
||
| 172 | 'symfony/string/AbstractUnicodeString.php', |
||
| 173 | 'symfony/string/ByteString.php', |
||
| 174 | 'symfony/string/UnicodeString.php', |
||
| 175 | ] |
||
| 176 | ); |
||
| 177 | if (in_array($filePath, $symfonyPolyfillFilesWithParentReturnType)) { |
||
| 178 | return str_replace( |
||
| 179 | "\\${prefix}\\parent", |
||
| 180 | 'parent', |
||
| 181 | $content |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * It changes the path to Parsedown source files in its composer.json |
||
| 187 | * Undo it! |
||
| 188 | */ |
||
| 189 | if ($filePath == convertRelativeToFullPath('erusev/parsedown/composer.json')) { |
||
| 190 | return str_replace( |
||
| 191 | ['"\\/Parsedown\\/"', '"psr-4"'], |
||
| 192 | ['""', '"psr-0"'], |
||
| 193 | $content |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | |||
| 197 | return $content; |
||
| 198 | }, |
||
| 199 | ], |
||
| 200 | ]; |
||
| 201 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths