Skip to content

Commit

Permalink
Merge branch 'trunk' into 464-allow-wp-prefixes-on-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
davidperezgar authored Sep 30, 2024
2 parents 09aef00 + b554c2d commit 42daebd
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 39 deletions.
24 changes: 17 additions & 7 deletions includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ abstract class Abstract_PHP_CodeSniffer_Check implements Static_Check {
* @var array
*/
protected $allowed_args = array(
'standard' => true,
'extensions' => true,
'sniffs' => true,
'exclude' => true, //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
'standard' => true,
'extensions' => true,
'sniffs' => true,
'runtime-set' => true,
'exclude' => true, //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
);

/**
* Returns an associative array of arguments to pass to PHPCS.
*
* @since 1.0.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array {
* An associative array of PHPCS CLI arguments. Can include one or more of the following options.
*
Expand All @@ -50,7 +52,7 @@ abstract class Abstract_PHP_CodeSniffer_Check implements Static_Check {
* @type string $exclude A comma separated list of sniff codes to exclude from checks.
* }
*/
abstract protected function get_args();
abstract protected function get_args( Check_Result $result );

/**
* Amends the given result by running the check on the associated plugin.
Expand Down Expand Up @@ -83,7 +85,7 @@ final public function run( Check_Result $result ) {
$defaults = $this->get_argv_defaults( $result );

// Set the check arguments for PHPCS.
$_SERVER['argv'] = $this->parse_argv( $this->get_args(), $defaults );
$_SERVER['argv'] = $this->parse_argv( $this->get_args( $result ), $defaults );

// Reset PHP_CodeSniffer config.
$this->reset_php_codesniffer_config();
Expand Down Expand Up @@ -145,7 +147,15 @@ private function parse_argv( $argv, $defaults ) {

// Format check arguments for PHPCS.
foreach ( $check_args as $key => $value ) {
$defaults[] = "--{$key}=$value";
if ( 'runtime-set' === $key ) {
if ( is_array( $value ) ) {
foreach ( $value as $item_key => $item_value ) {
$defaults = array_merge( $defaults, array( "--{$key}", $item_key, $item_value ) );
}
}
} else {
$defaults[] = "--{$key}=$value";
}
}

return $defaults;
Expand Down
13 changes: 9 additions & 4 deletions includes/Checker/Checks/General/I18n_Usage_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPress\Plugin_Check\Checker\Checks\General;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Stable_Check;

Expand Down Expand Up @@ -41,13 +42,17 @@ public function get_categories() {
*
* @since 1.0.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array An associative array of PHPCS CLI arguments.
*/
protected function get_args() {
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => 'WordPress',
'sniffs' => 'WordPress.WP.I18n',
'extensions' => 'php',
'standard' => 'WordPress',
'sniffs' => 'WordPress.WP.I18n',
'runtime-set' => array(
'text_domain' => $result->plugin()->slug(),
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPress\Plugin_Check\Checker\Checks\Performance;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Stable_Check;

Expand Down Expand Up @@ -38,9 +39,10 @@ public function get_categories() {
*
* @since 1.0.2
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array An associative array of PHPCS CLI arguments.
*/
protected function get_args() {
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => 'WordPress',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPress\Plugin_Check\Checker\Checks\Performance;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Stable_Check;

Expand Down Expand Up @@ -38,9 +39,10 @@ public function get_categories() {
*
* @since 1.0.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array An associative array of PHPCS CLI arguments.
*/
protected function get_args() {
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => 'WordPress',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPress\Plugin_Check\Checker\Checks\Performance;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Stable_Check;

Expand Down Expand Up @@ -38,9 +39,10 @@ public function get_categories() {
*
* @since 1.0.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array An associative array of PHPCS CLI arguments.
*/
protected function get_args() {
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => 'WordPress,WordPressVIPMinimum',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPress\Plugin_Check\Checker\Checks\Plugin_Repo;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
use WordPress\Plugin_Check\Traits\Stable_Check;
Expand Down Expand Up @@ -48,9 +49,10 @@ public function get_categories() {
*
* @since 1.0.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array An associative array of PHPCS CLI arguments.
*/
protected function get_args() {
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => 'PluginCheck',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPress\Plugin_Check\Checker\Checks\Plugin_Repo;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Stable_Check;

Expand Down Expand Up @@ -38,9 +39,10 @@ public function get_categories() {
*
* @since 1.0.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array An associative array of PHPCS CLI arguments.
*/
protected function get_args() {
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'phpcs-rulesets/plugin-review.xml',
Expand Down
4 changes: 3 additions & 1 deletion includes/Checker/Checks/Security/Direct_DB_Queries_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPress\Plugin_Check\Checker\Checks\Security;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Stable_Check;

Expand Down Expand Up @@ -38,9 +39,10 @@ public function get_categories() {
*
* @since 1.0.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array An associative array of PHPCS CLI arguments.
*/
protected function get_args() {
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => 'WordPress',
Expand Down
4 changes: 3 additions & 1 deletion includes/Checker/Checks/Security/Late_Escaping_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPress\Plugin_Check\Checker\Checks\Security;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Stable_Check;

Expand Down Expand Up @@ -38,9 +39,10 @@ public function get_categories() {
*
* @since 1.0.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @return array An associative array of PHPCS CLI arguments.
*/
protected function get_args() {
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => 'WordPress',
Expand Down
4 changes: 4 additions & 0 deletions includes/Checker/Runtime_Environment_Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ static function () use ( $permalink_structure ) {
* Cleans up the runtime environment setup.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
* @global string $table_prefix The database table prefix.
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
*/
public function clean_up() {
global $wpdb, $table_prefix, $wp_filesystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-check
* Text Domain: test-plugin-i18n-usage-without-errors
*
* @package test-plugin-check
*/
Expand All @@ -22,8 +22,8 @@

sprintf(
/* translators: %s: Name of a city */
__( 'Your city is %s.', 'test-plugin-check' ),
__( 'Your city is %s.', 'test-plugin-i18n-usage-without-errors' ),
$city
);

esc_html__( 'Hello World!', 'test-plugin-check' );
esc_html__( 'Hello World!', 'test-plugin-i18n-usage-without-errors' );
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

// This will cause a WordPress.WP.I18n.MissingTranslatorsComment error as it has no translators comment.
sprintf(
__( 'Your city is %s.', 'test-plugin-check-errors' ),
__( 'Your city is %s.', 'test-plugin-ignore-directories' ),
$city
);
$text_domain = 'test-plugin-check-errors';

$text_domain = 'test-plugin-ignore-directories';

// This will cause a WordPress.WP.I18n.NonSingularStringLiteralDomain error as a variable is used for the text-domain.
esc_html__( 'Hello World!', $text_domain );
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-check-errors
* Text Domain: test-plugin-ignore-directories
*
* @package test-plugin-check-errors
* @package test-plugin-ignore-directories
*/
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
*/

$city = 'Kathmandu';
sprintf( __( 'Your city is %s.', 'test-plugin-check-errors' ), $city ); // This will trigger WordPress.WP.I18n.MissingTranslatorsComment error.
sprintf( __( 'Your city is %s.', 'test-plugin-ignore-files' ), $city ); // This will trigger WordPress.WP.I18n.MissingTranslatorsComment error.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
*/

$name = 'John Doe';
esc_html__( 'Hello, ' . $name, 'plugin-check' ); // This will trigger WordPress.WP.I18n.NonSingularStringLiteralText error.
esc_html__( 'Hello, ' . $name, 'test-plugin-ignore-files' ); // This will trigger WordPress.WP.I18n.NonSingularStringLiteralText error.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
* File contains errors related to i18n translation issues.
*/

$text_domain = 'test-plugin-check-errors';
$text_domain = 'test-plugin-ignore-files';
esc_html__( 'Hello World!', $text_domain ); // This will trigger WordPress.WP.I18n.NonSingularStringLiteralDomain error.
14 changes: 5 additions & 9 deletions tests/phpunit/tests/Checker/Checks/I18n_Usage_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@ public function test_run_with_errors() {

$this->assertNotEmpty( $errors );
$this->assertArrayHasKey( 'load.php', $errors );
$this->assertEquals( 2, $check_result->get_error_count() );

// Check for WordPress.WP.I18n.MissingTranslatorsComment error on Line no 26 and column no at 5.
$this->assertArrayHasKey( 26, $errors['load.php'] );
$this->assertArrayHasKey( 5, $errors['load.php'][26] );
$this->assertArrayHasKey( 'code', $errors['load.php'][26][5][0] );
$this->assertEquals( 'WordPress.WP.I18n.MissingTranslatorsComment', $errors['load.php'][26][5][0]['code'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][26][5], array( 'code' => 'WordPress.WP.I18n.MissingTranslatorsComment' ) ) );

// Check for WordPress.WP.I18n.TextDomainMismatch error on Line no 26 and column no at 29.
$this->assertCount( 1, wp_list_filter( $errors['load.php'][26][29], array( 'code' => 'WordPress.WP.I18n.TextDomainMismatch' ) ) );

// Check for WordPress.WP.I18n.NonSingularStringLiteralDomain error on Line no 33 and column no at 29.
$this->assertArrayHasKey( 33, $errors['load.php'] );
$this->assertArrayHasKey( 29, $errors['load.php'][33] );
$this->assertArrayHasKey( 'code', $errors['load.php'][33][29][0] );
$this->assertEquals( 'WordPress.WP.I18n.NonSingularStringLiteralDomain', $errors['load.php'][33][29][0]['code'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][33][29], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralDomain' ) ) );
}

public function test_run_without_errors() {
Expand Down

0 comments on commit 42daebd

Please sign in to comment.