Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelStandard.xml" role="php" />
</dir>
<dir name="NamingConventions">
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameStandard.xml" role="php" />
</dir>
<dir name="PHP">
Expand Down Expand Up @@ -358,8 +361,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelSniff.php" role="php" />
</dir>
<dir name="NamingConventions">
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameSniff.php" role="php" />
</dir>
<dir name="PHP">
Expand Down Expand Up @@ -603,10 +609,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelUnitTest.php" role="test" />
</dir>
<dir name="NamingConventions">
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameUnitTest.php" role="test" />
</dir>
Expand Down
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
{
}
]]>
</code>
</code_comparison>
</documentation>
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>
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>
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
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
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
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
{

}
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
Loading