diff --git a/README.md b/README.md index a4abea25..ef5b8e6c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/Doctrine/ruleset.xml b/lib/Doctrine/ruleset.xml index 8eb95eec..6e84914b 100644 --- a/lib/Doctrine/ruleset.xml +++ b/lib/Doctrine/ruleset.xml @@ -132,6 +132,8 @@ + + diff --git a/tests/expected_report.txt b/tests/expected_report.txt index 8dea920c..bb5ef74f 100644 --- a/tests/expected_report.txt +++ b/tests/expected_report.txt @@ -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 @@ -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 ---------------------------------------------------------------------- diff --git a/tests/fixed/EarlyReturn.php b/tests/fixed/EarlyReturn.php new file mode 100644 index 00000000..da856545 --- /dev/null +++ b/tests/fixed/EarlyReturn.php @@ -0,0 +1,52 @@ +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; + } +} diff --git a/tests/input/EarlyReturn.php b/tests/input/EarlyReturn.php new file mode 100644 index 00000000..943a53c5 --- /dev/null +++ b/tests/input/EarlyReturn.php @@ -0,0 +1,52 @@ +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; + } +}