forked from FiveLab/CiRules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhiteSpaceAroundMultilineArraySniff.php
More file actions
89 lines (71 loc) · 2.91 KB
/
Copy pathWhiteSpaceAroundMultilineArraySniff.php
File metadata and controls
89 lines (71 loc) · 2.91 KB
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
<?php
declare(strict_types = 1);
/*
* This file is part of the FiveLab CiRules package
*
* (c) FiveLab
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Formatting;
use FiveLab\Component\CiRules\PhpCs\FiveLab\ErrorCodes;
use FiveLab\Component\CiRules\PhpCs\FiveLab\PhpCsUtils;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
/**
* Check what exist one blank line before and after multiline array creation.
*/
class WhiteSpaceAroundMultilineArraySniff implements Sniff
{
public function register(): array
{
return [
T_CLOSE_SHORT_ARRAY,
];
}
public function process(File $phpcsFile, mixed $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
$semicolonPtr = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $stackPtr + 1, null, true);
if (!$semicolonPtr || $tokens[$semicolonPtr]['code'] !== T_SEMICOLON) {
// Not close root array. Skip.
return;
}
$openerTokenPtr = $tokens[$stackPtr]['bracket_opener'];
$openerLineNumber = $tokens[$openerTokenPtr]['line'];
$closerLineNumber = $tokens[$stackPtr]['line'];
if ($closerLineNumber === $openerLineNumber) {
// Single line array. Skip.
return;
}
// Check before.
$firstTokenOnOpenerLinePtr = PhpCsUtils::findFirstTokenOnLine($phpcsFile, $tokens[$openerTokenPtr]['line']);
$prevTokenPtr = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, $firstTokenOnOpenerLinePtr - 1, null, true);
$prevToken = $tokens[$prevTokenPtr];
$diffLinesBefore = PhpCsUtils::getDiffLines($phpcsFile, (int) $prevTokenPtr, (int) $firstTokenOnOpenerLinePtr);
$possiblePrevTokens = [T_COLON, T_OPEN_CURLY_BRACKET];
if (!\in_array($prevToken['code'], $possiblePrevTokens, true) && $diffLinesBefore < 2) {
$phpcsFile->addError(
'Must be one blank line before multiline array creation.',
$openerTokenPtr,
ErrorCodes::MISSED_LINE_BEFORE
);
}
// Check after
$nextTokenPtr = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $semicolonPtr + 1, null, true);
if ($nextTokenPtr) {
$nextToken = $tokens[$nextTokenPtr];
$diffLinesAfter = PhpCsUtils::getDiffLines($phpcsFile, $semicolonPtr, $nextTokenPtr);
$possibleNextTokens = [T_CLOSE_CURLY_BRACKET, T_BREAK];
if (!\in_array($nextToken['code'], $possibleNextTokens, true) && $diffLinesAfter < 2) {
$phpcsFile->addError(
'Must be one blank line after multiline array creation.',
$semicolonPtr,
ErrorCodes::MISSED_LINE_AFTER
);
}
}
}
}