From 143afd2c1ec2750c77b3bf773b320530e0ee0d73 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 26 Feb 2025 18:00:04 +0100 Subject: [PATCH] Add patchers to fix namespace issues --- composer.json | 5 +- .../Checks/Abstract_PHP_CodeSniffer_Check.php | 2 +- scoper.inc.php | 55 ++++++++++++++++++- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index d9d6dc33..ce6d5620 100644 --- a/composer.json +++ b/composer.json @@ -81,9 +81,10 @@ "test": "phpunit", "prefix-dependencies": [ "vendor-bin/php-scoper/vendor/bin/php-scoper add-prefix --output-dir=./vendor-prefixed --force --quiet", - "echo '{ \"autoload\": { \"classmap\": [\"\"], \"exclude-from-classmap\": [\"vendor\\/squizlabs\\/php_codesniffer\\/autoload.php\"] } }' > ./vendor-prefixed/composer.json", + "echo '{ \"autoload\": { \"classmap\": [\"\"], \"exclude-from-classmap\": [\"vendor\\/squizlabs//\"] } }' > ./vendor-prefixed/composer.json", "@composer dump-autoload --working-dir ./vendor-prefixed --no-dev --classmap-authoritative", - "sed -i'.bak' -e 's/Composer\\\\Autoload/Plugin_Check_Composer\\\\Autoload/' vendor-prefixed/vendor/composer/*.php && rm -rf vendor-prefixed/vendor/composer/*.php.bak", + "sed -i'.bak' -e 's/Composer\\\\Autoload/Plugin_Check_Composer\\\\Autoload/' vendor-prefixed/vendor/composer/*.php", + "rm -rf vendor-prefixed/vendor/composer/*.php.bak", "echo '{ \"autoload\": { \"classmap\": [\"\"] } }' > ./includes/composer.json", "@composer dump-autoload --working-dir ./includes --no-dev --classmap-authoritative", "sed -i'.bak' -e 's/Composer\\\\Autoload/Plugin_Check_Composer\\\\Autoload/' includes/vendor/composer/*.php && rm -rf includes/vendor/composer/*.php.bak" diff --git a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php index 68004cd0..8752fff7 100644 --- a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php +++ b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php @@ -204,7 +204,7 @@ private function get_argv_defaults( Check_Result $result ): array { $defaults = array( '', $result->plugin()->location(), - '--report=Json', + '--report=WordPress\Plugin_Check\Vendor\PHP_CodeSniffer\Reports\Json', '--report-width=9999', ); diff --git a/scoper.inc.php b/scoper.inc.php index d89be2d2..03ca5c62 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -11,10 +11,10 @@ use Isolated\Symfony\Component\Finder\Finder; return array( - 'prefix' => 'WordPress\\Plugin_Check\\Vendor', + 'prefix' => 'WordPress\\Plugin_Check\\Vendor', // See: https://github.com/humbug/php-scoper#finders-and-paths. - 'finders' => array( + 'finders' => array( // PHP_CodeSniffer. Finder::create() ->files() @@ -93,6 +93,20 @@ ->notName( '*-test.php' ) ->in( 'vendor/automattic/vipwpcs' ), + // VariableAnalysis is used by WPCS. + Finder::create() + ->files() + ->ignoreVCS( true ) + ->ignoreDotFiles( true ) + ->name( + array( + '*.php', + 'ruleset.xml', + 'composer.json', + ) + ) + ->in( 'vendor/sirbrillig/phpcs-variable-analysis' ), + // Plugin Check custom PHPCS sniffs. Finder::create() ->files() @@ -101,6 +115,7 @@ ->name( array( '*.php', + 'ruleset.xml', 'composer.json', ) ) @@ -130,4 +145,40 @@ Finder::create() ->append( array( 'composer.json' ) ), ), + + 'patchers' => array( + static function ( string $file_path, string $prefix, string $content ) { + if ( str_ends_with( $file_path, 'vendor/squizlabs/php_codesniffer/autoload.php' ) ) { + $content = str_replace( + 'substr($class, 0, 16) === \'PHP_CodeSniffer\\', + 'substr($class, 0, 46) === \'WordPress\Plugin_Check\Vendor\PHP_CodeSniffer\\', + $content + ); + + $content = str_replace( + 'substr(str_replace(\'\\\\\', $ds, $class), 16)', + 'substr(str_replace(\'\\\\\', $ds, $class), 46)', + $content + ); + } + + if ( str_ends_with( $file_path, 'vendor/squizlabs/php_codesniffer/src/Files/File.php' ) ) { + $content = str_replace( + 'PHP_CodeSniffer\Tokenizers\\', + 'WordPress\Plugin_Check\Vendor\PHP_CodeSniffer\Tokenizers\\', + $content + ); + } + + if ( str_ends_with( $file_path, 'vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php' ) ) { + $content = str_replace( + 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff', + 'WordPress\Plugin_Check\Vendor\PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff', + $content + ); + } + + return $content; + }, + ), );