Skip to content

Commit 98f658a

Browse files
xaloppmmoll
authored andcommitted
split up array alignment sniff
1 parent 2927c7a commit 98f658a

11 files changed

+461
-50
lines changed

MO4/Sniffs/Formatting/ArrayAlignmentSniff.php renamed to MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
* @version GIT: master
1313
* @link https://github.com/Mayflower/mo4-coding-standard
1414
*/
15-
namespace MO4\Sniffs\Formatting;
15+
namespace MO4\Sniffs\Arrays;
1616

1717
use PHP_CodeSniffer\Files\File;
1818
use PHP_CodeSniffer\Sniffs\Sniff;
1919
use PHP_CodeSniffer\Util\Tokens as PHP_CodeSniffer_Tokens;
2020

2121
/**
22-
* Array Alignment sniff.
22+
* Array Double Arrow Alignment sniff.
2323
*
2424
* '=>' must be aligned in arrays, and the key and the '=>' must be in the same line
2525
*
@@ -30,7 +30,7 @@
3030
* @license http://spdx.org/licenses/MIT MIT License
3131
* @link https://github.com/Mayflower/mo4-coding-standard
3232
*/
33-
class ArrayAlignmentSniff implements Sniff
33+
class ArrayDoubleArrowAlignmentSniff implements Sniff
3434
{
3535
/**
3636
* Define all types of arrays.
@@ -80,21 +80,6 @@ public function process(File $phpcsFile, $stackPtr)
8080

8181
if ($tokens[$start]['line'] === $tokens[$end]['line']) {
8282
return;
83-
} if ($tokens[($end - 2)]['line'] === $tokens[$end]['line']) {
84-
if ($current['code'] === T_ARRAY) {
85-
$arrayBrackets = 'parenthesis';
86-
} else {
87-
$arrayBrackets = 'bracket';
88-
}
89-
90-
$phpcsFile->addError(
91-
sprintf(
92-
'closing %s of array must in own line',
93-
$arrayBrackets
94-
),
95-
$end,
96-
'ClosingMustBeInOwnLine'
97-
);
9883
}
9984

10085
$assignments = array();
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the mo4-coding-standard (phpcs standard)
5+
*
6+
* PHP version 5
7+
*
8+
* @category PHP
9+
* @package PHP_CodeSniffer-MO4
10+
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
11+
* @license http://spdx.org/licenses/MIT MIT License
12+
* @version GIT: master
13+
* @link https://github.com/Mayflower/mo4-coding-standard
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+
* @category PHP
24+
* @package PHP_CodeSniffer-MO4
25+
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
26+
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
27+
* @license http://spdx.org/licenses/MIT MIT License
28+
* @link https://github.com/Mayflower/mo4-coding-standard
29+
*/
30+
class MultiLineArraySniff implements Sniff
31+
{
32+
/**
33+
* Define all types of arrays.
34+
*
35+
* @var array
36+
*/
37+
protected $arrayTokens = array(
38+
T_ARRAY,
39+
T_OPEN_SHORT_ARRAY,
40+
);
41+
42+
43+
/**
44+
* Registers the tokens that this sniff wants to listen for.
45+
*
46+
* @return array(int)
47+
* @see Tokens.php
48+
*/
49+
public function register()
50+
{
51+
return $this->arrayTokens;
52+
53+
}//end register()
54+
55+
56+
/**
57+
* Processes this test, when one of its tokens is encountered.
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)
66+
{
67+
$tokens = $phpcsFile->getTokens();
68+
$current = $tokens[$stackPtr];
69+
70+
if ($current['code'] === T_ARRAY) {
71+
$arrayType = 'parenthesis';
72+
$start = $current['parenthesis_opener'];
73+
$end = $current['parenthesis_closer'];
74+
} else {
75+
$arrayType = 'bracket';
76+
$start = $current['bracket_opener'];
77+
$end = $current['bracket_closer'];
78+
}
79+
80+
if ($tokens[$start]['line'] === $tokens[$end]['line']) {
81+
return;
82+
}
83+
84+
if ($tokens[($start + 2)]['line'] === $tokens[$start]['line']) {
85+
$fixable = $phpcsFile->addFixableError(
86+
sprintf(
87+
'opening %s of multi line array must be followed by newline',
88+
$arrayType
89+
),
90+
$start,
91+
'OpeningMustBeFollowedByNewline'
92+
);
93+
94+
if ($fixable === true) {
95+
$phpcsFile->fixer->beginChangeset();
96+
$phpcsFile->fixer->addNewline($start);
97+
$phpcsFile->fixer->endChangeset();
98+
}
99+
}
100+
101+
if ($tokens[($end - 2)]['line'] === $tokens[$end]['line']) {
102+
$fixable = $phpcsFile->addFixableError(
103+
sprintf(
104+
'closing %s of multi line array must in own line',
105+
$arrayType
106+
),
107+
$end,
108+
'ClosingMustBeInOwnLine'
109+
);
110+
111+
if ($fixable === true) {
112+
$phpcsFile->fixer->beginChangeset();
113+
$phpcsFile->fixer->addNewlineBefore($end);
114+
$phpcsFile->fixer->endChangeset();
115+
}
116+
}
117+
118+
}//end process()
119+
120+
121+
}//end class

MO4/Tests/Formatting/ArrayAlignmentUnitTest.fail.inc renamed to MO4/Tests/Arrays/ArrayDoubleArrowAlignmentUnitTest.fail.inc

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,3 @@ $a = [
135135
98765432
136136
=> 'fail',
137137
];
138-
139-
$values = [
140-
'Test:1' => [
141-
'object_id' => 1,
142-
'object_class' => 'Test',
143-
'object_class_name' => 'TestClass',
144-
'object_name' => 'TestObject',
145-
'id' => 'Test:1', ],
146-
'Test:2' => [
147-
'object_id' => 2,
148-
'object_class' => 'Test',
149-
'object_class_name' => 'TestClass',
150-
'object_name' => 'TestObject',
151-
'id' => 'Test:2', ], ];
152-
153-
154-
$fail = array(
155-
1);
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
4+
$a = [
5+
'one' => 1,
6+
'four' => 4,
7+
];
8+
9+
$a = [
10+
'one' => 1,
11+
2,
12+
'four' => 4,
13+
];
14+
15+
// bad
16+
$a = [
17+
'one' => 1, 'two' => 2,
18+
'three' => 1, 'four' => 4,
19+
];
20+
21+
$a = [
22+
'one' => 1,
23+
2,
24+
'four' /* comment */ => 4,
25+
];
26+
27+
$a = [
28+
'one' => 1,
29+
2,
30+
// comment
31+
'four' => 4,
32+
];
33+
34+
$a = [
35+
2,
36+
'four' => 4,
37+
// comment
38+
'one' => /* comment */
39+
1,
40+
];
41+
42+
$a = array(
43+
'short' => 1,
44+
'verylongerkey' => 2,
45+
'x' => 3,
46+
);
47+
48+
$a = [
49+
'short' => 1,
50+
'verylongerkey' => 2,
51+
'x' => 3,
52+
];
53+
54+
$a = array(
55+
'short' => 1,
56+
'verylongerkey' => 2,
57+
'array' => array(
58+
'val1' => 1,
59+
'val112' => 1,
60+
'val11234567' => 1,
61+
'v' => 1,
62+
),
63+
'x' => 3,
64+
);
65+
66+
$a = [
67+
'short' => 1,
68+
'verylongerkey' => 2,
69+
'array' => [
70+
'val1' => 1,
71+
'val112' => 1,
72+
'val1123456' => 1,
73+
'v' => 1,
74+
],
75+
'x' => 3,
76+
];
77+
78+
$a = [
79+
'short' => 1,
80+
'verylongerkey' => 2,
81+
'array' => array(
82+
'val1' => 1,
83+
'val112' => 1,
84+
'val11234567' => 1,
85+
'v' => 1,
86+
),
87+
'x' => 3,
88+
];
89+
90+
$a = array(
91+
'short' => 1,
92+
'verylongerkey' => 2,
93+
'array' => [
94+
'val1' => 1,
95+
'val112' => 1,
96+
'val1123' => 1,
97+
'v' => 1,
98+
],
99+
'x' => 3,
100+
);
101+
102+
$a = [
103+
0 => [ 1 => 'O', 0 => 'O'],
104+
1 => [ 1 => 'O', 0 => 'I'],
105+
2 => [ 1 => 'I', 0 => 'O'],
106+
3 => [ 1 => 'I', 0 => 'I'],
107+
];
108+
109+
foreach ($x as $k => $v) {
110+
111+
}
112+
113+
$a = [
114+
function () {
115+
$b = [
116+
'one' => 1,
117+
'four' => 4,
118+
];
119+
foreach ($b as $k => $v) {
120+
}
121+
$bar = [
122+
'one' => 1,
123+
'four' => 4,
124+
];
125+
foreach ($bar as $key => $value) {
126+
}
127+
},
128+
function () {
129+
},
130+
'one' => 1,
131+
2,
132+
'four' => 2,
133+
123456789
134+
/* bla */ => 'fail',
135+
98765432
136+
=> 'fail',
137+
];

MO4/Tests/Formatting/ArrayAlignmentUnitTest.pass.inc renamed to MO4/Tests/Arrays/ArrayDoubleArrowAlignmentUnitTest.pass.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,4 @@ $a = [
126126
'one' => 1,
127127
2,
128128
'four' => 2,
129-
];
129+
];

