-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Added AbstractPrefixRequiredForAbstractClass, InterfaceSuffixRequired… #3055
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
gsherwood
merged 5 commits into
squizlabs:master
from
annechko:feature/psr-naming-conventions
Jan 13, 2021
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2fae0e8
Added AbstractPrefixRequiredForAbstractClass, InterfaceSuffixRequired…
annechko 798e7aa
Check class, trait, interface naming in lowercase, change error codes…
annechko b363360
Deleted extra whitespace
annechko 4b5aa4f
Added ability to set affix type, value, case sensitive while checking…
annechko 88c8dac
Revert "Added ability to set affix type, value, case sensitive while …
annechko 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
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
23 changes: 23 additions & 0 deletions
23
...andards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml
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,23 @@ | ||
| <documentation title="Abstract class name"> | ||
| <standard> | ||
| <![CDATA[ | ||
| Abstract classes MUST be prefixed by Abstract: e.g. Psr\Foo\AbstractBar. | ||
| ]]> | ||
| </standard> | ||
| <code_comparison> | ||
| <code title="Valid: "> | ||
| <![CDATA[ | ||
| abstract class AbstractFoo | ||
| { | ||
| } | ||
| ]]> | ||
| </code> | ||
| <code title="Invalid: "> | ||
| <![CDATA[ | ||
| abstract class Foo | ||
annechko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| } | ||
| ]]> | ||
| </code> | ||
| </code_comparison> | ||
| </documentation> | ||
23 changes: 23 additions & 0 deletions
23
src/Standards/Generic/Docs/NamingConventions/InterfaceSuffixRequiredForInterfaceStandard.xml
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,23 @@ | ||
| <documentation title="Interface name"> | ||
| <standard> | ||
| <![CDATA[ | ||
| Interfaces MUST be suffixed by Interface: e.g. Psr\Foo\BarInterface. | ||
| ]]> | ||
| </standard> | ||
| <code_comparison> | ||
| <code title="Valid: "> | ||
| <![CDATA[ | ||
| interface FooInterface | ||
| { | ||
| } | ||
| ]]> | ||
| </code> | ||
| <code title="Invalid: "> | ||
| <![CDATA[ | ||
| interface IFoo | ||
| { | ||
| } | ||
| ]]> | ||
| </code> | ||
| </code_comparison> | ||
| </documentation> |
23 changes: 23 additions & 0 deletions
23
src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml
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,23 @@ | ||
| <documentation title="Trait name"> | ||
| <standard> | ||
| <![CDATA[ | ||
| Traits MUST be suffixed by Trait: e.g. Psr\Foo\BarTrait. | ||
| ]]> | ||
| </standard> | ||
| <code_comparison> | ||
| <code title="Valid: "> | ||
| <![CDATA[ | ||
| trait BarTrait | ||
| { | ||
| } | ||
| ]]> | ||
| </code> | ||
| <code title="Invalid: "> | ||
| <![CDATA[ | ||
| trait Bar | ||
| { | ||
| } | ||
| ]]> | ||
| </code> | ||
| </code_comparison> | ||
| </documentation> |
60 changes: 60 additions & 0 deletions
60
...tandards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.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,60 @@ | ||
| <?php | ||
| /** | ||
| * Checks that abstract classes are prefixed by Abstract. | ||
| * | ||
| * @author Anna Borzenko <[email protected]> | ||
| * @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) | ||
| { | ||
| if ($phpcsFile->getClassProperties($stackPtr)['is_abstract'] === false) { | ||
| // 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 (strtolower($prefix) !== 'abstract') { | ||
| $phpcsFile->addError('Abstract classes MUST be prefixed by Abstract: e.g. AbstractBar. Found: %s', $stackPtr, 'Missing', [$className]); | ||
| } | ||
|
|
||
| }//end process() | ||
|
|
||
|
|
||
| }//end class |
54 changes: 54 additions & 0 deletions
54
src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.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,54 @@ | ||
| <?php | ||
| /** | ||
| * Checks that interfaces are suffixed by Interface. | ||
| * | ||
| * @author Anna Borzenko <[email protected]> | ||
| * @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 |
54 changes: 54 additions & 0 deletions
54
src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.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,54 @@ | ||
| <?php | ||
| /** | ||
| * Checks that traits are suffixed by Trait. | ||
| * | ||
| * @author Anna Borzenko <[email protected]> | ||
| * @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; | ||
| } | ||
|
|
||
| $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() | ||
|
|
||
|
|
||
| }//end class |
59 changes: 59 additions & 0 deletions
59
...ndards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc
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,59 @@ | ||
| <?php | ||
|
|
||
| abstract class IncorrectName // error | ||
| { | ||
|
|
||
| } | ||
|
|
||
| abstract class AbstractCorrectName | ||
| { | ||
|
|
||
| } | ||
|
|
||
| abstract class IncorrectNameAbstract // error | ||
| { | ||
|
|
||
| } | ||
|
|
||
| abstract class InvalidNameabstract // error | ||
| { | ||
|
|
||
| } | ||
|
|
||
| abstract class IncorrectAbstractName // error | ||
| { | ||
|
|
||
| } | ||
|
|
||
| $anon = new class {}; | ||
|
|
||
| class AbstractClassName | ||
| { | ||
|
|
||
| } | ||
|
|
||
| if (!class_exists('AbstractClassCorrectName')) { | ||
| abstract class AbstractClassCorrectName | ||
| { | ||
|
|
||
| } | ||
| } | ||
| if (!class_exists('ClassAbstractIncorrectName')) { | ||
| abstract class ClassAbstractIncorrectName // error | ||
| { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| $abstractVar = ''; | ||
|
|
||
| $var = 'abstract class IncorrectNameButOk'; | ||
|
|
||
| $abstracVar = ''; | ||
|
|
||
| class NameAbstractBar {} | ||
|
|
||
| abstract class abstractOkName | ||
| { | ||
|
|
||
| } |
53 changes: 53 additions & 0 deletions
53
...ndards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.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,53 @@ | ||
| <?php | ||
| /** | ||
| * Unit test class for the AbstractPrefixRequiredForAbstractClass sniff. | ||
| * | ||
| * @author Anna Borzenko <[email protected]> | ||
| * @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<int, int> | ||
| */ | ||
| 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<int, int> | ||
| */ | ||
| public function getWarningList() | ||
| { | ||
| return []; | ||
|
|
||
| }//end getWarningList() | ||
|
|
||
|
|
||
| }//end class |
Oops, something went wrong.
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.