Skip to content

Commit 4b5aa4f

Browse files
committed
Added ability to set affix type, value, case sensitive while checking Interface name, changed docs
1 parent b363360 commit 4b5aa4f

9 files changed

+235
-97
lines changed

package.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
256256
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassStandard.xml" role="php" />
257257
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameStandard.xml" role="php" />
258258
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameStandard.xml" role="php" />
259-
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceStandard.xml" role="php" />
259+
<file baseinstalldir="PHP/CodeSniffer" name="AffixRequiredForInterfaceStandard.xml" role="php" />
260260
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitStandard.xml" role="php" />
261261
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameStandard.xml" role="php" />
262262
</dir>
@@ -364,7 +364,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
364364
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassSniff.php" role="php" />
365365
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameSniff.php" role="php" />
366366
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameSniff.php" role="php" />
367-
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceSniff.php" role="php" />
367+
<file baseinstalldir="PHP/CodeSniffer" name="AffixRequiredForInterfaceSniff.php" role="php" />
368368
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitSniff.php" role="php" />
369369
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameSniff.php" role="php" />
370370
</dir>
@@ -615,8 +615,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
615615
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameUnitTest.php" role="test" />
616616
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameUnitTest.inc" role="test" />
617617
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameUnitTest.php" role="test" />
618-
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceUnitTest.inc" role="test" />
619-
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceUnitTest.php" role="test" />
618+
<file baseinstalldir="PHP/CodeSniffer" name="AffixRequiredForInterfaceUnitTest.inc" role="test" />
619+
<file baseinstalldir="PHP/CodeSniffer" name="AffixRequiredForInterfaceUnitTest.php" role="test" />
620620
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitUnitTest.inc" role="test" />
621621
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitUnitTest.php" role="test" />
622622
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameUnitTest.inc" role="test" />

src/Standards/Generic/Docs/NamingConventions/AbstractPrefixRequiredForAbstractClassStandard.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<documentation title="Abstract class name">
22
<standard>
33
<![CDATA[
4-
Abstract classes MUST be prefixed by Abstract: e.g. Psr\Foo\AbstractBar.
4+
Abstract classes MUST be prefixed by Abstract: e.g. AbstractBar.
55
]]>
66
</standard>
77
<code_comparison>
88
<code title="Valid: ">
99
<![CDATA[
10-
abstract class AbstractFoo
10+
abstract class <em>AbstractBar</em>
1111
{
1212
}
1313
]]>
1414
</code>
1515
<code title="Invalid: ">
1616
<![CDATA[
17-
abstract class Foo
17+
abstract class <em>Bar</em>
1818
{
1919
}
2020
]]>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<documentation title="Interface name">
22
<standard>
33
<![CDATA[
4-
Interfaces MUST be suffixed by Interface: e.g. Psr\Foo\BarInterface.
4+
Interfaces MUST be suffixed or prefixed by given affix: checks Interface suffix by default, e.g. BarInterface.
55
]]>
66
</standard>
77
<code_comparison>
88
<code title="Valid: ">
99
<![CDATA[
10-
interface FooInterface
10+
interface <em>BarInterface</em>
1111
{
1212
}
1313
]]>
1414
</code>
1515
<code title="Invalid: ">
1616
<![CDATA[
17-
interface IFoo
17+
interface <em>Bar</em>
1818
{
1919
}
2020
]]>

