Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/PhpCs/FiveLab/Sniffs/Commenting/InheritdocSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private function assertExistDocInParents(File $phpcsFile, int $line, string $met

if ($implementRef->hasMethod($methodName)) {
$exist = true;

break;
}

Expand All @@ -116,6 +117,7 @@ private function assertExistDocInParents(File $phpcsFile, int $line, string $met
if (\preg_match('/^\*\s*@method (\S+) ([^\\\(]+)\s*\(/', \trim($docCommentLine), $parts)) {
if ($methodName === $parts[2]) {
$exist = true;

break;
}
}
Expand All @@ -124,6 +126,7 @@ private function assertExistDocInParents(File $phpcsFile, int $line, string $met
if (\preg_match('/^\*\s*@method ([^\\\(]+)\s*\(/', \trim($docCommentLine), $parts)) {
if ($methodName === $parts[1]) {
$exist = true;

break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function process(File $phpcsFile, mixed $stackPtr): void
for ($i = $stackPtr + 1; $i < $closePtr; $i++) {
if ($tokens[$i]['code'] === T_DOC_COMMENT_TAG) {
$hasMetaTag = true;

break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ class WhiteSpaceAroundControlStatementSniff implements Sniff
* @var array<array<string|int>>
*/
private static array $ignoredTokens = [
T_IF => [T_ELSE, T_ELSEIF, T_COLON],
T_ELSE => [T_IF, T_CLOSE_CURLY_BRACKET],
T_ELSEIF => [T_ELSEIF, T_CLOSE_CURLY_BRACKET, T_ELSE],
T_DO => [T_WHILE],
T_WHILE => [T_DO],
T_MATCH => [T_OPEN_SHORT_ARRAY, T_CLOSE_SHORT_ARRAY, T_COMMA],
T_IF => [T_ELSE, T_ELSEIF, T_COLON],
T_ELSE => [T_IF, T_CLOSE_CURLY_BRACKET],
T_ELSEIF => [T_ELSEIF, T_CLOSE_CURLY_BRACKET, T_ELSE],
T_DO => [T_WHILE],
T_WHILE => [T_DO],
T_MATCH => [T_OPEN_SHORT_ARRAY, T_CLOSE_SHORT_ARRAY, T_COMMA],
T_BREAK => [T_CLOSE_CURLY_BRACKET],
T_CONTINUE => [T_CLOSE_CURLY_BRACKET],
];

public function register(): array
Expand All @@ -56,14 +58,16 @@ public function register(): array
T_DO,
T_SWITCH,
T_MATCH,
T_BREAK,
T_CONTINUE,
];
}

public function process(File $phpcsFile, mixed $stackPtr): void
{
$stackToken = $phpcsFile->getTokens()[$stackPtr];

if (!\array_key_exists('scope_closer', $stackToken)) {
if (!\array_key_exists('scope_closer', $stackToken) && !\in_array($stackToken['code'], [T_BREAK, T_CONTINUE], true)) {
// Active token hasn't close scope. Maybe use "else if" (or similar) construction where scope undefined.
return;
}
Expand Down Expand Up @@ -91,16 +95,22 @@ public function process(File $phpcsFile, mixed $stackPtr): void
}

// Check blank lines after
$scopeCloserPtr = $stackToken['scope_closer'];
$nextTokenPtr = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $scopeCloserPtr + 1, null, true);
if (\array_key_exists('scope_closer', $stackToken)) {
$scopeCloserPtr = $stackToken['scope_closer'];
$nextTokenPtr = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $scopeCloserPtr + 1, null, true);
} else {
$scopeCloserPtr = $stackPtr;
$nextTokenPtr = $stackPtr + 1;
}

if ($nextTokenPtr) {
$nextToken = $phpcsFile->getTokens()[$nextTokenPtr];

if ($nextToken['code'] === T_SEMICOLON && $stackToken['code'] === T_MATCH) {
// Use "match" construction.
$nextTokenPtr = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $nextTokenPtr + 1, null, true);
$nextToken = $nextTokenPtr ? $phpcsFile->getTokens()[$nextTokenPtr] : null;
if ($nextToken['code'] === T_SEMICOLON) {
if (\in_array($stackToken['code'], [T_MATCH, T_BREAK, T_CONTINUE], true)) {
$nextTokenPtr = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $nextTokenPtr + 1, null, true);
$nextToken = $nextTokenPtr ? $phpcsFile->getTokens()[$nextTokenPtr] : null;
}
}

if ($nextToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class WhiteSpaceBeforeChainCallSniff implements Sniff
{
const GAP = 4;
const int GAP = 4;

public function register(): array
{
Expand Down Expand Up @@ -96,21 +96,14 @@ public function process(File $phpcsFile, mixed $stackPtr): void
);
}

/**
* Calculate whitespaces before first token on line
*
* @param File $phpcsFile
* @param int $line
*
* @return int
*/
private function calculateWhitespacesBeforeFirstTokenOnLine(File $phpcsFile, int $line): int
{
$whitespaces = 0;

foreach (PhpCsUtils::getTokensOnLine($phpcsFile, $line) as $token) {
if (T_WHITESPACE === $token['code']) {
$whitespaces += $token['length'];

continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,18 @@
if (false) {
$bar = 'bar';
}
}

break;
}

if ($condition) {
$bar = 'bar';

break;
}

for ($i = 0; $i < 10; $i++) {
$someArray[] = $i;

continue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@
$a .= $key;
}
$bar = 'foo';

if ($a) {
$a = 'bar';
break;
}

foreach ($array as $key => $item) {
$a .= $key;
continue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public static function provideDataSet(): array
'message' => 'Must be one blank line after close "foreach" statement.',
'source' => 'FiveLab.Formatting.WhiteSpaceAroundControlStatement.MissedLineAfter',
],
[
'message' => 'Must be one blank line before "break" statement.',
'source' => 'FiveLab.Formatting.WhiteSpaceAroundControlStatement.MissedLineBefore',
],
[
'message' => 'Must be one blank line before "continue" statement.',
'source' => 'FiveLab.Formatting.WhiteSpaceAroundControlStatement.MissedLineBefore',
],
],
];
}
Expand Down