MO4/Tests/Formatting/ArrayAlignmentUnitTest.php renamed to MO4/Tests/Arrays/ArrayDoubleArrowAlignmentUnitTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
* @link https://github.com/Mayflower/mo4-coding-standard
1414
*/
1515

16+
namespace MO4\Tests\Arrays;
17+
18+
use PHP_CodeSniffer\Exceptions\RuntimeException;
19+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
20+
1621
/**
17-
* Unit test class for the ArrayValueAlignment sniff.
22+
* Unit test class for @see ArrayDoubleArrowAlignmentSniff
1823
*
1924
* A sniff unit test checks a .inc file for expected violations of a single
2025
* coding standard. Expected errors and warnings are stored in this class.
2126
*
2227
* @category PHP
2328
* @package PHP_CodeSniffer-MO4
2429
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
25-
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
30+
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
2631
* @license http://spdx.org/licenses/MIT MIT License
2732
* @link https://github.com/Mayflower/mo4-coding-standard
2833
*/
29-
namespace MO4\Tests\Formatting;
30-
31-
use PHP_CodeSniffer\Exceptions\RuntimeException;
32-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
33-
34-
class ArrayAlignmentUnitTest extends AbstractSniffUnitTest
34+
class ArrayDoubleArrowAlignmentUnitTest extends AbstractSniffUnitTest
3535
{
3636

3737

@@ -49,9 +49,9 @@ class ArrayAlignmentUnitTest extends AbstractSniffUnitTest
4949
protected function getErrorList($testFile='')
5050
{
5151
switch ($testFile) {
52-
case 'ArrayAlignmentUnitTest.pass.inc':
52+
case 'ArrayDoubleArrowAlignmentUnitTest.pass.inc':
5353
return array();
54-
case 'ArrayAlignmentUnitTest.fail.inc':
54+
case 'ArrayDoubleArrowAlignmentUnitTest.fail.inc':
5555
return array(
5656
5 => 1,
5757
10 => 1,
@@ -82,9 +82,6 @@ protected function getErrorList($testFile='')
8282
132 => 1,
8383
134 => 1,
8484
136 => 2,
85-
145 => 1,
86-
151 => 2,
87-
155 => 1,
8885
);
8986
}//end switch
9087

0 commit comments

Comments
 (0)