From 2fae0e8f54b2efeb3b519fcb5d6cef391cd6410e Mon Sep 17 00:00:00 2001 From: Anna Borzenko Date: Mon, 24 Aug 2020 12:05:50 +0200 Subject: [PATCH 1/5] Added AbstractPrefixRequiredForAbstractClass, InterfaceSuffixRequiredForInterface, TraitSuffixRequiredForTrait sniffs to Generic.NamingConventions --- package.xml | 12 ++++ ...PrefixRequiredForAbstractClassStandard.xml | 23 +++++++ ...faceSuffixRequiredForInterfaceStandard.xml | 23 +++++++ .../TraitSuffixRequiredForTraitStandard.xml | 23 +++++++ ...actPrefixRequiredForAbstractClassSniff.php | 61 +++++++++++++++++++ ...terfaceSuffixRequiredForInterfaceSniff.php | 55 +++++++++++++++++ .../TraitSuffixRequiredForTraitSniff.php | 55 +++++++++++++++++ ...PrefixRequiredForAbstractClassUnitTest.inc | 54 ++++++++++++++++ ...PrefixRequiredForAbstractClassUnitTest.php | 53 ++++++++++++++++ ...faceSuffixRequiredForInterfaceUnitTest.inc | 27 ++++++++ ...faceSuffixRequiredForInterfaceUnitTest.php | 50 +++++++++++++++ .../TraitSuffixRequiredForTraitUnitTest.inc | 13 ++++ .../TraitSuffixRequiredForTraitUnitTest.php | 51 ++++++++++++++++ 13 files changed, 500 insertions(+) create mode 100644 src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml create mode 100644 src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml create mode 100644 src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml create mode 100644 src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php create mode 100644 src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php create mode 100644 src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php create mode 100644 src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.php create mode 100644 src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.php create mode 100644 src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.php diff --git a/package.xml b/package.xml index 4932f5df71..be984e9a02 100644 --- a/package.xml +++ b/package.xml @@ -253,8 +253,11 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + + @@ -358,8 +361,11 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + + @@ -603,10 +609,16 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + + + + + diff --git a/src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml b/src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml new file mode 100644 index 0000000000..0d8b7c6c63 --- /dev/null +++ b/src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + diff --git a/src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml b/src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml new file mode 100644 index 0000000000..1a11acef77 --- /dev/null +++ b/src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + diff --git a/src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml b/src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml new file mode 100644 index 0000000000..f754775d7d --- /dev/null +++ b/src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + diff --git a/src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php new file mode 100644 index 0000000000..aca7bfde63 --- /dev/null +++ b/src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php @@ -0,0 +1,61 @@ + + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +class AbstractPrefixRequiredForAbstractClassSniff implements Sniff +{ + + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + */ + public function register() + { + return [T_CLASS]; + + }//end register() + + + /** + * Processes this sniff, when one of its tokens is encountered. + * + * @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) + { + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); + if ($prev === false || $phpcsFile->getTokens()[$prev]['code'] !== T_ABSTRACT) { + // This class is not abstract so we don't need to check it. + return; + } + + $className = $phpcsFile->getDeclarationName($stackPtr); + if ($className === null) { + // We are not interested in anonymous classes. + return; + } + + $prefix = substr($className, 0, 8); + if ($prefix !== 'Abstract') { + $phpcsFile->addError('Abstract classes MUST be prefixed by Abstract: e.g. Psr\Foo\AbstractBar.', $stackPtr, 'RequiredAbstractPrefix'); + } + + }//end process() + + +}//end class diff --git a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php new file mode 100644 index 0000000000..214f8475de --- /dev/null +++ b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php @@ -0,0 +1,55 @@ + + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +class InterfaceSuffixRequiredForInterfaceSniff implements Sniff +{ + + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + */ + public function register() + { + return [T_INTERFACE]; + + }//end register() + + + /** + * Processes this sniff, when one of its tokens is encountered. + * + * @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) + { + $interfaceName = $phpcsFile->getDeclarationName($stackPtr); + if ($interfaceName === null) { + return; + } + + $interfaceNameLength = strlen($interfaceName); + $suffix = substr($interfaceName, ($interfaceNameLength - 9), $interfaceNameLength); + if ($suffix !== 'Interface') { + $phpcsFile->addError('Interfaces MUST be suffixed by Interface: e.g. Psr\Foo\BarInterface.', $stackPtr, 'RequiredInterfaceSuffix'); + } + + }//end process() + + +}//end class diff --git a/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php new file mode 100644 index 0000000000..ecf26ea14c --- /dev/null +++ b/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php @@ -0,0 +1,55 @@ + + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +class TraitSuffixRequiredForTraitSniff implements Sniff +{ + + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + */ + public function register() + { + return [T_TRAIT]; + + }//end register() + + + /** + * Processes this sniff, when one of its tokens is encountered. + * + * @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) + { + $traitName = $phpcsFile->getDeclarationName($stackPtr); + if ($traitName === null) { + return; + } + + $traitNameLength = strlen($traitName); + $suffix = substr($traitName, ($traitNameLength - 5), $traitNameLength); + if ($suffix !== 'Trait') { + $phpcsFile->addError('Traits MUST be suffixed by Trait: e.g. Psr\Foo\BarTrait.', $stackPtr, 'RequiredTraitSuffix'); + } + + }//end process() + + +}//end class diff --git a/src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc new file mode 100644 index 0000000000..f47131918e --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc @@ -0,0 +1,54 @@ + + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Tests\NamingConventions; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + +class AbstractPrefixRequiredForAbstractClassUnitTest extends AbstractSniffUnitTest +{ + + + /** + * Returns the lines where errors should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of errors that should occur on that line. + * + * @return array + */ + public function getErrorList() + { + return [ + 3 => 1, + 13 => 1, + 18 => 1, + 23 => 1, + 42 => 1, + ]; + + }//end getErrorList() + + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @return array + */ + public function getWarningList() + { + return []; + + }//end getWarningList() + + +}//end class diff --git a/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc new file mode 100644 index 0000000000..aea665ddda --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc @@ -0,0 +1,27 @@ + + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Tests\NamingConventions; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + +class InterfaceSuffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest +{ + + + /** + * Returns the lines where errors should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of errors that should occur on that line. + * + * @return array + */ + public function getErrorList() + { + return [ + 8 => 1, + 19 => 1, + ]; + + }//end getErrorList() + + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @return array + */ + public function getWarningList() + { + return []; + + }//end getWarningList() + + +}//end class diff --git a/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc new file mode 100644 index 0000000000..9cea6f2a3f --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc @@ -0,0 +1,13 @@ + + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Tests\NamingConventions; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + +class TraitSuffixRequiredForTraitUnitTest extends AbstractSniffUnitTest +{ + + + /** + * Returns the lines where errors should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of errors that should occur on that line. + * + * @return array + */ + public function getErrorList() + { + return [ + 3 => 1, + 9 => 1, + 11 => 1, + ]; + + }//end getErrorList() + + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @return array + */ + public function getWarningList() + { + return []; + + }//end getWarningList() + + +}//end class From 798e7aa0462eeb69013e74b6645d127172b3afc7 Mon Sep 17 00:00:00 2001 From: Anna Borzenko Date: Tue, 25 Aug 2020 18:23:29 +0200 Subject: [PATCH 2/5] Check class, trait, interface naming in lowercase, change error codes to Missing, add Found info --- .../AbstractPrefixRequiredForAbstractClassSniff.php | 7 +++---- .../InterfaceSuffixRequiredForInterfaceSniff.php | 7 +++---- .../NamingConventions/TraitSuffixRequiredForTraitSniff.php | 7 +++---- .../AbstractPrefixRequiredForAbstractClassUnitTest.inc | 7 ++++++- .../InterfaceSuffixRequiredForInterfaceUnitTest.inc | 2 +- .../InterfaceSuffixRequiredForInterfaceUnitTest.php | 5 +---- .../TraitSuffixRequiredForTraitUnitTest.inc | 2 +- .../TraitSuffixRequiredForTraitUnitTest.php | 5 ++--- 8 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php index aca7bfde63..7892e111fc 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php @@ -38,8 +38,7 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); - if ($prev === false || $phpcsFile->getTokens()[$prev]['code'] !== T_ABSTRACT) { + if ($phpcsFile->getClassProperties($stackPtr)['is_abstract'] === false) { // This class is not abstract so we don't need to check it. return; } @@ -51,8 +50,8 @@ public function process(File $phpcsFile, $stackPtr) } $prefix = substr($className, 0, 8); - if ($prefix !== 'Abstract') { - $phpcsFile->addError('Abstract classes MUST be prefixed by Abstract: e.g. Psr\Foo\AbstractBar.', $stackPtr, 'RequiredAbstractPrefix'); + if (strtolower($prefix) !== 'abstract') { + $phpcsFile->addError('Abstract classes MUST be prefixed by Abstract: e.g. AbstractBar. Found: %s', $stackPtr, 'Missing', [$className]); } }//end process() diff --git a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php index 214f8475de..643e3e7289 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php @@ -43,10 +43,9 @@ public function process(File $phpcsFile, $stackPtr) return; } - $interfaceNameLength = strlen($interfaceName); - $suffix = substr($interfaceName, ($interfaceNameLength - 9), $interfaceNameLength); - if ($suffix !== 'Interface') { - $phpcsFile->addError('Interfaces MUST be suffixed by Interface: e.g. Psr\Foo\BarInterface.', $stackPtr, 'RequiredInterfaceSuffix'); + $suffix = substr($interfaceName, - 9); + if (strtolower($suffix) !== 'interface') { + $phpcsFile->addError('Interfaces MUST be suffixed by Interface: e.g. BarInterface. Found: %s', $stackPtr, 'Missing', [$interfaceName]); } }//end process() diff --git a/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php index ecf26ea14c..17179badb2 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php @@ -43,10 +43,9 @@ public function process(File $phpcsFile, $stackPtr) return; } - $traitNameLength = strlen($traitName); - $suffix = substr($traitName, ($traitNameLength - 5), $traitNameLength); - if ($suffix !== 'Trait') { - $phpcsFile->addError('Traits MUST be suffixed by Trait: e.g. Psr\Foo\BarTrait.', $stackPtr, 'RequiredTraitSuffix'); + $suffix = substr($traitName, - 5); + if (strtolower($suffix) !== 'trait') { + $phpcsFile->addError('Traits MUST be suffixed by Trait: e.g. BarTrait. Found: %s', $stackPtr, 'Missing', [$traitName]); } }//end process() diff --git a/src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc index f47131918e..27b9d818da 100644 --- a/src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc +++ b/src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc @@ -51,4 +51,9 @@ $var = 'abstract class IncorrectNameButOk'; $abstracVar = ''; -class NameAbstractBar {} \ No newline at end of file +class NameAbstractBar {} + +abstract class abstractOkName +{ + +} \ No newline at end of file diff --git a/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc index aea665ddda..d562d3eaeb 100644 --- a/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc +++ b/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc @@ -16,7 +16,7 @@ interface ISomeNameInterface } -interface ISomeOtherNameinterface // error +interface ISomeOtherNameinterface { } diff --git a/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.php index f614d50638..ccbf634893 100644 --- a/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.php @@ -24,10 +24,7 @@ class InterfaceSuffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest */ public function getErrorList() { - return [ - 8 => 1, - 19 => 1, - ]; + return [8 => 1]; }//end getErrorList() diff --git a/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc index 9cea6f2a3f..e807697185 100644 --- a/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc +++ b/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc @@ -8,6 +8,6 @@ trait GoodTraitTrait {} trait BadTraitName {} // error -trait BadTraitNametrait {} // error +trait BadTraitNametrait {} trait NormalTraitNameTrait {} \ No newline at end of file diff --git a/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.php index ddf74af43d..07f1b4c843 100644 --- a/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.php @@ -25,9 +25,8 @@ class TraitSuffixRequiredForTraitUnitTest extends AbstractSniffUnitTest public function getErrorList() { return [ - 3 => 1, - 9 => 1, - 11 => 1, + 3 => 1, + 9 => 1, ]; }//end getErrorList() From b3633601cb2826a3ef8f07decb615a54a9e91fa3 Mon Sep 17 00:00:00 2001 From: Anna Borzenko Date: Tue, 25 Aug 2020 18:32:38 +0200 Subject: [PATCH 3/5] Deleted extra whitespace --- .../InterfaceSuffixRequiredForInterfaceSniff.php | 2 +- .../NamingConventions/TraitSuffixRequiredForTraitSniff.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php index 643e3e7289..0f388a178b 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php @@ -43,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr) return; } - $suffix = substr($interfaceName, - 9); + $suffix = substr($interfaceName, -9); if (strtolower($suffix) !== 'interface') { $phpcsFile->addError('Interfaces MUST be suffixed by Interface: e.g. BarInterface. Found: %s', $stackPtr, 'Missing', [$interfaceName]); } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php index 17179badb2..d886669cb1 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php @@ -43,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr) return; } - $suffix = substr($traitName, - 5); + $suffix = substr($traitName, -5); if (strtolower($suffix) !== 'trait') { $phpcsFile->addError('Traits MUST be suffixed by Trait: e.g. BarTrait. Found: %s', $stackPtr, 'Missing', [$traitName]); } From 4b5aa4f1b8219b32bb908aeaf0306ba494e4434a Mon Sep 17 00:00:00 2001 From: Anna Borzenko Date: Tue, 25 Aug 2020 22:40:46 +0200 Subject: [PATCH 4/5] Added ability to set affix type, value, case sensitive while checking Interface name, changed docs --- package.xml | 8 +- ...PrefixRequiredForAbstractClassStandard.xml | 6 +- ... => AffixRequiredForInterfaceStandard.xml} | 6 +- .../TraitSuffixRequiredForTraitStandard.xml | 6 +- .../AffixRequiredForInterfaceSniff.php | 131 ++++++++++++++++++ ...terfaceSuffixRequiredForInterfaceSniff.php | 54 -------- .../AffixRequiredForInterfaceUnitTest.inc | 82 +++++++++++ ... => AffixRequiredForInterfaceUnitTest.php} | 12 +- ...faceSuffixRequiredForInterfaceUnitTest.inc | 27 ---- 9 files changed, 235 insertions(+), 97 deletions(-) rename src/Standards/Generic/Docs/NamingConventions/{InterfaceSuffixRequiredForInterfaceStandard.xml => AffixRequiredForInterfaceStandard.xml} (65%) create mode 100644 src/Standards/Generic/Sniffs/NamingConventions/AffixRequiredForInterfaceSniff.php delete mode 100644 src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php create mode 100644 src/Standards/Generic/Tests/NamingConventions/AffixRequiredForInterfaceUnitTest.inc rename src/Standards/Generic/Tests/NamingConventions/{InterfaceSuffixRequiredForInterfaceUnitTest.php => AffixRequiredForInterfaceUnitTest.php} (79%) delete mode 100644 src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc diff --git a/package.xml b/package.xml index be984e9a02..605b3c9657 100644 --- a/package.xml +++ b/package.xml @@ -256,7 +256,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - + @@ -364,7 +364,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - + @@ -615,8 +615,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> - - + + diff --git a/src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml b/src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml index 0d8b7c6c63..4f84fa4465 100644 --- a/src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml +++ b/src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml @@ -1,20 +1,20 @@ AbstractBar { } ]]> Bar { } ]]> diff --git a/src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml b/src/Standards/Generic/Docs/NamingConventions/AffixRequiredForInterfaceStandard.xml similarity index 65% rename from src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml rename to src/Standards/Generic/Docs/NamingConventions/AffixRequiredForInterfaceStandard.xml index 1a11acef77..9c67b1d3f5 100644 --- a/src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml +++ b/src/Standards/Generic/Docs/NamingConventions/AffixRequiredForInterfaceStandard.xml @@ -1,20 +1,20 @@ BarInterface { } ]]> Bar { } ]]> diff --git a/src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml b/src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml index f754775d7d..62c9b26d46 100644 --- a/src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml +++ b/src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml @@ -1,20 +1,20 @@ BarTrait { } ]]> Bar { } ]]> diff --git a/src/Standards/Generic/Sniffs/NamingConventions/AffixRequiredForInterfaceSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/AffixRequiredForInterfaceSniff.php new file mode 100644 index 0000000000..ab1cac7c6d --- /dev/null +++ b/src/Standards/Generic/Sniffs/NamingConventions/AffixRequiredForInterfaceSniff.php @@ -0,0 +1,131 @@ + + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +class AffixRequiredForInterfaceSniff implements Sniff +{ + + /** + * The affix type - either 'suffix' or 'prefix'. + * Default to 'suffix'. + * + * @var string + */ + public $affixType = 'suffix'; + + /** + * A prefix/suffix that must be added to interface name. + * Default to 'Interface'. + * + * @var string + */ + public $affixValue = 'Interface'; + + /** + * Whether to run case sensitive prefix/suffix comparison. + * Default to false. + * + * @var boolean + */ + public $isCaseSensitive = false; + + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + */ + public function register() + { + return [T_INTERFACE]; + + }//end register() + + + /** + * Processes this sniff, when one of its tokens is encountered. + * + * @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) + { + $interfaceName = $phpcsFile->getDeclarationName($stackPtr); + if ($interfaceName === null) { + return; + } + + $this->affixValue = trim((string) $this->affixValue); + $affixLength = strlen($this->affixValue); + if ($affixLength === 0) { + // If affix is empty - then we think all names are valid. + return; + } + + $isSuffixRequired = $this->affixType === 'suffix'; + if ($isSuffixRequired === true) { + $affix = substr($interfaceName, -$affixLength); + } else { + $affix = substr($interfaceName, 0, $affixLength); + } + + if (strlen($interfaceName) < $affixLength || $this->checkAffix($affix) === false) { + $verb = 'prefixed'; + if ($isSuffixRequired === true) { + $verb = 'suffixed'; + } + + $affixErrorValue = $this->affixValue; + if ($this->isCaseSensitive === true) { + $affixErrorValue .= ' (case sensitive)'; + } + + $nameExample = $this->affixValue.'Bar'; + if ($isSuffixRequired === true) { + $nameExample = 'Bar'.$this->affixValue; + } + + $errorData = [ + $verb, + $affixErrorValue, + $nameExample, + $interfaceName, + ]; + $phpcsFile->addError('Interfaces MUST be %s by %s: e.g. %s. Found: %s', $stackPtr, 'Missing', $errorData); + }//end if + + }//end process() + + + /** + * Checks if affix from the interface name is right. + * + * @param string $affix Affix from the checking interface name. + * + * @return bool + */ + private function checkAffix($affix) + { + if ($this->isCaseSensitive === false) { + return strtolower($affix) === strtolower($this->affixValue); + } + + return $affix === $this->affixValue; + + }//end checkAffix() + + +}//end class diff --git a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php deleted file mode 100644 index 0f388a178b..0000000000 --- a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence - */ - -namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions; - -use PHP_CodeSniffer\Files\File; -use PHP_CodeSniffer\Sniffs\Sniff; - -class InterfaceSuffixRequiredForInterfaceSniff implements Sniff -{ - - - /** - * Registers the tokens that this sniff wants to listen for. - * - * @return int[] - */ - public function register() - { - return [T_INTERFACE]; - - }//end register() - - - /** - * Processes this sniff, when one of its tokens is encountered. - * - * @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) - { - $interfaceName = $phpcsFile->getDeclarationName($stackPtr); - if ($interfaceName === null) { - return; - } - - $suffix = substr($interfaceName, -9); - if (strtolower($suffix) !== 'interface') { - $phpcsFile->addError('Interfaces MUST be suffixed by Interface: e.g. BarInterface. Found: %s', $stackPtr, 'Missing', [$interfaceName]); - } - - }//end process() - - -}//end class diff --git a/src/Standards/Generic/Tests/NamingConventions/AffixRequiredForInterfaceUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/AffixRequiredForInterfaceUnitTest.inc new file mode 100644 index 0000000000..56a562bb86 --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/AffixRequiredForInterfaceUnitTest.inc @@ -0,0 +1,82 @@ + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence @@ -10,7 +10,7 @@ use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; -class InterfaceSuffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest +class AffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest { @@ -24,7 +24,13 @@ class InterfaceSuffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest */ public function getErrorList() { - return [8 => 1]; + return [ + 8 => 1, + 32 => 1, + 51 => 1, + 62 => 1, + 70 => 1, + ]; }//end getErrorList() diff --git a/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc deleted file mode 100644 index d562d3eaeb..0000000000 --- a/src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc +++ /dev/null @@ -1,27 +0,0 @@ - Date: Fri, 28 Aug 2020 10:28:02 +0200 Subject: [PATCH 5/5] Revert "Added ability to set affix type, value, case sensitive while checking Interface name, changed docs" --- package.xml | 8 +- ...aceSuffixRequiredForInterfaceStandard.xml} | 2 +- .../AffixRequiredForInterfaceSniff.php | 131 ------------------ ...terfaceSuffixRequiredForInterfaceSniff.php | 54 ++++++++ .../AffixRequiredForInterfaceUnitTest.inc | 82 ----------- ...faceSuffixRequiredForInterfaceUnitTest.inc | 27 ++++ ...aceSuffixRequiredForInterfaceUnitTest.php} | 12 +- 7 files changed, 89 insertions(+), 227 deletions(-) rename src/Standards/Generic/Docs/NamingConventions/{AffixRequiredForInterfaceStandard.xml => InterfaceSuffixRequiredForInterfaceStandard.xml} (76%) delete mode 100644 src/Standards/Generic/Sniffs/NamingConventions/AffixRequiredForInterfaceSniff.php create mode 100644 src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php delete mode 100644 src/Standards/Generic/Tests/NamingConventions/AffixRequiredForInterfaceUnitTest.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc rename src/Standards/Generic/Tests/NamingConventions/{AffixRequiredForInterfaceUnitTest.php => InterfaceSuffixRequiredForInterfaceUnitTest.php} (79%) diff --git a/package.xml b/package.xml index 605b3c9657..be984e9a02 100644 --- a/package.xml +++ b/package.xml @@ -256,7 +256,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - + @@ -364,7 +364,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - + @@ -615,8 +615,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> - - + + diff --git a/src/Standards/Generic/Docs/NamingConventions/AffixRequiredForInterfaceStandard.xml b/src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml similarity index 76% rename from src/Standards/Generic/Docs/NamingConventions/AffixRequiredForInterfaceStandard.xml rename to src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml index 9c67b1d3f5..d4f38447e5 100644 --- a/src/Standards/Generic/Docs/NamingConventions/AffixRequiredForInterfaceStandard.xml +++ b/src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml @@ -1,7 +1,7 @@ diff --git a/src/Standards/Generic/Sniffs/NamingConventions/AffixRequiredForInterfaceSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/AffixRequiredForInterfaceSniff.php deleted file mode 100644 index ab1cac7c6d..0000000000 --- a/src/Standards/Generic/Sniffs/NamingConventions/AffixRequiredForInterfaceSniff.php +++ /dev/null @@ -1,131 +0,0 @@ - - * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence - */ - -namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions; - -use PHP_CodeSniffer\Files\File; -use PHP_CodeSniffer\Sniffs\Sniff; - -class AffixRequiredForInterfaceSniff implements Sniff -{ - - /** - * The affix type - either 'suffix' or 'prefix'. - * Default to 'suffix'. - * - * @var string - */ - public $affixType = 'suffix'; - - /** - * A prefix/suffix that must be added to interface name. - * Default to 'Interface'. - * - * @var string - */ - public $affixValue = 'Interface'; - - /** - * Whether to run case sensitive prefix/suffix comparison. - * Default to false. - * - * @var boolean - */ - public $isCaseSensitive = false; - - - /** - * Registers the tokens that this sniff wants to listen for. - * - * @return int[] - */ - public function register() - { - return [T_INTERFACE]; - - }//end register() - - - /** - * Processes this sniff, when one of its tokens is encountered. - * - * @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) - { - $interfaceName = $phpcsFile->getDeclarationName($stackPtr); - if ($interfaceName === null) { - return; - } - - $this->affixValue = trim((string) $this->affixValue); - $affixLength = strlen($this->affixValue); - if ($affixLength === 0) { - // If affix is empty - then we think all names are valid. - return; - } - - $isSuffixRequired = $this->affixType === 'suffix'; - if ($isSuffixRequired === true) { - $affix = substr($interfaceName, -$affixLength); - } else { - $affix = substr($interfaceName, 0, $affixLength); - } - - if (strlen($interfaceName) < $affixLength || $this->checkAffix($affix) === false) { - $verb = 'prefixed'; - if ($isSuffixRequired === true) { - $verb = 'suffixed'; - } - - $affixErrorValue = $this->affixValue; - if ($this->isCaseSensitive === true) { - $affixErrorValue .= ' (case sensitive)'; - } - - $nameExample = $this->affixValue.'Bar'; - if ($isSuffixRequired === true) { - $nameExample = 'Bar'.$this->affixValue; - } - - $errorData = [ - $verb, - $affixErrorValue, - $nameExample, - $interfaceName, - ]; - $phpcsFile->addError('Interfaces MUST be %s by %s: e.g. %s. Found: %s', $stackPtr, 'Missing', $errorData); - }//end if - - }//end process() - - - /** - * Checks if affix from the interface name is right. - * - * @param string $affix Affix from the checking interface name. - * - * @return bool - */ - private function checkAffix($affix) - { - if ($this->isCaseSensitive === false) { - return strtolower($affix) === strtolower($this->affixValue); - } - - return $affix === $this->affixValue; - - }//end checkAffix() - - -}//end class diff --git a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php new file mode 100644 index 0000000000..0f388a178b --- /dev/null +++ b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php @@ -0,0 +1,54 @@ + + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +class InterfaceSuffixRequiredForInterfaceSniff implements Sniff +{ + + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + */ + public function register() + { + return [T_INTERFACE]; + + }//end register() + + + /** + * Processes this sniff, when one of its tokens is encountered. + * + * @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) + { + $interfaceName = $phpcsFile->getDeclarationName($stackPtr); + if ($interfaceName === null) { + return; + } + + $suffix = substr($interfaceName, -9); + if (strtolower($suffix) !== 'interface') { + $phpcsFile->addError('Interfaces MUST be suffixed by Interface: e.g. BarInterface. Found: %s', $stackPtr, 'Missing', [$interfaceName]); + } + + }//end process() + + +}//end class diff --git a/src/Standards/Generic/Tests/NamingConventions/AffixRequiredForInterfaceUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/AffixRequiredForInterfaceUnitTest.inc deleted file mode 100644 index 56a562bb86..0000000000 --- a/src/Standards/Generic/Tests/NamingConventions/AffixRequiredForInterfaceUnitTest.inc +++ /dev/null @@ -1,82 +0,0 @@ - * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence @@ -10,7 +10,7 @@ use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; -class AffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest +class InterfaceSuffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest { @@ -24,13 +24,7 @@ class AffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest */ public function getErrorList() { - return [ - 8 => 1, - 32 => 1, - 51 => 1, - 62 => 1, - 70 => 1, - ]; + return [8 => 1]; }//end getErrorList()