Skip to content

Commit

Permalink
Merge pull request #367 from wp-cli/fix/wpcs-3.0-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Aug 30, 2023
2 parents 27c8be3 + e3610e1 commit 7d82e67
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 57 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"require-dev": {
"wp-cli/scaffold-command": "^1.2 || ^2",
"wp-cli/wp-cli-tests": "^3.1"
"wp-cli/wp-cli-tests": "^4"
},
"suggest": {
"ext-json": "Used for reading and generating JSON translation files",
Expand Down
2 changes: 1 addition & 1 deletion i18n-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'i18n make-pot',
'\WP_CLI\I18n\MakePotCommand',
array(
'before_invoke' => static function() {
'before_invoke' => static function () {
if ( ! function_exists( 'mb_ereg' ) ) {
WP_CLI::error( 'The mbstring extension is required for string extraction to work reliably.' );
}
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<!-- Whitelist property name for a a JSON-decoded object. -->
<rule ref="WordPress.NamingConventions.ValidVariableName">
<properties>
<property name="customPropertiesWhitelist" type="array">
<property name="allowed_custom_properties" type="array">
<element value="sourcesContent"/>
<element value="functionsScannerClass"/>
</property>
Expand Down
4 changes: 2 additions & 2 deletions src/BladeCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ final class BladeCodeExtractor extends BladeGettextExtractor {
/**
* {@inheritdoc}
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
public static function fromString( $text, Translations $translations, array $options = [] ) {
WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' );

try {
static::fromStringMultiple( $string, [ $translations ], $options );
self::fromStringMultiple( $text, [ $translations ], $options );
} catch ( Exception $exception ) {
WP_CLI::debug(
sprintf(
Expand Down
10 changes: 5 additions & 5 deletions src/BladeGettextExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ protected static function getBladeCompiler() {
/**
* Compiles the Blade template string into a PHP string in one step.
*
* @param string $string Blade string to be compiled to a PHP string
* @param string $text Blade string to be compiled to a PHP string
* @return string
*/
protected static function compileBladeToPhp( $string ) {
return static::getBladeCompiler()->compileString( $string );
protected static function compileBladeToPhp( $text ) {
return static::getBladeCompiler()->compileString( $text );
}

/**
* {@inheritdoc}
*
* Note: In the parent PhpCode class fromString() uses fromStringMultiple() (overriden here)
*/
public static function fromStringMultiple( $string, array $translations, array $options = [] ) {
$php_string = static::compileBladeToPhp( $string );
public static function fromStringMultiple( $text, array $translations, array $options = [] ) {
$php_string = static::compileBladeToPhp( $text );
return parent::fromStringMultiple( $php_string, $translations, $options );
}
}
6 changes: 3 additions & 3 deletions src/BlockExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ final class BlockExtractor extends JsonSchemaExtractor {
/**
* @inheritdoc
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
public static function fromString( $text, Translations $translations, array $options = [] ) {
$file = $options['file'];
WP_CLI::debug( "Parsing file $file", 'make-pot' );

$json = json_decode( $string, true );
$json = json_decode( $text, true );

if ( null === $json ) {
WP_CLI::debug(
Expand All @@ -35,6 +35,6 @@ public static function fromString( $string, Translations $translations, array $o
return;
}

parent::fromString( $string, $translations, $options );
parent::fromString( $text, $translations, $options );
}
}
6 changes: 3 additions & 3 deletions src/FileDataExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public static function get_file_data( $file, $headers ) {
/**
* Retrieves metadata from a string.
*
* @param string $string String to look for metadata in.
* @param string $text String to look for metadata in.
* @param array $headers List of headers.
*
* @return array Array of file headers in `HeaderKey => Header Value` format.
*/
public static function get_file_data_from_string( $string, $headers ) {
public static function get_file_data_from_string( $text, $headers ) {
foreach ( $headers as $field => $regex ) {
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $string, $match ) && $match[1] ) {
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $text, $match ) && $match[1] ) {
$headers[ $field ] = static::_cleanup_header_comment( $match[1] );
} else {
$headers[ $field ] = '';
Expand Down
30 changes: 15 additions & 15 deletions src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static function fromFile( $file_or_files, Translations $translations, arr
// Make sure a relative file path is added as a comment.
$options['file'] = ltrim( str_replace( static::$dir, '', Utils\normalize_path( $file ) ), '/' );

$string = file_get_contents( $file );
$text = file_get_contents( $file );

if ( ! $string ) {
if ( ! $text ) {
WP_CLI::debug(
sprintf(
'Could not load file %1s',
Expand All @@ -56,7 +56,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr
}

if ( ! empty( $options['wpExtractTemplates'] ) ) {
$headers = FileDataExtractor::get_file_data_from_string( $string, [ 'Template Name' => 'Template Name' ] );
$headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ] );

if ( ! empty( $headers['Template Name'] ) ) {
$translation = new Translation( '', $headers['Template Name'] );
Expand All @@ -69,7 +69,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr
// Patterns are only supported when in a top-level patterns/ folder.
if ( ! empty( $options['wpExtractPatterns'] ) && 0 === strpos( $options['file'], 'patterns/' ) ) {
$headers = FileDataExtractor::get_file_data_from_string(
$string,
$text,
[
'Title' => 'Title',
'Description' => 'Description',
Expand All @@ -91,7 +91,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr
}
}

static::fromString( $string, $translations, $options );
static::fromString( $text, $translations, $options );
}
}

Expand Down Expand Up @@ -225,29 +225,29 @@ protected static function containsMatchingChildren( SplFileInfo $dir, array $mat
* Recursively gets all PHP files within a directory.
*
* @param string $dir A path of a directory.
* @param array $include List of files and directories to include.
* @param array $exclude List of files and directories to skip.
* @param array $includes List of files and directories to include.
* @param array $excludes List of files and directories to skip.
* @param array $extensions List of filename extensions to process.
*
* @return array File list.
*/
public static function getFilesFromDirectory( $dir, array $include = [], array $exclude = [], $extensions = [] ) {
public static function getFilesFromDirectory( $dir, array $includes = [], array $excludes = [], $extensions = [] ) {
$filtered_files = [];

$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS | RecursiveDirectoryIterator::UNIX_PATHS | RecursiveDirectoryIterator::FOLLOW_SYMLINKS ),
static function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions ) {
static function ( $file, $key, $iterator ) use ( $includes, $excludes, $extensions ) {
/** @var RecursiveCallbackFilterIterator $iterator */
/** @var SplFileInfo $file */

// Normalize include and exclude paths.
$include = array_map( self::class . '::trim_leading_slash', $include );
$exclude = array_map( self::class . '::trim_leading_slash', $exclude );
$includes = array_map( self::class . '::trim_leading_slash', $includes );
$excludes = array_map( self::class . '::trim_leading_slash', $excludes );

// If no $include is passed everything gets the weakest possible matching score.
$inclusion_score = empty( $include ) ? 0.1 : static::calculateMatchScore( $file, $include );
$exclusion_score = static::calculateMatchScore( $file, $exclude );
// If no $includes is passed everything gets the weakest possible matching score.
$inclusion_score = empty( $includes ) ? 0.1 : static::calculateMatchScore( $file, $includes );
$exclusion_score = static::calculateMatchScore( $file, $excludes );

// Always include directories that aren't excluded.
if ( 0 === $exclusion_score && $iterator->hasChildren() ) {
Expand All @@ -256,7 +256,7 @@ static function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions

if ( ( 0 === $inclusion_score || $exclusion_score > $inclusion_score ) && $iterator->hasChildren() ) {
// Always include directories that may have matching children even if they are excluded.
return static::containsMatchingChildren( $file, $include );
return static::containsMatchingChildren( $file, $includes );
}

// Include directories that are excluded but include score is higher.
Expand Down
10 changes: 5 additions & 5 deletions src/JsCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ final class JsCodeExtractor extends JsCode {
/**
* @inheritdoc
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
public static function fromString( $text, Translations $translations, array $options = [] ) {
WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' );

try {
static::fromStringMultiple( $string, [ $translations ], $options );
self::fromStringMultiple( $text, [ $translations ], $options );
} catch ( PeastException $exception ) {
WP_CLI::debug(
sprintf(
Expand All @@ -58,11 +58,11 @@ public static function fromString( $string, Translations $translations, array $o
/**
* @inheritDoc
*/
public static function fromStringMultiple( $string, array $translations, array $options = [] ) {
$options += static::$options;
public static function fromStringMultiple( $text, array $translations, array $options = [] ) {
$options += self::$options;

/** @var JsFunctionsScanner $functions */
$functions = new static::$functionsScannerClass( $string );
$functions = new self::$functionsScannerClass( $text );
$functions->enableCommentsExtraction( $options['extractComments'] );
$functions->saveGettextFunctions( $translations, $options );
}
Expand Down
5 changes: 2 additions & 3 deletions src/JsonSchemaExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ protected static function load_schema( $schema, $fallback ) {
/**
* @inheritdoc
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
public static function fromString( $text, Translations $translations, array $options = [] ) {
$file = $options['file'];
WP_CLI::debug( "Parsing file {$file}", 'make-pot' );

$schema = self::load_schema( $options['schema'], $options['schemaFallback'] );

$json = json_decode( $string, true );
$json = json_decode( $text, true );

if ( null === $json ) {
WP_CLI::debug(
Expand Down Expand Up @@ -182,5 +182,4 @@ private static function remote_get( $url ) {

return trim( $response->body );
}

}
2 changes: 1 addition & 1 deletion src/MakeMoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __invoke( $args, $assoc_args ) {
continue;
}

$result_count++;
++$result_count;
}

WP_CLI::success( sprintf( 'Created %d %s.', $result_count, Utils\pluralize( 'file', $result_count ) ) );
Expand Down
6 changes: 3 additions & 3 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,11 @@ function ( $file ) {
/**
* Removes leading and trailing slashes of a string.
*
* @param string $string What to add and remove slashes from.
* @param string $text What to add and remove slashes from.
* @return string String without leading and trailing slashes.
*/
protected function unslashit( $string ) {
return ltrim( rtrim( trim( $string ), '/\\' ), '/\\' );
protected function unslashit( $text ) {
return ltrim( rtrim( trim( $text ), '/\\' ), '/\\' );
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/MapCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ final class MapCodeExtractor extends JsCode {
/**
* {@inheritdoc}
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
public static function fromString( $text, Translations $translations, array $options = [] ) {
if ( ! array_key_exists( 'file', $options ) || substr( $options['file'], -7 ) !== '.js.map' ) {
return;
}
$options['file'] = substr( $options['file'], 0, -7 ) . '.js';

try {
$options += static::$options;
$options += self::$options;

$map_object = json_decode( $string );
$map_object = json_decode( $text );

if ( ! isset( $map_object->sourcesContent ) || ! is_array( $map_object->sourcesContent ) ) {
return;
}

$string = implode( "\n", $map_object->sourcesContent );
$text = implode( "\n", $map_object->sourcesContent );

WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' );

$functions = new JsFunctionsScanner( $string );
$functions = new JsFunctionsScanner( $text );

$functions->enableCommentsExtraction( $options['extractComments'] );
$functions->saveGettextFunctions( $translations, $options );
Expand Down
4 changes: 2 additions & 2 deletions src/PhpCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ final class PhpCodeExtractor extends PhpCode {
/**
* {@inheritdoc}
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
public static function fromString( $text, Translations $translations, array $options = [] ) {
WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' );

try {
static::fromStringMultiple( $string, [ $translations ], $options );
self::fromStringMultiple( $text, [ $translations ], $options );
} catch ( Exception $exception ) {
WP_CLI::debug(
sprintf(
Expand Down
8 changes: 4 additions & 4 deletions src/PotGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function toString( Translations $translations, array $options = []
if ( $translation->hasPlural() ) {
self::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() );

for ( $i = 0; $i <= $plural_size; $i ++ ) {
for ( $i = 0; $i <= $plural_size; $i++ ) {
self::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' );
}
} else {
Expand All @@ -105,12 +105,12 @@ public static function toString( Translations $translations, array $options = []
/**
* Escapes and adds double quotes to a string.
*
* @param string $string Multiline string.
* @param string $text Multiline string.
*
* @return string[]
*/
protected static function multilineQuote( $string ) {
$lines = explode( "\n", $string );
protected static function multilineQuote( $text ) {
$lines = explode( "\n", $text );
$last = count( $lines ) - 1;

foreach ( $lines as $k => $line ) {
Expand Down
4 changes: 2 additions & 2 deletions src/ThemeJsonExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class ThemeJsonExtractor extends JsonSchemaExtractor {
/**
* @inheritdoc
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
public static function fromString( $text, Translations $translations, array $options = [] ) {
$file = $options['file'];

// Only support top-level theme.json file or any JSON file within a top-level styles/ folder.
Expand All @@ -24,6 +24,6 @@ public static function fromString( $string, Translations $translations, array $o
return;
}

parent::fromString( $string, $translations, $options );
parent::fromString( $text, $translations, $options );
}
}
2 changes: 1 addition & 1 deletion src/UpdatePoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __invoke( $args, $assoc_args ) {
continue;
}

$result_count++;
++$result_count;
}

WP_CLI::success( sprintf( 'Updated %d %s.', $result_count, Utils\pluralize( 'file', $result_count ) ) );
Expand Down

0 comments on commit 7d82e67

Please sign in to comment.