Skip to content

Commit 6bade42

Browse files
authored
Add Sniff to ensure usage of Stringable interface (#172)
2 parents 02a44a6 + 00b93bf commit 6bade42

File tree

5 files changed

+253
-0
lines changed

5 files changed

+253
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<documentation title="Stringable">
2+
<standard>
3+
<![CDATA[
4+
Classes that implement the "__toString" magic method should implement the Stringable interface.
5+
]]>
6+
</standard>
7+
<code_comparison>
8+
<code title="Valid: Class implementing __toString implements the Stringable interface.">
9+
<![CDATA[
10+
class Foo<em> implements Stringable</em> {
11+
public function __toString(): string {
12+
return __CLASS__;
13+
}
14+
}
15+
]]>
16+
</code>
17+
<code title="Invalid: Class implementing __toString doesn't implement the Stringable interface.">
18+
<![CDATA[
19+
class Foo<em></em> {
20+
public function __toString(): string {
21+
return __CLASS__;
22+
}
23+
}
24+
]]>
25+
</code>
26+
</code_comparison>
27+
</documentation>
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* BigBite Coding Standards.
4+
*
5+
* @package BigBiteCS\BigBite
6+
* @link https://github.com/bigbite/phpcs-config
7+
* @license https://opensource.org/licenses/MIT MIT
8+
*/
9+
10+
namespace BigBiteCS\BigBite\Sniffs\Classes;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHPCSUtils\Utils\ObjectDeclarations;
15+
16+
/**
17+
* Ensures classes that define a __toString method also implement the Stringable interface
18+
*/
19+
final class StringableSniff implements Sniff {
20+
21+
/**
22+
* A list of tokenizers this sniff supports.
23+
*
24+
* @var array<int,string>
25+
*/
26+
public $supportedTokenizers = array(
27+
'PHP',
28+
);
29+
30+
/**
31+
* Returns an array of tokens this test wants to listen for.
32+
*
33+
* @return array<int,int>
34+
*/
35+
public function register() {
36+
if ( version_compare( phpversion(), '8.0.0', '<' ) ) {
37+
return array();
38+
}
39+
40+
return array( \T_FUNCTION );
41+
}
42+
43+
/**
44+
* Processes this test, when one of its tokens is encountered.
45+
*
46+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
47+
* @param int $stackPtr The position of the current token
48+
* in the stack passed in $tokens.
49+
*
50+
* @return void
51+
*/
52+
public function process( File $phpcsFile, $stackPtr ) {
53+
$tokens = $phpcsFile->getTokens();
54+
$fnName = $phpcsFile->findNext( T_STRING, $stackPtr, ( $stackPtr + 5 ), false, null, true );
55+
56+
if ( false === $fnName || '__toString' !== $tokens[ $fnName ]['content'] ) {
57+
return;
58+
}
59+
60+
$classDecl = $phpcsFile->findPrevious( T_CLASS, $stackPtr, 0 );
61+
62+
// this __toString function isn't a class member?
63+
if ( false === $classDecl ) {
64+
return;
65+
}
66+
67+
$interfaces = ObjectDeclarations::findImplementedInterfaceNames( $phpcsFile, $classDecl );
68+
69+
// we're good - class containing __toString implements the interface.
70+
if ( is_array( $interfaces ) && in_array( 'Stringable', $interfaces, true ) ) {
71+
return;
72+
}
73+
74+
$message = 'Classes that declare "__toString" should implement the Stringable interface.';
75+
$doWeFix = $phpcsFile->addFixableError( $message, $classDecl, 'NotImplemented', array(), 0 );
76+
77+
if ( true !== $doWeFix ) {
78+
return;
79+
}
80+
81+
$openingCurly = $tokens[ $classDecl ]['scope_opener'];
82+
83+
if ( $tokens[ $openingCurly ]['line'] === $tokens[ $classDecl ]['line'] ) {
84+
$prevToken = $phpcsFile->findPrevious( T_STRING, $openingCurly, $classDecl );
85+
86+
if ( false === $prevToken ) {
87+
return;
88+
}
89+
90+
$phpcsFile->fixer->beginChangeset();
91+
$phpcsFile->fixer->addContent( $prevToken, ' implements Stringable' );
92+
$phpcsFile->fixer->endChangeset();
93+
94+
return;
95+
}
96+
97+
$endOfLine = $phpcsFile->findPrevious( T_WHITESPACE, $openingCurly, $classDecl );
98+
99+
if ( false === $endOfLine ) {
100+
return;
101+
}
102+
103+
$phpcsFile->fixer->beginChangeset();
104+
$phpcsFile->fixer->addContentBefore( $endOfLine, ' implements Stringable' );
105+
$phpcsFile->fixer->endChangeset();
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
class DoesNotImplementToStringMagicMethod {
4+
}
5+
6+
class CorrectlyImplementsStringable implements Stringable {
7+
public function __toString(): string {
8+
return __CLASS__;
9+
}
10+
}
11+
12+
class IncorrectWithWhitespace {
13+
public function __toString(): string {
14+
return __CLASS__;
15+
}
16+
}
17+
18+
class IncorrectWithNoWhitespace{
19+
public function __toString(): string {
20+
return __CLASS__;
21+
}
22+
}
23+
24+
class IncorrectWithNewline
25+
{
26+
public function __toString(): string {
27+
return __CLASS__;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
class DoesNotImplementToStringMagicMethod {
4+
}
5+
6+
class CorrectlyImplementsStringable implements Stringable {
7+
public function __toString(): string {
8+
return __CLASS__;
9+
}
10+
}
11+
12+
class IncorrectWithWhitespace implements Stringable {
13+
public function __toString(): string {
14+
return __CLASS__;
15+
}
16+
}
17+
18+
class IncorrectWithNoWhitespace implements Stringable{
19+
public function __toString(): string {
20+
return __CLASS__;
21+
}
22+
}
23+
24+
class IncorrectWithNewline implements Stringable
25+
{
26+
public function __toString(): string {
27+
return __CLASS__;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Unit test class for BigBite Coding Standard.
4+
*
5+
* @package BigBiteCS\BigBite
6+
* @link https://github.com/bigbite/phpcs-config
7+
* @license https://opensource.org/licenses/MIT MIT
8+
*/
9+
10+
namespace BigBiteCS\BigBite\Tests\Classes;
11+
12+
use BigBiteCS\BigBite\Tests\AbstractSniffUnitTest;
13+
14+
/**
15+
* Unit test class for the Stringable sniff.
16+
*
17+
* @package BigBiteCS\BigBite
18+
*/
19+
final class StringableUnitTest extends AbstractSniffUnitTest {
20+
21+
/**
22+
* Returns the lines where errors should occur.
23+
*
24+
* The key of the array should represent the line number and the value
25+
* should represent the number of errors that should occur on that line.
26+
*
27+
* @param string $testFile The name of the file being tested.
28+
*
29+
* @return array<int,int>
30+
*/
31+
public function getErrorList( $testFile = '' ) {
32+
if ( version_compare( phpversion(), '8.0.0', '<' ) ) {
33+
return array();
34+
}
35+
36+
switch ( $testFile ) {
37+
case 'StringableUnitTest.1.inc':
38+
return array(
39+
12 => 1,
40+
18 => 1,
41+
24 => 1,
42+
);
43+
default:
44+
return array();
45+
}
46+
}
47+
48+
/**
49+
* Returns the lines where warnings should occur.
50+
*
51+
* The key of the array should represent the line number and the value
52+
* should represent the number of warnings that should occur on that line.
53+
*
54+
* @param string $testFile The name of the file being tested.
55+
*
56+
* @return array<int,int>
57+
*/
58+
public function getWarningList( $testFile = '' ) {
59+
return array();
60+
}
61+
}

0 commit comments

Comments
 (0)