Skip to content

Commit

Permalink
EarlyExitSniff: New option "ignoreOneLineTrailingIf"
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Feb 11, 2020
1 parent 01f7373 commit 055af01
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ foreach ($values as $value) {
}
```

* `ignoreOneLineTrailingIf`: ignores `if` that has one line content and is on the last position in scope, like this:

```php
foreach ($values as $value) {
$value .= 'whatever';

if ($value) {
doSomething();
}
}
```

#### SlevomatCodingStandard.Functions.StaticClosure 🔧

Reports closures not using `$this` that are not declared `static`.
Expand Down
10 changes: 10 additions & 0 deletions SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class EarlyExitSniff implements Sniff
/** @var bool */
public $ignoreStandaloneIfInScope = false;

/** @var bool */
public $ignoreOneLineTrailingIf = false;

/**
* @return array<int, (int|string)>
*/
Expand Down Expand Up @@ -283,6 +286,13 @@ private function processIf(File $phpcsFile, int $ifPointer): void
return;
}

if (
$this->ignoreOneLineTrailingIf
&& $tokens[$tokens[$ifPointer]['scope_opener']]['line'] + 2 === $tokens[$tokens[$ifPointer]['scope_closer']]['line']
) {
return;
}

$scopePointer = $tokens[$nextPointer]['scope_condition'];

if (!in_array($tokens[$scopePointer]['code'], [T_FUNCTION, T_CLOSURE, T_WHILE, T_DO, T_FOREACH, T_FOR], true)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/Sniffs/ControlStructures/EarlyExitSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,27 @@ public function testNotIgnoredStandaloneIfInScopeErrors(): void
self::assertSniffError($report, 11, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit to reduce code nesting.');
}

public function testIgnoredOneLineTrailingIfNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/earlyExitIgnoredOneLineTrailingIfNoErrors.php', [
'ignoreOneLineTrailingIf' => true,
]);

self::assertNoSniffErrorInFile($report);
}

public function testIgnoredOneLineTrailingIfErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/earlyExitIgnoredOneLineTrailingIfErrors.php', [
'ignoreOneLineTrailingIf' => true,
]);

self::assertSame(2, $report->getErrorCount());

self::assertSniffError($report, 7, EarlyExitSniff::CODE_USELESS_ELSEIF);
self::assertSniffError($report, 17, EarlyExitSniff::CODE_USELESS_ELSEIF);

self::assertAllFixedInFile($report);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

function ($values) {
foreach ($values as $value) {
if ($value === null) {
return;
}

if ($value) {
doSomething();
}
}
};

function ($values) {
foreach ($values as $value) {
if ($value === null) {
return;
}

if (!$value) {
continue;
}

doSomething();
doMore();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

function ($values) {
foreach ($values as $value) {
if ($value === null) {
return;
} elseif ($value) {
doSomething();
}
}
};

function ($values) {
foreach ($values as $value) {
if ($value === null) {
return;
} elseif ($value) {
doSomething();
doMore();
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

function ($values) {
foreach ($values as $value) {
if ($value) {
doSomething();
}
}
};

function ($values) {
foreach ($values as $value) {
$value .= 'whatever';

if ($value) {
doSomething();
}
}
};

1 comment on commit 055af01

@colinodell
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for implementing this! I'm looking forward to using this in the next release :)

Please sign in to comment.