Skip to content

Commit

Permalink
Merge pull request #22 from carusogabriel/early-returns
Browse files Browse the repository at this point in the history
Early return sniff
  • Loading branch information
Ocramius committed Feb 6, 2018
2 parents c813466 + 79ea474 commit 2f3a075
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ exceptions/differences/extensions (:white_check_mark: are the implemented sniffs
- :white_check_mark: Assignment in condition is not allowed
- :white_check_mark: Use parentheses when creating new instances that do not require arguments `$foo = new Foo()`
- :white_check_mark: Use Null Coalesce Operator `$foo = $bar ?? $baz`
- :white_check_mark: Use early return

For full reference of enforcements, go through `lib/Doctrine/ruleset.xml` where each sniff is briefly described.

Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
<!-- Forbid weak comparisons -->
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>
<!-- Require usage of early exit -->
<rule ref="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
<!-- Require language constructs without parentheses -->
<rule ref="SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses"/>
<!-- Require new instances with parentheses -->
Expand Down
5 changes: 3 additions & 2 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PHP CODE SNIFFER REPORT SUMMARY
FILE ERRORS WARNINGS
----------------------------------------------------------------------
tests/input/concatenation_spacing.php 24 0
tests/input/EarlyReturn.php 4 0
tests/input/example-class.php 19 0
tests/input/forbidden-comments.php 4 0
tests/input/forbidden-functions.php 3 0
Expand All @@ -14,9 +15,9 @@ tests/input/return_type_on_closures.php 21 0
tests/input/return_type_on_methods.php 17 0
tests/input/test-case.php 6 0
----------------------------------------------------------------------
A TOTAL OF 121 ERRORS AND 0 WARNINGS WERE FOUND IN 10 FILES
A TOTAL OF 125 ERRORS AND 0 WARNINGS WERE FOUND IN 11 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 106 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 110 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


52 changes: 52 additions & 0 deletions tests/fixed/EarlyReturn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Example;

class EarlyReturn
{
public function bar() : bool
{
if ($bar === 'bar') {
return true;
}

return false;
}

public function foo() : ?string
{
foreach ($itens as $item) {
if (! $item->isItem()) {
return 'There is an item that is not an item';
}

continue;
}

return null;
}

public function baz() : string
{
if ($number > 0) {
return 'Number is grater then 0';
}

exit;
}

public function quoox() : bool
{
if (true !== 'true') {
return false;
}

if (false === false) {
return true;
}

return true;
}
}
52 changes: 52 additions & 0 deletions tests/input/EarlyReturn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Example;

class EarlyReturn
{
public function bar() : bool
{
if ($bar === 'bar') {
return true;
} else {
return false;
}
}

public function foo() : ?string
{
foreach ($itens as $item) {
if (! $item->isItem()) {
return 'There is an item that is not an item';
} else {
continue;
}
}

return null;
}

public function baz() : string
{
if ($number > 0) {
return 'Number is grater then 0';
} else {
exit;
}
}

public function quoox() : bool
{
if (true === 'true') {
if (false === false) {
return true;
}
} else {
return false;
}

return true;
}
}

0 comments on commit 2f3a075

Please sign in to comment.