Skip to content

Commit d2e7fb9

Browse files
committed
Switch coding standard for MO4 files to MO4
1 parent 64846ca commit d2e7fb9

18 files changed

+604
-594
lines changed

.github/CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Contributing
22

33
If you contribute code, please make sure it conforms to the
4-
[PHPCS coding standard](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/phpcs.xml.dist)
4+
MO4 coding standard
55
and that the unit tests still pass.
66
Whenever possible, add an auto fixer for coding standard violations.
77

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ script:
4242
- diff -B phpcs.xml.dist <(xmllint --format phpcs.xml.dist)
4343
- vendor/bin/phpcs -s --standard=MO4 integrationtests/testfile.php # Check, if all the sniffs work with the current dependencies
4444
- vendor/bin/phpunit # Run tests
45-
- vendor/bin/phpcs # Stylecheck against PHPCS's ruleset
45+
- vendor/bin/phpcs # Stylecheck against MO4
4646
- vendor/bin/phpstan analyse --no-progress # Run PHPStan
4747
- vendor/bin/psalm --show-info=false # run psalm
4848
- if [ "${DEPENDENCIES}" = "highest" ]; then vendor/bin/phan; fi; # Run phan

MO4/Library/PregLibrary.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?php
2+
23
/**
34
* This file is part of the mo4-coding-standard (phpcs standard)
45
*
56
* @author Michael Moll <[email protected]>
7+
*
68
* @license http://spdx.org/licenses/MIT MIT License
9+
*
710
* @link https://github.com/mayflower/mo4-coding-standard
811
*/
12+
913
declare(strict_types=1);
1014

1115
namespace MO4\Library;
@@ -14,8 +18,6 @@
1418

1519
class PregLibrary
1620
{
17-
18-
1921
/**
2022
* Split string by a regular expression
2123
*
@@ -31,23 +33,21 @@ class PregLibrary
3133
* PREG_SPLIT_OFFSET_CAPTURE: If this flag is set, for every occurring match the appendant
3234
* string offset will also be returned.
3335
*
34-
* @return string[]|array[]
36+
* @return array<string>|array<array>
3537
*
3638
* @throws RuntimeException
3739
*
3840
* @psalm-suppress ArgumentTypeCoercion
3941
*/
40-
public static function mo4_preg_split($pattern, $subject, $limit=-1, $flags=0): array
42+
public static function MO4PregSplit(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
4143
{
4244
$pregSplitResult = \preg_split($pattern, $subject, $limit, $flags);
43-
// @phan-suppress-next-line PhanTypeComparisonFromArray
44-
if ($pregSplitResult === false) {
45+
46+
// @phan-suppress-next-line PhanTypeComparisonToArray
47+
if (false === $pregSplitResult) {
4548
throw new RuntimeException('Unexpected Error in MO4 Coding Standard.');
4649
}
4750

4851
return $pregSplitResult;
49-
50-
}//end mo4_preg_split()
51-
52-
53-
}//end class
52+
}
53+
}

MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php

+89-83
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
* This file is part of the mo4-coding-standard (phpcs standard)
55
*
66
* @author Xaver Loppenstedt <[email protected]>
7+
*
78
* @license http://spdx.org/licenses/MIT MIT License
9+
*
810
* @link https://github.com/mayflower/mo4-coding-standard
911
*/
12+
1013
declare(strict_types=1);
1114

