Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
Upgrate to WPCS 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aubreypwd committed May 3, 2019
1 parent f5d4db6 commit a210b73
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 51 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -26,6 +26,10 @@ ___________________

# Changelog

## 2.0.0

PHPCS Upgrade Guide: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Version-3.0-Upgrade-Guide

## 1.2.0

- WordPress Coding Standards update to `1.2.1`
Expand Down
54 changes: 30 additions & 24 deletions WebDevStudios/Sniffs/All/BaseSniff.php
Expand Up @@ -7,14 +7,16 @@
*/

namespace WebDevStudios\Sniffs\All;
use PHP_CodeSniffer_Sniff;

use \PHP_CodeSniffer\Sniffs\Sniff;
use \PHP_CodeSniffer\Files\File;

/**
* Base class for extending so you can get the below common tools.
*
* @since 1.1.0
*/
abstract class BaseSniff implements PHP_CodeSniffer_Sniff {
abstract class BaseSniff implements Sniff {

/**
* The tokens.
Expand All @@ -32,10 +34,10 @@ abstract class BaseSniff implements PHP_CodeSniffer_Sniff {
* @author Aubrey Portwood
* @since 1.1.0
*
* @param PHP_CodeSniffer_File $file The file.
* @param int $where Where the error happened.
* @param string $message The message.
* @param int $severity The severity, defaults to 0.
* @param \PHP_CodeSniffer\Files\File $file The file.
* @param int $where Where the error happened.
* @param string $message The message.
* @param int $severity The severity, defaults to 0.
*/
protected function error( &$file, $where, $message, $severity = 0 ) {
$this->console( (object) array(
Expand All @@ -52,10 +54,10 @@ protected function error( &$file, $where, $message, $severity = 0 ) {
* @author Aubrey Portwood
* @since 1.1.0
*
* @param PHP_CodeSniffer_File $file The file.
* @param int $where Where the warning happened.
* @param string $message The message.
* @param int $severity The severity, defaults to 0.
* @param \PHP_CodeSniffer\Files\File $file The file.
* @param int $where Where the warning happened.
* @param string $message The message.
* @param int $severity The severity, defaults to 0.
*/
protected function warn( &$file, $where, $message, $severity = 0 ) {
$this->console( (object) array(
Expand All @@ -72,13 +74,14 @@ protected function warn( &$file, $where, $message, $severity = 0 ) {
* @author Aubrey Portwood
* @since 1.1.0
*
* @param array $args {
* @param array $args {
* Arguments.
* @type string $message The message.
* @type int $start The starting position of the record.
* @type string $log Whether to log an `error` or a `warning`.
* }
* @param PHP_CodeSniffer_File $phpcs_file The file.
*
* @param \PHP_CodeSniffer\Files\File $phpcs_file The file.
*
* @return void Early bail when we finally log to the console.
*/
Expand Down Expand Up @@ -149,17 +152,20 @@ protected function get_token_content( $token ) {
}

/**
* Wrapper for PHP_CodeSniffer_File->findNext() with validation.
* Wrapper for File->findNext() with validation.
*
* @param PHP_CodeSniffer_File $file The file.
* @param int $token_type The token type e.g. from http://php.net/manual/en/tokens.php.
* @param string $validate The token type for validation..
* @param int $start The position to start searching.
* @param int $end The position to stop searching.
* @param boolean $local Whether to go past $end and search again.
* @param \PHP_CodeSniffer\Files\File $file The file.
* @param int $token_type The token type e.g. from http://php.net/manual/en/tokens.php.
* @param string $validate The token type for validation..
* @param int $start The position to start searching.
* @param int $end The position to stop searching.
* @param boolean $local Whether to go past $end and search again.
*
* @return boolean|mixed False if the token type did not validate, the value of findNext else.
*
* @since Unknown
* @author Aubrey Portwood <aubrey@webdevstudios.com>
*
* @see https://pear.php.net/package/PHP_CodeSniffer/docs/3.2.3/apidoc/PHP_CodeSniffer/File.html#methodfindNext
*/
protected function find_next( $file, $token_type, $validate = null, $start, $end = null, $local = false ) {
Expand All @@ -184,11 +190,11 @@ protected function find_next( $file, $token_type, $validate = null, $start, $end
* @author Aubrey Portwood <aubrey@webdevstudios.com>
* @since 1.2.0
*
* @param PHP_CodeSniffer_File $file File.
* @param int $token_type The Token Type Code.
* @param string $token_name The token name e.g. T_FUNCTION for validation.
* @param int $position The position of the current token.
* @return boolean True if, on the next line, that token type is found.
* @param \PHP_CodeSniffer\Files\File $file File.
* @param int $token_type The Token Type Code.
* @param string $token_name The token name e.g. T_FUNCTION for validation.
* @param int $position The position of the current token.
* @return boolean True if, on the next line, that token type is found.
*/
protected function next_line_is_token_type( $file, $token_type, $token_name, $position ) {
$token = $this->get_token( $position );
Expand Down
14 changes: 6 additions & 8 deletions WebDevStudios/Sniffs/All/RequireAuthorSniff.php
Expand Up @@ -10,8 +10,8 @@

namespace WebDevStudios\Sniffs\All;

use PHP_CodeSniffer_Sniff;
use PHP_CodeSniffer_File;
use \PHP_CodeSniffer\Sniffs\Sniff;
use \PHP_CodeSniffer\Files\File;

/**
* Require the return tag.
Expand Down Expand Up @@ -59,13 +59,12 @@ public function register() {
/**
* Process file.
*
* @param PHP_CodeSniffer_File $file The file object.
* @param int $start Where the docblock starts.
* @param \PHP_CodeSniffer\Files\File $file The file object.
* @param int $start Where the docblock starts.
*
* @since 1.2.0
* @return void Early bail if we don't enforce this on the docblock.
*/
public function process( PHP_CodeSniffer_File $file, $start ) {
public function process( File $file, $start ) {

// Get the tokens.
$this->tokens = $file->getTokens();
Expand Down Expand Up @@ -93,7 +92,7 @@ public function process( PHP_CodeSniffer_File $file, $start ) {

// But not commented out.
&& ! $this->next_line_is_token_type( $file, T_COMMENT, 'T_COMMENT', $end_position ),
) );
), true );

// We're enforcing this...
if ( $enforce ) {
Expand All @@ -118,7 +117,6 @@ public function process( PHP_CodeSniffer_File $file, $start ) {
// We didn't find @author, let them know.
$this->warn( $file, $end_position, 'Documenting @author is helpful. If the author is unknown, you can use @author Unknown.' );
}

} // enforce.
}
}
29 changes: 17 additions & 12 deletions WebDevStudios/Sniffs/All/RequireReturnSniff.php
Expand Up @@ -10,8 +10,9 @@
*/

namespace WebDevStudios\Sniffs\All;
use PHP_CodeSniffer_Sniff;
use PHP_CodeSniffer_File;

use \PHP_CodeSniffer\Sniffs\Sniff;
use \PHP_CodeSniffer\Files\File;

/**
* Require the return tag.
Expand Down Expand Up @@ -61,13 +62,15 @@ public function register() {
* @author Aubrey Portwood
* @since 1.1.0
*
* @param PHP_CodeSniffer_File $file The file object.
* @param int $doc_block_start Where the docblock starts.
* @return void Skips errors when not working with functions.
* @param \PHP_CodeSniffer\Files\File $file The file object.
* @param int $doc_block_start Where the docblock starts.
* @return void Skips errors when not working with functions.
*/
public function process( PHP_CodeSniffer_File $file, $doc_block_start ) {
public function process( File $file, $doc_block_start ) {
$this->tokens = $file->getTokens();

$token = $this->tokens[ $doc_block_start ];

$doc_block_end = $token['comment_closer'];

// The @ return in the comment block, false by default.
Expand Down Expand Up @@ -104,16 +107,16 @@ public function process( PHP_CodeSniffer_File $file, $doc_block_start ) {
/**
* Examine a function, and get some context about whether it has a return statement or not.
*
* @param PHP_CodeSniffer_File $file The file.
* @param array $args {
* @param \PHP_CodeSniffer\Files\File $file The file.
* @param array $args {
* Arguments.
* @type string $doc_block_end Where the docblock ends.
* }
*
* @since 1.1.0
* @return string Contextual information about the function (if it is a function).
*/
protected function examine_function( PHP_CodeSniffer_File &$file, $args ) {
protected function examine_function( File &$file, $args ) {

// See if we can find a function start.
$function_start = $file->findNext( T_FUNCTION, $args->doc_block_end );
Expand All @@ -124,7 +127,9 @@ protected function examine_function( PHP_CodeSniffer_File &$file, $args ) {
}

$doc_block_end_line = $this->get_token( $args->doc_block_end, 'line' );

$function_start_line = $this->get_token( $function_start, 'line' );

if ( $function_start_line !== $doc_block_end_line + 1 ) {

// This also isn't a function, it's okay.
Expand Down Expand Up @@ -166,7 +171,7 @@ protected function examine_function( PHP_CodeSniffer_File &$file, $args ) {
isset( $return['nested_parenthesis'] ) ||

// If it's condition is a closure.
isset( $return['conditions'] ) && in_array( 'PHPCS_T_CLOSURE', $return['conditions'] )
isset( $return['conditions'] ) && in_array( 'PHPCS_T_CLOSURE', $return['conditions'], true )

) &&

Expand All @@ -192,8 +197,8 @@ protected function examine_function( PHP_CodeSniffer_File &$file, $args ) {
/**
* Find whether the abstract keyword is present in a function.
*
* @param PHP_CodeSniffer_File $file Reference to the current file.
* @param object $args Current working arguments.
* @param \PHP_CodeSniffer\Files\File $file Reference to the current file.
* @param object $args Current working arguments.
*
* @author Jeremy Ward <jeremy.ward@webdevstudios.com>
* @since 2018-11-21
Expand Down
10 changes: 5 additions & 5 deletions WebDevStudios/Sniffs/All/RequireSinceSniff.php
Expand Up @@ -10,8 +10,8 @@

namespace WebDevStudios\Sniffs\All;

use PHP_CodeSniffer_Sniff;
use PHP_CodeSniffer_File;
use \PHP_CodeSniffer\Sniffs\Sniff;
use \PHP_CodeSniffer\Files\File;

/**
* Require the return tag.
Expand Down Expand Up @@ -61,8 +61,8 @@ public function register() {
*
* @author Aubrey Portwood
*
* @param PHP_CodeSniffer_File $file The file object.
* @param int $doc_block_start Where the docblock starts.
* @param \PHP_CodeSniffer\Files\File $file The file object.
* @param int $doc_block_start Where the docblock starts.
*
* @since 1.1.0
*
Expand All @@ -71,7 +71,7 @@ public function register() {
*
* @return void Early bail if a WordPress theme.
*/
public function process( PHP_CodeSniffer_File $file, $doc_block_start ) {
public function process( File $file, $doc_block_start ) {
$this->tokens = $file->getTokens();
$token = $this->tokens[ $doc_block_start ];
$doc_block_end = $token['comment_closer'];
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -5,7 +5,7 @@
"minimum-stability": "stable",
"authors": [],
"require": {
"wp-coding-standards/wpcs": "1.2.1",
"squizlabs/php_codesniffer": "3.3.2"
"wp-coding-standards/wpcs": "2.1.0",
"squizlabs/php_codesniffer": "3.3.1"
}
}

0 comments on commit a210b73

Please sign in to comment.