|
| 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 | +} |
0 commit comments