1215
namespace MO4\Sniffs\Arrays;
@@ -21,41 +24,43 @@
2124
* '=>' must be aligned in arrays, and the key and the '=>' must be in the same line
2225
*
2326
* @author Xaver Loppenstedt <[email protected]>
27+
*
2428
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
29+
*
2530
* @license http://spdx.org/licenses/MIT MIT License
31+
*
2632
* @link https://github.com/mayflower/mo4-coding-standard
2733
*/
2834
class ArrayDoubleArrowAlignmentSniff implements Sniff
2935
{
30-
3136
/**
3237
* Define all types of arrays.
3338
*
3439
* @var array
3540
*/
36-
protected $arrayTokens = [
41+
protected $arrayTokens = [
3742
// @phan-suppress-next-line PhanUndeclaredConstant
3843
T_OPEN_SHORT_ARRAY,
3944
T_ARRAY,
4045
];
4146

42-
4347
/**
4448
* Registers the tokens that this sniff wants to listen for.
4549
*
4650
* @return array<int, int>
51+
*
4752
* @see Tokens.php
4853
*/
4954
public function register(): array
5055
{
5156
return $this->arrayTokens;
52-
53-
}//end register()
54-
57+
}
5558

5659
/**
5760
* Processes this test, when one of its tokens is encountered.
5861
*
62+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
63+
*
5964
* @param File $phpcsFile The file being scanned.
6065
* @param int $stackPtr The position of the current token in
6166
* the stack passed in $tokens.
@@ -69,7 +74,7 @@ public function process(File $phpcsFile, $stackPtr): void
6974
$tokens = $phpcsFile->getTokens();
7075
$current = $tokens[$stackPtr];
7176

72-
if ($current['code'] === T_ARRAY) {
77+
if (T_ARRAY === $current['code']) {
7378
$start = $current['parenthesis_opener'];
7479
$end = $current['parenthesis_closer'];
7580
} else {
@@ -93,82 +98,82 @@ public function process(File $phpcsFile, $stackPtr): void
9398
$previous = $tokens[($i - 1)];
9499

95100
// Skip nested arrays.
96-
if (\in_array($current['code'], $this->arrayTokens, true) === true) {
97-
if ($current['code'] === T_ARRAY) {
98-
$i = ($current['parenthesis_closer'] + 1);
99-
} else {
100-
$i = ($current['bracket_closer'] + 1);
101-
}
101+
if (true === \in_array($current['code'], $this->arrayTokens, true)) {
102+
$i = T_ARRAY === $current['code'] ? ($current['parenthesis_closer'] + 1) : ($current['bracket_closer'] + 1);
102103

103104
continue;
104105
}
105106

106107
// Skip closures in array.
107-
if ($current['code'] === T_CLOSURE) {
108+
if (T_CLOSURE === $current['code']) {
108109
$i = ($current['scope_closer'] + 1);
110+
109111
continue;
110112
}
111113

112114
$i = (int) $i;
113115

114-
if ($current['code'] === T_DOUBLE_ARROW) {
115-
$assignments[] = $i;
116-
$column = $previous['column'];
117-
$line = $current['line'];
118-
119-
if ($lastLine === $line) {
120-
$previousComma = $this->getPreviousComma($phpcsFile, $i, $start);
121-
122-
$msg = 'only one "=>" assignments per line is allowed in a multi line array';
123-
124-
if ($previousComma !== false) {
125-
$fixable = $phpcsFile->addFixableError($msg, $i, 'OneAssignmentPerLine');
126-
127-
if ($fixable === true) {
128-
$phpcsFile->fixer->beginChangeset();
129-
$phpcsFile->fixer->addNewline((int) $previousComma);
130-
$phpcsFile->fixer->endChangeset();
131-
}
132-
} else {
133-
// Remove current and previous '=>' from array for further processing.
134-
\array_pop($assignments);
135-
\array_pop($assignments);
136-
$phpcsFile->addError($msg, $i, 'OneAssignmentPerLine');
137-
}
138-
}
116+
if (T_DOUBLE_ARROW !== $current['code']) {
117+
continue;
118+
}
139119

140-
$hasKeyInLine = false;
120+
$assignments[] = $i;
121+
$column = $previous['column'];
122+
$line = $current['line'];
141123

142-
$j = ($i - 1);
143-
while (($j >= 0) && ($tokens[$j]['line'] === $current['line'])) {
144-
if (\in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens, true) === false) {
145-
$hasKeyInLine = true;
146-
}
124+
if ($lastLine === $line) {
125+
$previousComma = $this->getPreviousComma($phpcsFile, $i, $start);
147126

148-
$j--;
149-
}
127+
$msg = 'only one "=>" assignments per line is allowed in a multi line array';
150128

151-
if ($hasKeyInLine === false) {
152-
$fixable = $phpcsFile->addFixableError(
153-
'in arrays, keys and "=>" must be on the same line',
154-
$i,
155-
'KeyAndValueNotOnSameLine'
156-
);
129+
if (false !== $previousComma) {
130+
$fixable = $phpcsFile->addFixableError($msg, $i, 'OneAssignmentPerLine');
157131

158-
if ($fixable === true) {
132+
if (true === $fixable) {
159133
$phpcsFile->fixer->beginChangeset();
160-
$phpcsFile->fixer->replaceToken($j, '');
134+
$phpcsFile->fixer->addNewline((int) $previousComma);
161135
$phpcsFile->fixer->endChangeset();
162136
}
137+
} else {
138+
// Remove current and previous '=>' from array for further processing.
139+
\array_pop($assignments);
140+
\array_pop($assignments);
141+
$phpcsFile->addError($msg, $i, 'OneAssignmentPerLine');
163142
}
143+
}
144+
145+
$hasKeyInLine = false;
164146

165-
if ($column > $keyEndColumn) {
166-
$keyEndColumn = $column;
147+
$j = ($i - 1);
148+
149+
while (($j >= 0) && ($tokens[$j]['line'] === $current['line'])) {
150+
if (false === \in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens, true)) {
151+
$hasKeyInLine = true;
152+
}
153+
154+
$j--;
155+
}
156+
157+
if (false === $hasKeyInLine) {
158+
$fixable = $phpcsFile->addFixableError(
159+
'in arrays, keys and "=>" must be on the same line',
160+
$i,
161+
'KeyAndValueNotOnSameLine'
162+
);
163+
164+
if (true === $fixable) {
165+
$phpcsFile->fixer->beginChangeset();
166+
$phpcsFile->fixer->replaceToken($j, '');
167+
$phpcsFile->fixer->endChangeset();
167168
}
169+
}
168170

169-
$lastLine = $line;
170-
}//end if
171-
}//end for
171+
if ($column > $keyEndColumn) {
172+
$keyEndColumn = $column;
173+
}
174+
175+
$lastLine = $line;
176+
}
172177

173178
$doubleArrowStartColumn = ($keyEndColumn + 1);
174179

@@ -179,26 +184,28 @@ public function process(File $phpcsFile, $stackPtr): void
179184
$beforeArrowPtr = ($ptr - 1);
180185
$currentIndent = \strlen($tokens[$beforeArrowPtr]['content']);
181186
$correctIndent = ($currentIndent - $column + $doubleArrowStartColumn);
182-
if ($column !== $doubleArrowStartColumn) {
183-
$fixable = $phpcsFile->addFixableError("each \"=>\" assignments must be aligned; current indentation before \"=>\" are $currentIndent space(s), must be $correctIndent space(s)", $ptr, 'AssignmentsNotAligned');
184187

185-
if ($fixable === false) {
186-
continue;
187-
}
188+
if ($column === $doubleArrowStartColumn) {
189+
continue;
190+
}
188191

189-
$phpcsFile->fixer->beginChangeset();
190-
if ($tokens[$beforeArrowPtr]['code'] === T_WHITESPACE) {
191-
$phpcsFile->fixer->replaceToken($beforeArrowPtr, \str_repeat(' ', $correctIndent));
192-
} else {
193-
$phpcsFile->fixer->addContent($beforeArrowPtr, \str_repeat(' ', $correctIndent));
194-
}
192+
$fixable = $phpcsFile->addFixableError("each \"=>\" assignments must be aligned; current indentation before \"=>\" are {$currentIndent} space(s), must be {$correctIndent} space(s)", $ptr, 'AssignmentsNotAligned');
195193

196-
$phpcsFile->fixer->endChangeset();
194+
if (false === $fixable) {
195+
continue;
197196
}
198-
}//end foreach
199197

200-
}//end process()
198+
$phpcsFile->fixer->beginChangeset();
201199

200+
if (T_WHITESPACE === $tokens[$beforeArrowPtr]['code']) {
201+
$phpcsFile->fixer->replaceToken($beforeArrowPtr, \str_repeat(' ', $correctIndent));
202+
} else {
203+
$phpcsFile->fixer->addContent($beforeArrowPtr, \str_repeat(' ', $correctIndent));
204+
}
205+
206+
$phpcsFile->fixer->endChangeset();
207+
}
208+
}
202209

203210
/**
204211
* Find previous comma in array.
@@ -210,28 +217,27 @@ public function process(File $phpcsFile, $stackPtr): void
210217
*
211218
* @return bool|int
212219
*/
213-
private function getPreviousComma(File $phpcsFile, $stackPtr, $start)
220+
private function getPreviousComma(File $phpcsFile, int $stackPtr, int $start)
214221
{
215222
$previousComma = false;
216223
$tokens = $phpcsFile->getTokens();
217224

218225
$ptr = $phpcsFile->findPrevious([T_COMMA, T_CLOSE_SHORT_ARRAY], $stackPtr, $start);
219-
while ($ptr !== false) {
220-
if ($tokens[$ptr]['code'] === T_COMMA) {
226+
227+
while (false !== $ptr) {
228+
if (T_COMMA === $tokens[$ptr]['code']) {
221229
$previousComma = $ptr;
230+
222231
break;
223232
}
224233

225-
if ($tokens[$ptr]['code'] === T_CLOSE_SHORT_ARRAY) {
234+
if (T_CLOSE_SHORT_ARRAY === $tokens[$ptr]['code']) {
226235
$ptr = $tokens[$ptr]['bracket_opener'];
227236
}
228237

229238
$ptr = $phpcsFile->findPrevious([T_COMMA, T_CLOSE_SHORT_ARRAY], ($ptr - 1), $start);
230239
}
231240

232241
return $previousComma;
233-
234-
}//end getPreviousComma()
235-
236-
237-
}//end class
242+
}
243+
}

0 commit comments

Comments
 (0)