Skip to content

Commit

Permalink
Merge branch 'feature/51' into develop
Browse files Browse the repository at this point in the history
Close #51
  • Loading branch information
michalbundyra committed Nov 10, 2019
2 parents 2494384 + 7f7d147 commit d01b569
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 51 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ All notable changes to this project will be documented in this file, in reverse
- [#50](https://github.com/webimpress/coding-standard/pull/50) adds check for open and close of doc block comment in `Commenting\DocComment`.
Only short version is allowed: `/**` and `*/`. Additional asterisk are disallowed.

- [#51](https://github.com/webimpress/coding-standard/pull/51) adds check for blank lines and comments before arrow in arrays in `Array\Format` sniff.
Arrow must be after the index value, can be in new line, but any additional lines or comments are disallowed.

### Changed

- [#42](https://github.com/webimpress/coding-standard/pull/42) changes `NamingConventions\ValidVariableName` to require variable names be in strict camelCase. It means two capital letters next to each other are not allowed.
Expand All @@ -49,7 +52,7 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#51](https://github.com/webimpress/coding-standard/pull/51) fixes multiple cases when empty line before comment in array was not allowed

## 1.0.6 - TBD

Expand Down
109 changes: 60 additions & 49 deletions src/WebimpressCodingStandard/Sniffs/Arrays/FormatSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractArraySniff;
use PHP_CodeSniffer\Util\Tokens;

use function ltrim;
use function str_repeat;
use function strlen;
use function strpos;
use function trim;

use const T_CLOSE_CURLY_BRACKET;
use const T_CLOSE_SHORT_ARRAY;
use const T_COMMENT;
use const T_DOC_COMMENT_OPEN_TAG;
use const T_DOUBLE_ARROW;
use const T_WHITESPACE;

/**
Expand All @@ -39,6 +37,8 @@
* ];
* ```
* - array closing bracket in new line (code: `ClosingBracketInNewLine`).
* - no comment before arrow (code `CommentBeforeArrow`)
* - no blank line before arrow (code: `BlankLineBeforeArrow`)
*/
class FormatSniff extends AbstractArraySniff
{
Expand Down Expand Up @@ -124,57 +124,68 @@ protected function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $ar
}
}

$previousLine = $tokens[$arrayStart]['line'];
$next = $arrayStart;
while ($next = $phpcsFile->findNext(T_WHITESPACE, $next + 1, $arrayEnd, true)) {
if ($previousLine === $tokens[$next]['line']) {
if ($tokens[$next]['code'] !== T_COMMENT) {
$error = 'There must be one array element per line';
$fix = $phpcsFile->addFixableError($error, $next, 'OneElementPerLine');

if ($fix) {
$phpcsFile->fixer->beginChangeset();
if ($tokens[$next - 1]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->replaceToken($next - 1, '');
}
$phpcsFile->fixer->addNewlineBefore($next);
$phpcsFile->fixer->endChangeset();
}
}
} else {
if ($previousLine < $tokens[$next]['line'] - 1
&& (! empty($tokens[$stackPtr]['conditions'])
|| $previousLine === $tokens[$arrayStart]['line'])
) {
$firstOnLine = $phpcsFile->findFirstOnLine([], $next, true);

$error = 'Blank line is not allowed here';
$fix = $phpcsFile->addFixableError($error, $firstOnLine - 1, 'BlankLine');

if ($fix) {
$phpcsFile->fixer->replaceToken($firstOnLine - 1, '');
// $previousLine = $tokens[$arrayStart]['line'];
foreach ($indices as $element) {
$start = $element['index_start'] ?? $element['value_start'];

// For some reasons empty array has value_start = false
if (! $start) {
continue;
}

$nonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, $start - 1, null, true);
if ($tokens[$start]['line'] === $tokens[$nonEmpty]['line']) {
$error = 'There must be one array element per line';
$fix = $phpcsFile->addFixableError($error, $start, 'OneElementPerLine');

if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewline($nonEmpty);
if ($tokens[$nonEmpty + 1]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->replaceToken($nonEmpty + 1, '');
}
$phpcsFile->fixer->endChangeset();
}
}

if ($tokens[$next]['code'] === T_COMMENT
&& (strpos($tokens[$next]['content'], '//') === 0
|| strpos($tokens[$next]['content'], '#') === 0)
) {
$end = $next;
} elseif ($tokens[$next]['code'] === T_DOC_COMMENT_OPEN_TAG) {
$end = $tokens[$next]['comment_closer'];
} else {
$end = $phpcsFile->findEndOfStatement($next);
if ($tokens[$end]['code'] === T_DOUBLE_ARROW
|| $tokens[$end]['code'] === T_CLOSE_CURLY_BRACKET
) {
$end = $phpcsFile->findEndOfStatement($end);
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $start - 1, null, true);
if ($tokens[$prev]['line'] < $tokens[$start]['line'] - 1) {
$blankLine = $tokens[$prev]['line'] === $tokens[$prev + 1]['line'] ? $prev + 2 : $prev + 1;
$error = 'Blank line is not allowed here';

$fix = $phpcsFile->addFixableError($error, $blankLine, 'BlankLine');
if ($fix) {
$phpcsFile->fixer->replaceToken($blankLine, '');
}
}

$previousLine = $tokens[$end]['line'];
$next = $end;
if (! isset($element['arrow'])) {
continue;
}

$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $element['arrow'] - 1, null, true);
$content = $phpcsFile->getTokensAsString($prev + 1, $element['arrow'] - $prev - 1);
if (trim($content) !== '') {
$error = 'Comment is not allowed before arrow in array';
$comment = $phpcsFile->findNext(Tokens::$commentTokens, $prev + 1);

$fix = $phpcsFile->addFixableError($error, $comment, 'CommentBeforeArrow');
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContentBefore($element['index_start'], ltrim($content));
for ($i = $comment; $i < $element['arrow']; ++$i) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
} elseif ($tokens[$prev]['line'] < $tokens[$element['arrow']]['line'] - 1) {
$error = 'Blank line is not allowed before arrow in array';

$fix = $phpcsFile->addFixableError($error, $prev + 2, 'BlankLineBeforeArrow');
if ($fix) {
$phpcsFile->fixer->replaceToken($prev + 2, '');
}
}
}

if ($first = $phpcsFile->findFirstOnLine([], $arrayEnd, true)) {
Expand Down
46 changes: 46 additions & 0 deletions test/Sniffs/Arrays/FormatUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,49 @@ $a = [
*/
// 'key' => 'val',
];

$a = [
'a',

// b

'c',
];

$a = [
'a',

/*
multiline comment
*/

'b',
];

$x = [
// comment
'a', 'b',
'd',


// comment 1
'i' => 'd',
//comment 2
'j'

//comment 3

=>'r',

'r'
. 'x ',
// comment 4
'x1'

=> 'y1',
];

$a = [
// COMMENT
];
$a = [ /**/ ];
42 changes: 41 additions & 1 deletion test/Sniffs/Arrays/FormatUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ $a1 = ['foo', 'bar'];
$a2 = [
'foo',
'bar',

function() {

},
Expand Down Expand Up @@ -100,3 +99,44 @@ $a = [
*/
// 'key' => 'val',
];

$a = [
'a',

// b
'c',
];

$a = [
'a',

/*
multiline comment
*/
'b',
];

$x = [
// comment
'a',
'b',
'd',


// comment 1
'i' => 'd',
//comment 2
//comment 3
'j'
=>'r',
'r'
. 'x ',
// comment 4
'x1'
=> 'y1',
];

$a = [
// COMMENT
];
$a = [/**/];
8 changes: 8 additions & 0 deletions test/Sniffs/Arrays/FormatUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protected function getErrorList(string $testFile = '') : array
{
return [
2 => 2,
6 => 1,
14 => 2,
16 => 2,
19 => 1,
Expand All @@ -34,6 +35,13 @@ protected function getErrorList(string $testFile = '') : array
74 => 1,
75 => 1,
76 => 1,
95 => 1,
105 => 1,
111 => 1,
120 => 1,
123 => 1,
128 => 1,
135 => 2,
];
}

Expand Down

0 comments on commit d01b569

Please sign in to comment.