Skip to content

Commit

Permalink
Merge pull request #16 from chrisryan/master
Browse files Browse the repository at this point in the history
Update to support php_codesniffer version 3.
  • Loading branch information
chadicus authored Sep 15, 2022
2 parents b0ec63a + def6ca4 commit 3a91f00
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 83 deletions.
20 changes: 13 additions & 7 deletions Chadicus/Sniffs/Classes/UnusedUseStatementSniff.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php

class Chadicus_Sniffs_Classes_UnusedUseStatementSniff implements PHP_CodeSniffer_Sniff
namespace Chadicus\Sniffs\Classes;

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

class UnusedUseStatementSniff implements Sniff
{

/**
Expand All @@ -17,13 +23,13 @@ public function register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

Expand All @@ -39,7 +45,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}

$classPtr = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
Tokens::$emptyTokens,
($semiColon - 1),
null,
true
Expand Down Expand Up @@ -101,7 +107,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}

$beforeUsage = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
Tokens::$emptyTokens,
($classUsed - 1),
null,
true
Expand Down
19 changes: 13 additions & 6 deletions Chadicus/Sniffs/Commenting/ClassCommentSniff.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php

namespace Chadicus\Sniffs\Commenting;

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

/**
* Parses and verifies the class doc comment.
*
Expand All @@ -12,7 +19,7 @@
* <li>Disallows throws, return, var and param tags.</li>
* </ul>
*/
final class Chadicus_Sniffs_Commenting_ClassCommentSniff implements PHP_CodeSniffer_Sniff
final class ClassCommentSniff implements Sniff
{
public $disallowedTags = array('@throws', '@return', '@var', '@param');

Expand All @@ -29,16 +36,16 @@ public function register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$find = PHP_CodeSniffer_Tokens::$methodPrefixes;
$find = Tokens::$methodPrefixes;
$find[] = T_WHITESPACE;

$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
Expand Down
69 changes: 38 additions & 31 deletions Chadicus/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php

class Chadicus_Sniffs_Commenting_FunctionCommentSniff extends PEAR_Sniffs_Commenting_FunctionCommentSniff
namespace Chadicus\Sniffs\Commenting;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff as PEARFunctionCommentSniff;
use PHP_CodeSniffer\Util\Common;

class FunctionCommentSniff extends PEARFunctionCommentSniff
{
/**
* The current PHP version.
Expand All @@ -19,12 +26,12 @@ class Chadicus_Sniffs_Commenting_FunctionCommentSniff extends PEAR_Sniffs_Commen
/**
* Ensures only public methods are processed.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr)
{
$methodProperties = $phpcsFile->getMethodProperties($stackPtr);
if ($methodProperties['scope'] !== 'public') {
Expand All @@ -37,14 +44,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
/**
* Process the return comment of this function comment.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
{
$tokens = $phpcsFile->getTokens();

Expand Down Expand Up @@ -187,14 +194,14 @@ private function isReturnVoid($phpcsFile, $tokens, $stackPtr) : bool
/**
* Process any throw tags that this function comment has.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
protected function processThrows(File $phpcsFile, $stackPtr, $commentStart)
{
$tokens = $phpcsFile->getTokens();

Expand Down Expand Up @@ -242,17 +249,17 @@ protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
/**
* Process the function parameter comments.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
{
if ($this->_phpVersion === null) {
$this->_phpVersion = PHP_CodeSniffer::getConfigData('php_version');
$this->_phpVersion = Config::getConfigData('php_version');
if ($this->_phpVersion === null) {
$this->_phpVersion = PHP_VERSION_ID;
}
Expand Down Expand Up @@ -388,7 +395,7 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
$suggestedTypeHint = 'callable';
} else if (strpos($suggestedName, 'callback') !== false) {
$suggestedTypeHint = 'callable';
} else if (in_array($suggestedName, PHP_CodeSniffer::$allowedTypes) === false) {
} else if (in_array($suggestedName, Common::$allowedTypes) === false) {
$suggestedTypeHint = $suggestedName;
}

Expand Down Expand Up @@ -548,14 +555,14 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
/**
* Check the spacing after the type of a parameter.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param array $param The parameter to be checked.
* @param int $maxType The maxlength of the longest parameter type.
* @param int $spacing The number of spaces to add after the type.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param array $param The parameter to be checked.
* @param int $maxType The maxlength of the longest parameter type.
* @param int $spacing The number of spaces to add after the type.
*
* @return void
*/
protected function checkSpacingAfterParamType(PHP_CodeSniffer_File $phpcsFile, $param, $maxType, $spacing = 1)
protected function checkSpacingAfterParamType(File $phpcsFile, $param, $maxType, $spacing = 1)
{
// Check number of spaces after the type.
$spaces = ($maxType - strlen($param['type']) + $spacing);
Expand Down Expand Up @@ -602,14 +609,14 @@ protected function checkSpacingAfterParamType(PHP_CodeSniffer_File $phpcsFile, $
/**
* Check the spacing after the name of a parameter.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param array $param The parameter to be checked.
* @param int $maxVar The maxlength of the longest parameter name.
* @param int $spacing The number of spaces to add after the type.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param array $param The parameter to be checked.
* @param int $maxVar The maxlength of the longest parameter name.
* @param int $spacing The number of spaces to add after the type.
*
* @return void
*/
protected function checkSpacingAfterParamName(PHP_CodeSniffer_File $phpcsFile, $param, $maxVar, $spacing = 1)
protected function checkSpacingAfterParamName(File $phpcsFile, $param, $maxVar, $spacing = 1)
{
// Check number of spaces after the var name.
$spaces = ($maxVar - strlen($param['var']) + $spacing);
Expand Down
31 changes: 18 additions & 13 deletions Chadicus/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<?php

final class Chadicus_Sniffs_Commenting_VariableCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
namespace Chadicus\Sniffs\Commenting;

use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Files\File;

final class VariableCommentSniff extends AbstractVariableSniff
{
/**
* Called to process class member vars.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
public function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$ignore = array(T_PUBLIC, T_PRIVATE, T_PROTECTED, T_VAR, T_STATIC, T_WHITESPACE);
Expand Down Expand Up @@ -81,13 +86,13 @@ public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
*
* Not required for this sniff.
*
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found.
* @param int $stackPtr The position where the double quoted
* string was found.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this token was found.
* @param int $stackPtr The position where the double quoted
* string was found.
*
* @return void
*/
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
protected function processVariable(File $phpcsFile, $stackPtr)
{

}
Expand All @@ -97,13 +102,13 @@ protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
*
* Not required for this sniff.
*
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found.
* @param int $stackPtr The position where the double quoted
* string was found.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this token was found.
* @param int $stackPtr The position where the double quoted
* string was found.
*
* @return void
*/
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
protected function processVariableInString(File $phpcsFile, $stackPtr)
{

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php

namespace Chadicus\Sniffs\ConstrolStructures;

use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Files\File;

/**
* Generates a warning if else or elseif control structures are used.
*/
final class Chadicus_Sniffs_ControlStructures_ElseIfAndElseDeclarationSniff
extends PHP_CodeSniffer_Standards_AbstractScopeSniff
final class ElseIfAndElseDeclarationSniff extends AbstractScopeSniff
{
/**
* Constructs the test with the tokens it wishes to listen for.
Expand All @@ -16,18 +21,30 @@ public function __construct()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @param int $currScope A pointer to the start of the scope.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @param int $currScope A pointer to the start of the scope.
*
* @return void
*/
public function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$error = 'Use of ELSE and ELSEIF is discouraged. An if expression with an else branch is never necessary. You '
. 'can rewrite the conditions in a way that the else is not necessary and the code becomes simpler to '
. 'read.';
$phpcsFile->addWarning($error, $stackPtr, 'Discouraged');
}

/**
* Process of tokens outside of scope.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @return void
*/
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
{
}
}
13 changes: 9 additions & 4 deletions Chadicus/Sniffs/Methods/LongMethodSniff.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php

namespace Chadicus\Sniffs\Methods;

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

/**
* Issues warning with method length greater than 100 and issues error if length is greater than 200.
*/
final class Chadicus_Sniffs_Methods_LongMethodSniff implements PHP_CodeSniffer_Sniff
final class LongMethodSniff implements Sniff
{
/**
* Length at which a warning will be triggered.
Expand All @@ -30,12 +35,12 @@ public function register()
}

/**
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param intege $stackPtr The position in the stack where the token was found.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
* @param intege $stackPtr The position in the stack where the token was found.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
Expand Down
Loading

0 comments on commit 3a91f00

Please sign in to comment.