Skip to content

Commit

Permalink
Merge pull request #255 from simPod/null-safe
Browse files Browse the repository at this point in the history
Require Null Safe Object operator
  • Loading branch information
greg0ire committed Jul 20, 2021
2 parents 88be266 + bc9bf7e commit f4079ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/Doctrine/ruleset.xml
Expand Up @@ -266,6 +266,8 @@
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator"/>
<!-- Require usage of null coalesce operator when possible -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
<!-- Require usage of null safe operator when possible -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator"/>
<!-- Forbid usage of conditions when a simple return can be used -->
<rule ref="SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn"/>
<!-- Forbid usage of boolean-only ternary operator usage (e.g. $foo ? true : false) -->
Expand Down
5 changes: 5 additions & 0 deletions tests/fixed/null_safe_operator.php
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

$var = $object === null ? null : $object->property;
5 changes: 5 additions & 0 deletions tests/input/null_safe_operator.php
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

$var = $object === null ? null : $object->property;
22 changes: 17 additions & 5 deletions tests/php80-compatibility.patch
@@ -1,8 +1,8 @@
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
index c644926..1b5b271 100644
index c644926..d0f0a44 100644
--- a/tests/expected_report.txt
+++ b/tests/expected_report.txt
@@ -15,17 +15,17 @@ tests/input/ControlStructures.php 28 0
@@ -15,18 +15,19 @@ tests/input/ControlStructures.php 28 0
tests/input/doc-comment-spacing.php 11 0
tests/input/duplicate-assignment-variable.php 1 0
tests/input/EarlyReturn.php 6 0
Expand All @@ -22,9 +22,11 @@ index c644926..1b5b271 100644
-tests/input/null_coalesce_equal_operator.php 1 0
+tests/input/null_coalesce_equal_operator.php 5 0
tests/input/null_coalesce_operator.php 3 0
+tests/input/null_safe_operator.php 1 0
tests/input/optimized-functions.php 1 0
tests/input/PropertyTypeHintSpacing.php 6 0
@@ -39,15 +39,15 @@ tests/input/superfluous-naming.php 11 0
tests/input/return_type_on_closures.php 21 0
@@ -39,15 +40,15 @@ tests/input/superfluous-naming.php 11 0
tests/input/test-case.php 8 0
tests/input/trailing_comma_on_array.php 1 0
tests/input/traits-uses.php 11 0
Expand All @@ -36,10 +38,10 @@ index c644926..1b5b271 100644
tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
-A TOTAL OF 377 ERRORS AND 0 WARNINGS WERE FOUND IN 41 FILES
+A TOTAL OF 390 ERRORS AND 0 WARNINGS WERE FOUND IN 41 FILES
+A TOTAL OF 391 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 313 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 326 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 327 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


Expand Down Expand Up @@ -138,6 +140,16 @@ index 8846dd1..51c361c 100644

$bar = $bar['baz'] ?? 'baz';

diff --git a/tests/fixed/null_safe_operator.php b/tests/fixed/null_safe_operator.php
index 5bbb636..7ce8a3d 100644
--- a/tests/fixed/null_safe_operator.php
+++ b/tests/fixed/null_safe_operator.php
@@ -2,4 +2,4 @@

declare(strict_types=1);

-$var = $object === null ? null : $object->property;
+$var = $object?->property;
diff --git a/tests/fixed/type-hints.php b/tests/fixed/type-hints.php
index 0e952fc..9824fb0 100644
--- a/tests/fixed/type-hints.php
Expand Down

0 comments on commit f4079ae

Please sign in to comment.