-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMultiLineArraySniff.php
126 lines (111 loc) · 3.25 KB
/
MultiLineArraySniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* This file is part of the mo4-coding-standard (phpcs standard)
*
* @author Xaver Loppenstedt <[email protected]>
*
* @license http://spdx.org/licenses/MIT MIT License
*
* @link https://github.com/mayflower/mo4-coding-standard
*/
declare(strict_types=1);
namespace MO4\Sniffs\Arrays;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Multi Line Array sniff.
*
* @author Xaver Loppenstedt <[email protected]>
*
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
*
* @license http://spdx.org/licenses/MIT MIT License
*
* @link https://github.com/mayflower/mo4-coding-standard
*
* @psalm-api
*/
class MultiLineArraySniff implements Sniff
{
/**
* Define all types of arrays.
*
* @var array
*/
protected $arrayTokens = [
// @phan-suppress-next-line PhanUndeclaredConstant
T_OPEN_SHORT_ARRAY,
T_ARRAY,
];
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array<int, int>
*
* @see Tokens.php
*/
public function register(): array
{
return $this->arrayTokens;
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
$current = $tokens[$stackPtr];
if (T_ARRAY === $current['code']) {
$arrayType = 'parenthesis';
$start = $current['parenthesis_opener'];
$end = $current['parenthesis_closer'];
} else {
$arrayType = 'bracket';
$start = $current['bracket_opener'];
$end = $current['bracket_closer'];
}
if ($tokens[$start]['line'] === $tokens[$end]['line']) {
return;
}
if ($tokens[($start + 2)]['line'] === $tokens[$start]['line']) {
$fixable = $phpcsFile->addFixableError(
\sprintf(
'opening %s of multi line array must be followed by newline',
$arrayType
),
$start,
'OpeningMustBeFollowedByNewline'
);
if (true === $fixable) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewline($start);
$phpcsFile->fixer->endChangeset();
}
}
if ($tokens[($end - 2)]['line'] !== $tokens[$end]['line']) {
return;
}
$fixable = $phpcsFile->addFixableError(
\sprintf(
'closing %s of multi line array must in own line',
$arrayType
),
$end,
'ClosingMustBeInOwnLine'
);
if (true !== $fixable) {
return;
}
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewlineBefore($end);
$phpcsFile->fixer->endChangeset();
}
}