Skip to content

Commit

Permalink
Add patchers to fix namespace issues
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Feb 26, 2025
1 parent 46645a2 commit 143afd2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);

Expand Down
55 changes: 53 additions & 2 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -101,6 +115,7 @@
->name(
array(
'*.php',
'ruleset.xml',
'composer.json',
)
)
Expand Down Expand Up @@ -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;
},
),
);

0 comments on commit 143afd2

Please sign in to comment.