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\Arrays; |
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-2017 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 MultiLineArraySniff implements Sniff |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Define all types of arrays. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $arrayTokens = [ |
39
|
|
|
// @phan-suppress-next-line PhanUndeclaredConstant |
40
|
|
|
T_OPEN_SHORT_ARRAY, |
41
|
|
|
T_ARRAY, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Registers the tokens that this sniff wants to listen for. |
46
|
|
|
* |
47
|
|
|
* @return array<int, int> |
48
|
|
|
* |
49
|
|
|
* @see Tokens.php |
50
|
|
|
*/ |
51
|
|
|
public function register(): array |
52
|
|
|
{ |
53
|
|
|
return $this->arrayTokens; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Processes this test, when one of its tokens is encountered. |
58
|
|
|
* |
59
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint |
60
|
|
|
* |
61
|
|
|
* @param File $phpcsFile The file being scanned. |
62
|
|
|
* @param int $stackPtr The position of the current token in |
63
|
|
|
* the stack passed in $tokens. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function process(File $phpcsFile, $stackPtr): void |
68
|
|
|
{ |
69
|
|
|
$tokens = $phpcsFile->getTokens(); |
70
|
|
|
$current = $tokens[$stackPtr]; |
71
|
|
|
|
72
|
|
|
if (T_ARRAY === $current['code']) { |
73
|
|
|
$arrayType = 'parenthesis'; |
74
|
|
|
$start = $current['parenthesis_opener']; |
75
|
|
|
$end = $current['parenthesis_closer']; |
76
|
|
|
} else { |
77
|
|
|
$arrayType = 'bracket'; |
78
|
|
|
$start = $current['bracket_opener']; |
79
|
|
|
$end = $current['bracket_closer']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($tokens[$start]['line'] === $tokens[$end]['line']) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($tokens[($start + 2)]['line'] === $tokens[$start]['line']) { |
87
|
|
|
$fixable = $phpcsFile->addFixableError( |
88
|
|
|
\sprintf( |
89
|
|
|
'opening %s of multi line array must be followed by newline', |
90
|
|
|
$arrayType |
91
|
|
|
), |
92
|
|
|
$start, |
93
|
|
|
'OpeningMustBeFollowedByNewline' |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
if (true === $fixable) { |
97
|
|
|
$phpcsFile->fixer->beginChangeset(); |
98
|
|
|
$phpcsFile->fixer->addNewline($start); |
99
|
|
|
$phpcsFile->fixer->endChangeset(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($tokens[($end - 2)]['line'] !== $tokens[$end]['line']) { |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$fixable = $phpcsFile->addFixableError( |
108
|
|
|
\sprintf( |
109
|
|
|
'closing %s of multi line array must in own line', |
110
|
|
|
$arrayType |
111
|
|
|
), |
112
|
|
|
$end, |
113
|
|
|
'ClosingMustBeInOwnLine' |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
if (true !== $fixable) { |
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$phpcsFile->fixer->beginChangeset(); |
121
|
|
|
$phpcsFile->fixer->addNewlineBefore($end); |
122
|
|
|
$phpcsFile->fixer->endChangeset(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|