-
Notifications
You must be signed in to change notification settings - Fork 109
add Abstract_PHP_CodeSniffer_Check #53
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
Merged
mukeshpanchal27
merged 9 commits into
trunk
from
feature/create-abstract-php-codesniffer-check
Jan 12, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
32450b6
add Abstract_PHP_CodeSniffer_Check
jjgrainger ed3244f
add PHPCS autoloader in run method
jjgrainger d67cce3
fix syntax
jjgrainger 716d3ea
Merge branch 'trunk' into feature/create-abstract-php-codesniffer-check
jjgrainger 6806ba9
fix comments and directory path
jjgrainger bf4e620
update parse_argv method
jjgrainger cbd20d3
update method docblocks
jjgrainger 1dbc933
fix lint issues
jjgrainger 3dc303d
Merge branch 'trunk' into feature/create-abstract-php-codesniffer-check
jjgrainger 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
140 changes: 140 additions & 0 deletions
140
includes/Checker/Checks/Abstract_PHP_CodeSniffer_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,140 @@ | ||
| <?php | ||
| /** | ||
| * Class WordPress\Plugin_Check\Checker\Checks\PHP_CodeSniffer_Check | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| namespace WordPress\Plugin_Check\Checker\Checks; | ||
|
|
||
| use WordPress\Plugin_Check\Checker\Check; | ||
| use WordPress\Plugin_Check\Checker\Check_Result; | ||
| use Exception; | ||
|
|
||
| /** | ||
| * Check for running one or more PHP CodeSniffer sniffs. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| abstract class Abstract_PHP_CodeSniffer_Check implements Check { | ||
|
|
||
| /** | ||
| * List of allowed PHPCS arguments. | ||
| * | ||
| * @since n.e.x.t | ||
| * @var array | ||
| */ | ||
| protected $allowed_args = array( | ||
| 'standard' => true, | ||
| 'extensions' => true, | ||
| 'sniffs' => true, | ||
| 'exclude' => true, | ||
| ); | ||
|
|
||
| /** | ||
| * Returns an associative array of arguments to pass to PHPCS. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return array { | ||
| * An associative array of PHPCS CLI arguments. Can include one or more of the following options. | ||
| * | ||
| * @type string $standard The name or path to the coding standard to check against. | ||
| * @type string $extensions A comma separated list of file extensions to check against. | ||
| * @type string $sniffs A comma separated list of sniff codes to include from checks. | ||
| * @type string $exclude A comma separated list of sniff codes to exclude from checks. | ||
| * } | ||
| */ | ||
| abstract public function get_args(); | ||
|
|
||
| /** | ||
| * Amends the given result by running the check on the associated plugin. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param Check_Result $result The check result to amend, including the plugin context to check. | ||
| * | ||
| * @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of | ||
| * the check). | ||
| */ | ||
| public function run( Check_Result $result ) { | ||
| // Include the PHPCS autoloader. | ||
| $autoloader = WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/vendor/squizlabs/php_codesniffer/autoload.php'; | ||
|
|
||
| if ( file_exists( $autoloader ) ) { | ||
| include_once $autoloader; | ||
| } | ||
|
|
||
| // Backup the original command line arguments. | ||
| $orig_cmd_args = $_SERVER['argv']; | ||
|
|
||
| // Create the default arguments for PHPCS. | ||
| $defaults = array( | ||
| '', | ||
| $result->plugin()->path( '' ), | ||
| '--report=Json', | ||
| '--report-width=9999', | ||
| ); | ||
|
|
||
| // Set the check arguments for PHPCS. | ||
| $_SERVER['argv'] = $this->parse_argv( $this->get_args(), $defaults ); | ||
|
|
||
| // Run PHPCS. | ||
| try { | ||
| ob_start(); | ||
| $runner = new \PHP_CodeSniffer\Runner(); | ||
| $runner->runPHPCS(); | ||
| $reports = ob_get_clean(); | ||
| } catch ( Exception $e ) { | ||
| $_SERVER['argv'] = $orig_cmd_args; | ||
| throw $e; | ||
| } | ||
|
|
||
| // Parse the reports into data to add to the overall $result. | ||
| $reports = json_decode( trim( $reports ), true ); | ||
|
|
||
| if ( empty( $reports['files'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ( $reports['files'] as $file_name => $file_results ) { | ||
| if ( empty( $file_results['messages'] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ( $file_results['messages'] as $file_message ) { | ||
| $result->add_message( | ||
| strtoupper( $file_message['type'] ) === 'ERROR', | ||
| $file_message['message'], | ||
| array( | ||
| 'code' => $file_message['source'], | ||
| 'file' => $file_name, | ||
| 'line' => $file_message['line'], | ||
| 'column' => $file_message['column'], | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parse the command arguments. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param array $argv An array of arguments to pass. | ||
| * @param array $defaults An array of default arguments. | ||
| * @return array An indexed array of PHPCS CLI arguments. | ||
| */ | ||
| private function parse_argv( $argv, $defaults ) { | ||
| // Only accept allowed PHPCS arguments from check arguments array. | ||
| $check_args = array_intersect_key( $argv, $this->allowed_args ); | ||
|
|
||
| // Format check arguments for PHPCS. | ||
| foreach ( $check_args as $key => $value ) { | ||
| $defaults[] = "--{$key}=$value"; | ||
| } | ||
|
|
||
| return $defaults; | ||
| } | ||
| } | ||
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.