|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the mo4-coding-standard (phpcs standard) |
| 5 | + * |
| 6 | + * @author Xaver Loppenstedt <xaver@loppenstedt.de> |
| 7 | + * |
| 8 | + * @license http://spdx.org/licenses/MIT MIT License |
| 9 | + * |
| 10 | + * @link https://github.com/mayflower/mo4-coding-standard |
| 11 | + */ |
| 12 | + |
| 13 | +declare(strict_types=1); |
| 14 | + |
| 15 | +namespace MO4\Sniffs\WhiteSpace; |
| 16 | + |
| 17 | +use PHP_CodeSniffer\Files\File; |
| 18 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 19 | + |
| 20 | +/** |
| 21 | + * Multi Line Array sniff. |
| 22 | + * |
| 23 | + * @author Xaver Loppenstedt <xaver@loppenstedt.de> |
| 24 | + * |
| 25 | + * @copyright 2013-2021 Xaver Loppenstedt, some rights reserved. |
| 26 | + * |
| 27 | + * @license http://spdx.org/licenses/MIT MIT License |
| 28 | + * |
| 29 | + * @link https://github.com/mayflower/mo4-coding-standard |
| 30 | + */ |
| 31 | +class ConstantSpacingSniff implements Sniff |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Define all types of arrays. |
| 35 | + * |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + protected $arrayTokens = [ |
| 39 | + T_CONST, |
| 40 | + ]; |
| 41 | + |
| 42 | + /** |
| 43 | + * Registers the tokens that this sniff wants to listen for. |
| 44 | + * |
| 45 | + * @return array<int, int> |
| 46 | + * |
| 47 | + * @see Tokens.php |
| 48 | + */ |
| 49 | + public function register(): array |
| 50 | + { |
| 51 | + return $this->arrayTokens; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Processes this test, when one of its tokens is encountered. |
| 56 | + * |
| 57 | + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint |
| 58 | + * |
| 59 | + * @param File $phpcsFile The file being scanned. |
| 60 | + * @param int $stackPtr The position of the current token in |
| 61 | + * the stack passed in $tokens. |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function process(File $phpcsFile, $stackPtr): void |
| 66 | + { |
| 67 | + $tokens = $phpcsFile->getTokens(); |
| 68 | + $nextPtr = $stackPtr + 1; |
| 69 | + $next = $tokens[$nextPtr]['content']; |
| 70 | + |
| 71 | + if (T_WHITESPACE !== $tokens[$nextPtr]['code']) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (' ' === $next) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + $fix = $phpcsFile->addFixableError( |
| 80 | + 'Keyword const must be followed by a single space, but found %s', |
| 81 | + $stackPtr, |
| 82 | + 'Incorrect', |
| 83 | + [\strlen($next)] |
| 84 | + ); |
| 85 | + |
| 86 | + if (true !== $fix) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + $phpcsFile->fixer->replaceToken($nextPtr, ' '); |
| 91 | + } |
| 92 | +} |
0 commit comments