Skip to content

Commit 2f3a075

Browse files
authored
Merge pull request #22 from carusogabriel/early-returns
Early return sniff
2 parents c813466 + 79ea474 commit 2f3a075

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ exceptions/differences/extensions (:white_check_mark: are the implemented sniffs
3333
- :white_check_mark: Assignment in condition is not allowed
3434
- :white_check_mark: Use parentheses when creating new instances that do not require arguments `$foo = new Foo()`
3535
- :white_check_mark: Use Null Coalesce Operator `$foo = $bar ?? $baz`
36+
- :white_check_mark: Use early return
3637

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

lib/Doctrine/ruleset.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@
132132
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
133133
<!-- Forbid weak comparisons -->
134134
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>
135+
<!-- Require usage of early exit -->
136+
<rule ref="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
135137
<!-- Require language constructs without parentheses -->
136138
<rule ref="SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses"/>
137139
<!-- Require new instances with parentheses -->

tests/expected_report.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PHP CODE SNIFFER REPORT SUMMARY
44
FILE ERRORS WARNINGS
55
----------------------------------------------------------------------
66
tests/input/concatenation_spacing.php 24 0
7+
tests/input/EarlyReturn.php 4 0
78
tests/input/example-class.php 19 0
89
tests/input/forbidden-comments.php 4 0
910
tests/input/forbidden-functions.php 3 0
@@ -14,9 +15,9 @@ tests/input/return_type_on_closures.php 21 0
1415
tests/input/return_type_on_methods.php 17 0
1516
tests/input/test-case.php 6 0
1617
----------------------------------------------------------------------
17-
A TOTAL OF 121 ERRORS AND 0 WARNINGS WERE FOUND IN 10 FILES
18+
A TOTAL OF 125 ERRORS AND 0 WARNINGS WERE FOUND IN 11 FILES
1819
----------------------------------------------------------------------
19-
PHPCBF CAN FIX 106 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
20+
PHPCBF CAN FIX 110 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
2021
----------------------------------------------------------------------
2122

2223

tests/fixed/EarlyReturn.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Example;
6+
7+
class EarlyReturn
8+
{
9+
public function bar() : bool
10+
{
11+
if ($bar === 'bar') {
12+
return true;
13+
}
14+
15+
return false;
16+
}
17+
18+
public function foo() : ?string
19+
{
20+
foreach ($itens as $item) {
21+
if (! $item->isItem()) {
22+
return 'There is an item that is not an item';
23+
}
24+
25+
continue;
26+
}
27+
28+
return null;
29+
}
30+
31+
public function baz() : string
32+
{
33+
if ($number > 0) {
34+
return 'Number is grater then 0';
35+
}
36+
37+
exit;
38+
}
39+
40+
public function quoox() : bool
41+
{
42+
if (true !== 'true') {
43+
return false;
44+
}
45+
46+
if (false === false) {
47+
return true;
48+
}
49+
50+
return true;
51+
}
52+
}

tests/input/EarlyReturn.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Example;
6+
7+
class EarlyReturn
8+
{
9+
public function bar() : bool
10+
{
11+
if ($bar === 'bar') {
12+
return true;
13+
} else {
14+
return false;
15+
}
16+
}
17+
18+
public function foo() : ?string
19+
{
20+
foreach ($itens as $item) {
21+
if (! $item->isItem()) {
22+
return 'There is an item that is not an item';
23+
} else {
24+
continue;
25+
}
26+
}
27+
28+
return null;
29+
}
30+
31+
public function baz() : string
32+
{
33+
if ($number > 0) {
34+
return 'Number is grater then 0';
35+
} else {
36+
exit;
37+
}
38+
}
39+
40+
public function quoox() : bool
41+
{
42+
if (true === 'true') {
43+
if (false === false) {
44+
return true;
45+
}
46+
} else {
47+
return false;
48+
}
49+
50+
return true;
51+
}
52+
}

0 commit comments

Comments
 (0)