- Added new sniffs for codesniffer to be compliant with the FHComplete coding style

- Commented tests/codesniffer/FHComplete/ruleset.xml
- Removed redundant PHPCS checks already performed by PHPMD
- Added/removed checks from PHPCS to be compliant with the FHComplete coding style
- PHPMD: Removed the ElseExpression rule from the rulesets/cleancode.xml
This commit is contained in:
Paolo
2022-03-04 19:59:57 +01:00
parent d4d1320503
commit 9f28db36b8
6 changed files with 583 additions and 26 deletions
@@ -0,0 +1,143 @@
<?php
/**
* Verifies that properties are declared correctly.
*
* NOTE: It simply overrides the method processMemberVar of the Standards\PSR2\Sniffs\Classes\PropertyDeclarationSniff class
*
*/
namespace PHP_CodeSniffer\Standards\FHComplete\Sniffs\Classes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Util\Tokens;
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\PropertyDeclarationSniff;
class FHCPropertyDeclarationSniff extends PropertyDeclarationSniff
{
/**
* Processes the function tokens within the class.
*
* NOTE: it does not check if the property name starts with an underscore "_"
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Detect multiple properties defined at the same time. Throw an error
// for this, but also only process the first property in the list so we don't
// repeat errors.
$find = Tokens::$scopeModifiers;
$find[] = T_VARIABLE;
$find[] = T_VAR;
$find[] = T_SEMICOLON;
$find[] = T_OPEN_CURLY_BRACKET;
$prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
if ($tokens[$prev]['code'] === T_VARIABLE) {
return;
}
if ($tokens[$prev]['code'] === T_VAR) {
$error = 'The var keyword must not be used to declare a property';
$phpcsFile->addError($error, $stackPtr, 'VarUsed');
}
$next = $phpcsFile->findNext([T_VARIABLE, T_SEMICOLON], ($stackPtr + 1));
if ($next !== false && $tokens[$next]['code'] === T_VARIABLE) {
$error = 'There must not be more than one property declared per statement';
$phpcsFile->addError($error, $stackPtr, 'Multiple');
}
try {
$propertyInfo = $phpcsFile->getMemberProperties($stackPtr);
if (empty($propertyInfo) === true) {
return;
}
} catch (\Exception $e) {
// Turns out not to be a property after all.
return;
}
if ($propertyInfo['type'] !== '') {
$typeToken = $propertyInfo['type_end_token'];
$error = 'There must be 1 space after the property type declaration; %s found';
if ($tokens[($typeToken + 1)]['code'] !== T_WHITESPACE) {
$data = ['0'];
$fix = $phpcsFile->addFixableError($error, $typeToken, 'SpacingAfterType', $data);
if ($fix === true) {
$phpcsFile->fixer->addContent($typeToken, ' ');
}
} else if ($tokens[($typeToken + 1)]['content'] !== ' ') {
$next = $phpcsFile->findNext(T_WHITESPACE, ($typeToken + 1), null, true);
if ($tokens[$next]['line'] !== $tokens[$typeToken]['line']) {
$found = 'newline';
} else {
$found = $tokens[($typeToken + 1)]['length'];
}
$data = [$found];
$nextNonWs = $phpcsFile->findNext(Tokens::$emptyTokens, ($typeToken + 1), null, true);
if ($nextNonWs !== $next) {
$phpcsFile->addError($error, $typeToken, 'SpacingAfterType', $data);
} else {
$fix = $phpcsFile->addFixableError($error, $typeToken, 'SpacingAfterType', $data);
if ($fix === true) {
if ($found === 'newline') {
$phpcsFile->fixer->beginChangeset();
for ($x = ($typeToken + 1); $x < $next; $x++) {
$phpcsFile->fixer->replaceToken($x, '');
}
$phpcsFile->fixer->addContent($typeToken, ' ');
$phpcsFile->fixer->endChangeset();
} else {
$phpcsFile->fixer->replaceToken(($typeToken + 1), ' ');
}
}
}
}//end if
}//end if
if ($propertyInfo['scope_specified'] === false) {
$error = 'Visibility must be declared on property "%s"';
$data = [$tokens[$stackPtr]['content']];
$phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
}
if ($propertyInfo['scope_specified'] === true && $propertyInfo['is_static'] === true) {
$scopePtr = $phpcsFile->findPrevious(Tokens::$scopeModifiers, ($stackPtr - 1));
$staticPtr = $phpcsFile->findPrevious(T_STATIC, ($stackPtr - 1));
if ($scopePtr < $staticPtr) {
return;
}
$error = 'The static declaration must come after the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'StaticBeforeVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($scopePtr + 1); $scopePtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($scopePtr, '');
$phpcsFile->fixer->addContentBefore($staticPtr, $propertyInfo['scope'].' ');
$phpcsFile->fixer->endChangeset();
}
}//end if
}//end processMemberVar()
}//end class
@@ -0,0 +1,101 @@
<?php
/**
* Ensures classes are in camel caps, and the first letter is capitalised.
*
* NOTE:
* - it simply overrides the method process of the Standards\Squiz\Sniffs\Classes\ValidClassNameSniff class
* - it contains a new implementation of PHP_CodeSniffer\Util\Common::isCamelCaps
*
*/
namespace PHP_CodeSniffer\Standards\FHComplete\Sniffs\Classes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ValidClassNameSniff;
class FHCValidClassNameSniff extends ValidClassNameSniff
{
/**
* Processes this test, when one of its tokens is encountered.
*
* NOTE: it does not check if the class name contains an underscore "_"
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
$error = 'Possible parse error: %s missing opening or closing brace';
$data = [$tokens[$stackPtr]['content']];
$phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
return;
}
// Determine the name of the class or interface. Note that we cannot
// simply look for the first T_STRING because a class name
// starting with the number will be multiple tokens.
$opener = $tokens[$stackPtr]['scope_opener'];
$nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true);
$nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener);
if ($nameEnd === false) {
$name = $tokens[$nameStart]['content'];
} else {
$name = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart)));
}
// Check for PascalCase format.
$valid = $this->isCamelCaps($name);
if ($valid === false) {
$type = ucfirst($tokens[$stackPtr]['content']);
$error = '%s name "%s" is not in PascalCase format';
$data = [
$type,
$name,
];
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
$phpcsFile->recordMetric($stackPtr, 'PascalCase class name', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'PascalCase class name', 'yes');
}
}//end process()
/**
* Returns true if the specified string is in the camel caps format.
*
* NOTE:
* - it does not allow the string to start with an underscore "_"
* - it does allow that the string contains an underscore "_"
* - the string must starts with a capitol letter
*
* @param string $string The string the verify.
*
* @return boolean
*/
private function isCamelCaps($string)
{
$legalFirstChar = '[A-Z]';
if (preg_match("/^$legalFirstChar/", $string) === 0) {
return false;
}
// Check that the name only contains legal characters.
$legalChars = 'a-zA-Z0-9_';
if (preg_match("|[^$legalChars]|", substr($string, 1)) > 0) {
return false;
}
return true;
}//end isCamelCaps()
}//end class
@@ -0,0 +1,154 @@
<?php
/**
* Verifies that control statements conform to their coding standards.
*
* NOTE: It simply overrides the method process of the PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\ControlSignatureSniff class
*
*/
namespace PHP_CodeSniffer\Standards\FHComplete\Sniffs\ControlStructures;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\ControlSignatureSniff;
class FHCControlSignatureSniff extends ControlSignatureSniff
{
/**
* Processes this test, when one of its tokens is encountered.
*
* NOTE:
* - Does not force to have whitespaces after the brackets
* - Allows to have a bracket at newline after a control structure, ex:
*
* if (condition)
* {
*
* @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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($nextNonEmpty === false) {
return;
}
$isAlternative = false;
if (isset($tokens[$stackPtr]['scope_opener']) === true
&& $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_COLON
) {
$isAlternative = true;
}
// Single newline after opening brace.
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
$opener = $tokens[$stackPtr]['scope_opener'];
for ($next = ($opener + 1); $next < $phpcsFile->numTokens; $next++) {
$code = $tokens[$next]['code'];
if ($code === T_WHITESPACE
|| ($code === T_INLINE_HTML
&& trim($tokens[$next]['content']) === '')
) {
continue;
}
// Skip all empty tokens on the same line as the opener.
if ($tokens[$next]['line'] === $tokens[$opener]['line']
&& (isset(Tokens::$emptyTokens[$code]) === true
|| $code === T_CLOSE_TAG)
) {
continue;
}
// We found the first bit of a code, or a comment on the
// following line.
break;
}//end for
if ($tokens[$next]['line'] === $tokens[$opener]['line']) {
$error = 'Newline required after opening brace';
$fix = $phpcsFile->addFixableError($error, $opener, 'NewlineAfterOpenBrace');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($opener + 1); $i < $next; $i++) {
if (trim($tokens[$i]['content']) !== '') {
break;
}
// Remove whitespace.
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->addContent($opener, $phpcsFile->eolChar);
$phpcsFile->fixer->endChangeset();
}
}//end if
} else if ($tokens[$stackPtr]['code'] === T_WHILE) {
// Zero spaces after parenthesis closer, but only if followed by a semicolon.
$closer = $tokens[$stackPtr]['parenthesis_closer'];
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($closer + 1), null, true);
if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_SEMICOLON) {
$found = 0;
if ($tokens[($closer + 1)]['code'] === T_WHITESPACE) {
if (strpos($tokens[($closer + 1)]['content'], $phpcsFile->eolChar) !== false) {
$found = 'newline';
} else {
$found = $tokens[($closer + 1)]['length'];
}
}
if ($found !== 0) {
$error = 'Expected 0 spaces before semicolon; %s found';
$data = [$found];
$fix = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeSemicolon', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($closer + 1), '');
}
}
}
}//end if
// Only want to check multi-keyword structures from here on.
if ($tokens[$stackPtr]['code'] === T_WHILE) {
if (isset($tokens[$stackPtr]['scope_closer']) !== false) {
return;
}
$closer = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($closer === false
|| $tokens[$closer]['code'] !== T_CLOSE_CURLY_BRACKET
|| $tokens[$tokens[$closer]['scope_condition']]['code'] !== T_DO
) {
return;
}
} else if ($tokens[$stackPtr]['code'] === T_ELSE
|| $tokens[$stackPtr]['code'] === T_ELSEIF
|| $tokens[$stackPtr]['code'] === T_CATCH
|| $tokens[$stackPtr]['code'] === T_FINALLY
) {
if (isset($tokens[$stackPtr]['scope_opener']) === true
&& $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_COLON
) {
// Special case for alternate syntax, where this token is actually
// the closer for the previous block, so there is no spacing to check.
return;
}
$closer = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($closer === false || $tokens[$closer]['code'] !== T_CLOSE_CURLY_BRACKET) {
return;
}
} else {
return;
}//end if
}//end process()
}//end class
@@ -0,0 +1,129 @@
<?php
/**
* Checks that the method declaration is correct.
*
* NOTE: It simply overrides the method processTokenWithinScope of the Standards\PSR2\Sniffs\Methods\MethodDeclarationSniff class
*
*/
namespace PHP_CodeSniffer\Standards\FHComplete\Sniffs\Methods;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Util\Tokens;
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\MethodDeclarationSniff;
class FHCMethodDeclarationSniff extends MethodDeclarationSniff
{
/**
* Processes the function tokens within the class.
*
* NOTE: it does not check if the method name starts with an underscore "_"
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
* @param int $currScope The current scope opener token.
*
* @return void
*/
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$tokens = $phpcsFile->getTokens();
// Determine if this is a function which needs to be examined.
$conditions = $tokens[$stackPtr]['conditions'];
end($conditions);
$deepestScope = key($conditions);
if ($deepestScope !== $currScope) {
return;
}
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$visibility = 0;
$static = 0;
$abstract = 0;
$final = 0;
$find = (Tokens::$methodPrefixes + Tokens::$emptyTokens);
$prev = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
$prefix = $stackPtr;
while (($prefix = $phpcsFile->findPrevious(Tokens::$methodPrefixes, ($prefix - 1), $prev)) !== false) {
switch ($tokens[$prefix]['code']) {
case T_STATIC:
$static = $prefix;
break;
case T_ABSTRACT:
$abstract = $prefix;
break;
case T_FINAL:
$final = $prefix;
break;
default:
$visibility = $prefix;
break;
}
}
$fixes = [];
if ($visibility !== 0 && $final > $visibility) {
$error = 'The final declaration must precede the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $final, 'FinalAfterVisibility');
if ($fix === true) {
$fixes[$final] = '';
$fixes[($final + 1)] = '';
if (isset($fixes[$visibility]) === true) {
$fixes[$visibility] = 'final '.$fixes[$visibility];
} else {
$fixes[$visibility] = 'final '.$tokens[$visibility]['content'];
}
}
}
if ($visibility !== 0 && $abstract > $visibility) {
$error = 'The abstract declaration must precede the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $abstract, 'AbstractAfterVisibility');
if ($fix === true) {
$fixes[$abstract] = '';
$fixes[($abstract + 1)] = '';
if (isset($fixes[$visibility]) === true) {
$fixes[$visibility] = 'abstract '.$fixes[$visibility];
} else {
$fixes[$visibility] = 'abstract '.$tokens[$visibility]['content'];
}
}
}
if ($static !== 0 && $static < $visibility) {
$error = 'The static declaration must come after the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $static, 'StaticBeforeVisibility');
if ($fix === true) {
$fixes[$static] = '';
$fixes[($static + 1)] = '';
if (isset($fixes[$visibility]) === true) {
$fixes[$visibility] .= ' static';
} else {
$fixes[$visibility] = $tokens[$visibility]['content'].' static';
}
}
}
// Batch all the fixes together to reduce the possibility of conflicts.
if (empty($fixes) === false) {
$phpcsFile->fixer->beginChangeset();
foreach ($fixes as $stackPtr => $content) {
$phpcsFile->fixer->replaceToken($stackPtr, $content);
}
$phpcsFile->fixer->endChangeset();
}
}//end processTokenWithinScope()
}//end class
+51 -24
View File
@@ -10,56 +10,83 @@
<exclude-pattern>application/extensions</exclude-pattern>
<exclude-pattern>addons</exclude-pattern>
<!-- This ruleset is based mainly on the PSR2 standard ruleset, exluding the following rules: -->
<rule ref="PSR2">
<!-- Forces the use of namespaces -->
<exclude name="PSR1.Classes.ClassDeclaration" />
<exclude name="PSR1.Methods.CamelCapsMethodName" />
<exclude name="PSR1.Files.SideEffects" />
<exclude name="Generic.WhiteSpace.DisallowTabIndent" />
<exclude name="Squiz.ControlStructures.ControlSignature" />
<!-- Forbids the use of underscores at the beginning of properties name -->
<exclude name="PSR2.Classes.PropertyDeclaration" />
<!-- Forbids the use of classes name that contain underscores -->
<exclude name="Squiz.Classes.ValidClassName" />
<!-- Forbids the use inline control structure (ex. an if statement on one line) -->
<exclude name="Generic.ControlStructures.InlineControlStructure" />
<exclude name="Squiz.PHP.DisallowSizeFunctionsInLoops" />
<!-- Forces to have the bracket on the same line of the statement -->
<exclude name="Squiz.ControlStructures.ControlSignature" />
<!-- Forces to have in a single file only a class declaration, an if statement outside the class is forbidden -->
<exclude name="PSR1.Files.SideEffects" />
<!-- Forbids to have an empty line at the end of the file -->
<exclude name="PSR2.Files.EndFileNewline" />
<!-- Forbids the use of underscores at the beginning of methods name -->
<exclude name="PSR2.Methods.MethodDeclaration" />
<!-- Forbids the use of tabs -->
<exclude name="Generic.WhiteSpace.DisallowTabIndent" />
</rule>
<!-- Do not prompt any warning about the line length -->
<!-- Prompt an error if the line length is more than 150 -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
<property name="lineLimit" value="9999"/>
<property name="absoluteLineLimit" value="150"/>
</properties>
</rule>
<!-- No white spaces are allowed in array declaration between brackets -->
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
<!-- Ensures all class keywords are lowercase -->
<rule ref="Squiz.Classes.LowercaseClassKeywords"/>
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
<!-- Forbids the use of TODO in the comments -->
<rule ref="Generic.Commenting.Todo"/>
<!-- Tests that the stars in a doc comment align correctly -->
<rule ref="Squiz.Commenting.DocCommentAlignment"/>
<rule ref="Generic.Files.LineEndings"/>
<!-- Ensures the file ends with a newline character -->
<rule ref="Generic.Files.EndFileNewline"/>
<!-- Ensures there is no space after cast tokens -->
<rule ref="Generic.Formatting.NoSpaceAfterCast"/>
<!-- Ensures logical operators 'and' and 'or' are not used -->
<rule ref="Squiz.Operators.ValidLogicalOperators"/>
<rule ref="Squiz.PHP.Eval"/>
<rule ref="Squiz.PHP.NonExecutableCode"/>
<rule ref="Generic.PHP.NoSilencedErrors"/>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="Generic.PHP.DeprecatedFunctions"/>
<rule ref="Squiz.Scope.MemberVarScope"/>
<rule ref="Squiz.Scope.StaticThisUsage"/>
<!-- Ensure cast statements don't contain whitespace -->
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>
<!-- Verifies that operators have valid spacing surrounding them -->
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
<!-- Checks that control structures are defined and indented correctly -->
<!-- The listed tokens are ignored: comment, doc comment open tag, php close tag -->
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="ignoreIndentationTokens" type="array">
<element value="T_COMMENT"/>
<element value="T_DOC_COMMENT_OPEN_TAG"/>
<element value="T_CLOSE_TAG"/>
</property>
</properties>
</rule>
<!-- Ensure there is no whitespace before a semicolon -->
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
<!-- All rules in ./Sniffs are included automatically -->
<!-- FHComplete sniffs:
- Classes/FHCPropertyDeclarationSniff: class properties checks, it does not check if the property name starts with an underscore "_"
- Classes/FHCValidClassNameSniff: class names checks, it does not check if the class name contains an underscore "_"
- ControlStructures/FHCControlSignatureSniff:
- Does not force to have whitespaces after the brackets
- Allows to have a bracket at newline after a control structure
- Methods/FHCMethodDeclarationSniff: it does not check if the method name starts with an underscore "_"
-->
</ruleset>
+5 -2
View File
@@ -17,8 +17,11 @@
<exclude-pattern>application/extensions</exclude-pattern>
<exclude-pattern>addons</exclude-pattern>
<!-- Import the entire clean code rule set -->
<rule ref="rulesets/cleancode.xml" />
<!-- Import the clean code rule set -->
<rule ref="rulesets/cleancode.xml">
<!-- Exclude the ElseExpression rule -->
<exclude name="ElseExpression" />
</rule>
<!-- Import the entire unused code rule set -->
<rule ref="rulesets/unusedcode.xml" />
<!-- Import the DevelopmentCodeFragment rule from the design rule set -->