Passed
Push — master ( 259207...f50b15 )
by Michael
03:00 queued 25s
created

ConstantSpacingSniff   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 19
c 1
b 1
f 0
dl 0
loc 60
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
A process() 0 26 4
1
<?php
2
3
/**
4
 * This file is part of the mo4-coding-standard (phpcs standard)
5
 *
6
 * @author  Xaver Loppenstedt <[email protected]>
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 <[email protected]>
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
}
93