-
Notifications
You must be signed in to change notification settings - Fork 83
Making check Generic names #541
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
Draft
davidperezgar
wants to merge
10
commits into
trunk
Choose a base branch
from
523-check-generic-functionclassdefineoption-prefix-names
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a33a656
making check prefix all globals
davidperezgar 605e53b
change the way how to detect it
davidperezgar a93eb0d
Merge branch 'trunk' into 523-check-generic-functionclassdefineoption…
davidperezgar da77095
moved to WPCS again
davidperezgar 6f0a7b8
Merge branch 'trunk' into 523-check-generic-functionclassdefineoption…
davidperezgar a6c6734
start sniff
davidperezgar 425605e
order checks
davidperezgar d904b19
tclass
davidperezgar 08bcc4e
updated tests
davidperezgar b79fff9
unit test
davidperezgar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' ); | ||
| } | ||
|
|
||
| /** | ||
| * 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'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?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; | ||
| $targets[] = \T_CLASS; | ||
|
|
||
| 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 ); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!'; | ||
| } |
57 changes: 57 additions & 0 deletions
57
phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| /** | ||
| * Unit tests for GenericNamesSniff. | ||
| * | ||
| * @package PluginCheck | ||
| */ | ||
|
|
||
| namespace PluginCheckCS\PluginCheck\Tests\CodeAnalysis; | ||
|
|
||
| use PluginCheckCS\PluginCheck\Sniffs\CodeAnalysis\GenericNamesSniff; | ||
| use PluginCheckCS\PluginCheck\Tests\AbstractSniffUnitTest; | ||
| use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
|
||
| /** | ||
| * Unit tests for GenericNamesSniff. | ||
| */ | ||
| final class GenericNamesUnitTest extends AbstractSniffUnitTest { | ||
|
|
||
| /** | ||
| * Returns the lines where errors should occur. | ||
| * | ||
| * @return array <int line number> => <int number of errors> | ||
| */ | ||
| public function getErrorList() { | ||
| return array( | ||
| 3 => 1, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the lines where warnings should occur. | ||
| * | ||
| * @return array <int line number> => <int number of warnings> | ||
| */ | ||
| public function getWarningList() { | ||
| return array(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the fully qualified class name (FQCN) of the sniff. | ||
| * | ||
| * @return string The fully qualified class name of the sniff. | ||
| */ | ||
| protected function get_sniff_fqcn() { | ||
| return GenericNamesSniff::class; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the parameters for the sniff. | ||
| * | ||
| * @throws \RuntimeException If unable to set the ruleset parameters required for the test. | ||
| * | ||
| * @param Sniff $sniff The sniff being tested. | ||
| */ | ||
| public function set_sniff_parameters( Sniff $sniff ) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/phpunit/testdata/plugins/test-plugin-generic-names-with-errors/load.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!'; | ||
| } |
21 changes: 21 additions & 0 deletions
21
tests/phpunit/testdata/plugins/test-plugin-generic-names-without-errors/load.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
40
tests/phpunit/tests/Checker/Checks/Generic_Names_Check_Tests.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the Generic_Names_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\Generic_Names_Check; | ||
|
|
||
| class Generic_Names_Check_Tests extends WP_UnitTestCase { | ||
|
|
||
| public function test_run_with_errors() { | ||
| $check = new Generic_Names_Check(); | ||
| $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-generic-names-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 Generic_Names_Check(); | ||
| $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-generic-names-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() ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.