src/Standards/Generic/Docs/NamingConventions/TraitSuffixRequiredForTraitStandard.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<documentation title="Trait name">
22
<standard>
33
<![CDATA[
4-
Traits MUST be suffixed by Trait: e.g. Psr\Foo\BarTrait.
4+
Traits MUST be suffixed by Trait: e.g. BarTrait.
55
]]>
66
</standard>
77
<code_comparison>
88
<code title="Valid: ">
99
<![CDATA[
10-
trait BarTrait
10+
trait <em>BarTrait</em>
1111
{
1212
}
1313
]]>
1414
</code>
1515
<code title="Invalid: ">
1616
<![CDATA[
17-
trait Bar
17+
trait <em>Bar</em>
1818
{
1919
}
2020
]]>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* Checks that interfaces are suffixed or prefixed by given affix.
4+
* By default it checks that interface has 'Interface' suffix - e.g. BarInterface.
5+
*
6+
* @author Anna Borzenko <[email protected]>
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
15+
class AffixRequiredForInterfaceSniff implements Sniff
16+
{
17+
18+
/**
19+
* The affix type - either 'suffix' or 'prefix'.
20+
* Default to 'suffix'.
21+
*
22+
* @var string
23+
*/
24+
public $affixType = 'suffix';
25+
26+
/**
27+
* A prefix/suffix that must be added to interface name.
28+
* Default to 'Interface'.
29+
*
30+
* @var string
31+
*/
32+
public $affixValue = 'Interface';
33+
34+
/**
35+
* Whether to run case sensitive prefix/suffix comparison.
36+
* Default to false.
37+
*
38+
* @var boolean
39+
*/
40+
public $isCaseSensitive = false;
41+
42+
43+
/**
44+
* Registers the tokens that this sniff wants to listen for.
45+
*
46+
* @return int[]
47+
*/
48+
public function register()
49+
{
50+
return [T_INTERFACE];
51+
52+
}//end register()
53+
54+
55+
/**
56+
* Processes this sniff, when one of its tokens is encountered.
57+
*
58+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
59+
* @param int $stackPtr The position of the current token
60+
* in the stack passed in $tokens.
61+
*
62+
* @return void
63+
*/
64+
public function process(File $phpcsFile, $stackPtr)
65+
{
66+
$interfaceName = $phpcsFile->getDeclarationName($stackPtr);
67+
if ($interfaceName === null) {
68+
return;
69+
}
70+
71+
$this->affixValue = trim((string) $this->affixValue);
72+
$affixLength = strlen($this->affixValue);
73+
if ($affixLength === 0) {
74+
// If affix is empty - then we think all names are valid.
75+
return;
76+
}
77+
78+
$isSuffixRequired = $this->affixType === 'suffix';
79+
if ($isSuffixRequired === true) {
80+
$affix = substr($interfaceName, -$affixLength);
81+
} else {
82+
$affix = substr($interfaceName, 0, $affixLength);
83+
}
84+
85+
if (strlen($interfaceName) < $affixLength || $this->checkAffix($affix) === false) {
86+
$verb = 'prefixed';
87+
if ($isSuffixRequired === true) {
88+
$verb = 'suffixed';
89+
}
90+
91+
$affixErrorValue = $this->affixValue;
92+
if ($this->isCaseSensitive === true) {
93+
$affixErrorValue .= ' (case sensitive)';
94+
}
95+
96+
$nameExample = $this->affixValue.'Bar';
97+
if ($isSuffixRequired === true) {
98+
$nameExample = 'Bar'.$this->affixValue;
99+
}
100+
101+
$errorData = [
102+
$verb,
103+
$affixErrorValue,
104+
$nameExample,
105+
$interfaceName,
106+
];
107+
$phpcsFile->addError('Interfaces MUST be %s by %s: e.g. %s. Found: %s', $stackPtr, 'Missing', $errorData);
108+
}//end if
109+
110+
}//end process()
111+
112+
113+
/**
114+
* Checks if affix from the interface name is right.
115+
*
116+
* @param string $affix Affix from the checking interface name.
117+
*
118+
* @return bool
119+
*/
120+
private function checkAffix($affix)
121+
{
122+
if ($this->isCaseSensitive === false) {
123+
return strtolower($affix) === strtolower($this->affixValue);
124+
}
125+
126+
return $affix === $this->affixValue;
127+
128+
}//end checkAffix()
129+
130+
131+
}//end class

src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
interface SomeNameInterface
4+
{
5+
6+
}
7+
8+
interface ISomeName // error
9+
{
10+
11+
}
12+
13+
interface ISomeNameInterface
14+
{
15+
16+
}
17+
18+
19+
interface ISomeOtherNameinterface
20+
{
21+
22+
}
23+
24+
interface mInterface
25+
{
26+
27+
}
28+
29+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface affixType prefix
30+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface affixValue I
31+
32+
interface SomeOtherNameInterface // error
33+
{
34+
35+
}
36+
37+
interface IInterface
38+
{
39+
40+
}
41+
42+
interface I
43+
{
44+
45+
}
46+
47+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface affixType prefix
48+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface affixValue PREF
49+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface isCaseSensitive true
50+
51+
interface Badpref // error
52+
{
53+
54+
}
55+
56+
interface PREFName
57+
{
58+
59+
}
60+
61+
62+
interface PrefInvalid // error
63+
{
64+
65+
}
66+
67+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface affixType prefix
68+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface affixValue PrefixLongerThanInterfaceName
69+
70+
interface ShortName // error
71+
{
72+
73+
}
74+
75+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface affixType prefix
76+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface affixValue PrefixSameAsInterfaceName
77+
// phpcs:set Generic.NamingConventions.AffixRequiredForInterface isCaseSensitive true
78+
79+
interface PrefixSameAsInterfaceName
80+
{
81+
82+
}

0 commit comments

Comments
 (0)