Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making check Generic names #541

Draft
wants to merge 10 commits into
base: trunk
Choose a base branch
from
88 changes: 88 additions & 0 deletions includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Class Generic_Names_Check.php.
*
* @package plugin-check
*/

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;

/**
* Check for running WordPress generic names sniffs.
*
* @since 1.3.0
*/
class Generic_Names_Check extends Abstract_PHP_CodeSniffer_Check {

use Amend_Check_Result;
use Stable_Check;

/**
* Bitwise flags to control check behavior.
*
* @since 1.2.0.
* @var int
*/
protected $flags = 0;

/**
* Gets the categories for the check.
*
* Every check must have at least one category.
*
* @since 1.3.0
*
* @return array The categories for the check.
*/
public function get_categories() {
return array( Check_Categories::CATEGORY_PLUGIN_REPO );
}

/**
* 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.
*/
protected function get_args( Check_Result $result ) {
return array(
'extensions' => 'php',
'standard' => 'PluginCheck',
'sniffs' => 'PluginCheck.CodeAnalysis.GenericNames',
);
}

/**
* Gets the description for the check.
*
* Every check must have a short description explaining what the check does.
*
* @since 1.3.0
*
* @return string Description.
*/
public function get_description(): string {
return __( 'Checks the use of prefixes in all generic functions and globals.', 'plugin-check' );

Check warning on line 73 in includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php#L72-L73

Added lines #L72 - L73 were not covered by tests
}

/**
* Gets the documentation URL for the check.
*
* Every check must have a URL with further information about the check.
*
* @since 1.1.0
*
* @return string The documentation URL.
*/
public function get_documentation_url(): string {
return 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#prefixing';

Check warning on line 86 in includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php#L85-L86

Added lines #L85 - L86 were not covered by tests
}
}
1 change: 1 addition & 0 deletions includes/Checker/Default_Check_Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private function register_default_checks() {
'non_blocking_scripts' => new Checks\Performance\Non_Blocking_Scripts_Check(),
'offloading_files' => new Checks\Plugin_Repo\Offloading_Files_Check(),
'image_functions' => new Checks\Performance\Image_Functions_Check(),
'generic_names' => new Checks\Plugin_Repo\Generic_Names_Check(),
)
);

Expand Down
58 changes: 58 additions & 0 deletions phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* GenericNamesSniff
*
* Based on code from {@link https://github.com/WordPress/WordPress-Coding-Standards}
* which is licensed under {@link https://opensource.org/licenses/MIT}.
*
* @package PluginCheck
*/

namespace PluginCheckCS\PluginCheck\Sniffs\CodeAnalysis;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\Sniff;

/**
* Gets all function/class/define/namespace/option names and checks them to be not generic.
*
* @link https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/
*
* @since 1.3.0
*/
final class GenericNamesSniff extends Sniff {

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
$targets = Collections::textStringStartTokens();
$targets[] = \T_FUNCTION;

return $targets;
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process_token( $stackPtr ) {
$end_ptr = $stackPtr;
$content = $this->tokens[ $stackPtr ]['content'];

if ( empty( trim( $content ) ) ) {
return;
}

return ( $end_ptr + 1 );
}
}
davidperezgar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
// This is a test plugin to check for the use of global variables without a prefix.
function dosomething() {
echo 'Hello, World!';
}

function er_dosomething() {
echo 'Hello, World!';
}

function tppg_dosomething() {
echo 'Hello, World!';
}
1 change: 1 addition & 0 deletions phpcs-sniffs/PluginCheck/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<rule ref="PluginCheck.CodeAnalysis.EnqueuedResourceOffloading" />
<rule ref="PluginCheck.CodeAnalysis.Offloading" />
<rule ref="PluginCheck.CodeAnalysis.ImageFunctions" />
<rule ref="PluginCheck.CodeAnalysis.GenericNames" />

</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Plugin Name: Test Plugin Prefix Globals check with errors
* Plugin URI: https://github.com/wordpress/plugin-check
* Description: Test plugin for the Prefix Globals check.
* Requires at least: 6.0
* Requires PHP: 5.6
* Version: 1.0.0
* Author: WordPress Review Team
* 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-prefix-globals-with-errors
*
* @package test-plugin-prefix-globals-with-errors
*/

// This is a test plugin to check for the use of global variables without a prefix.
function dosomething() {
echo 'Hello, World!';
}

function er_dosomething() {
echo 'Hello, World!';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Plugin Name: Test Plugin Prefix Globals check without errors
* Plugin URI: https://github.com/wordpress/plugin-check
* Description: Test plugin for the Prefix Globals check.
* Requires at least: 6.0
* Requires PHP: 5.6
* Version: 1.0.0
* Author: WordPress Review Team
* 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-prefix-globals-without-errors
*
* @package test-plugin-prefix-globals-without-errors
*/

// This is a test plugin to check for the use of global variables with a prefix.
function tppg_dosomething() {
echo 'Hello, World!';
}
40 changes: 40 additions & 0 deletions tests/phpunit/tests/Checker/Checks/Prefix_Globals_Check_Tests.php
davidperezgar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Tests for the Prefix_All_Globals_Check class.
*
* @package plugin-check
*/

use WordPress\Plugin_Check\Checker\Check_Context;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Plugin_Repo\Prefix_All_Globals_Check;

class Prefix_All_Globals_Check_Tests extends WP_UnitTestCase {

public function test_run_with_errors() {
$check = new Prefix_All_Globals_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-prefix-all-globals-with-errors/load.php' );
$check_result = new Check_Result( $check_context );

$check->run( $check_result );

$errors = $check_result->get_errors();

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

public function test_run_without_errors() {
$check = new Prefix_All_Globals_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-prefix-all-globals-without-errors/load.php' );
$check_result = new Check_Result( $check_context );

$check->run( $check_result );

$errors = $check_result->get_errors();

$this->assertEmpty( $errors );
$this->assertEquals( 0, $check_result->get_error_count() );
}
}
Loading