Skip to content

Add Null Coalesce Operator #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exceptions/differences/extensions (:white_check_mark: are the implemented sniffs
- :white_check_mark: Omit phpDoc for parameters/returns with native types, unless adding description
- :white_check_mark: Don't use `@author`, `@since` and similar annotations that duplicate Git information
- :white_check_mark: Assignment in condition is not allowed
- :white_check_mark: Use Null Coalesce Operator `$foo = $bar ?? $baz`

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 @@ -95,6 +95,8 @@
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>
<!-- Require language constructs without parentheses -->
<rule ref="SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses"/>
<!-- Require usage of null coalesce operator when possible -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
<!-- Forbid useless unreachable catch blocks -->
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
<!-- Require using Throwable instead of Exception -->
Expand Down
5 changes: 3 additions & 2 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ FILE ERRORS WARNINGS
tests/input/concatenation_spacing.php 24 0
tests/input/example-class.php 19 0
tests/input/not_spacing.php 7 0
tests/input/null_coalesce_operator.php 3 0
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 94 ERRORS AND 0 WARNINGS WERE FOUND IN 6 FILES
A TOTAL OF 97 ERRORS AND 0 WARNINGS WERE FOUND IN 7 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 83 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 86 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


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

declare(strict_types=1);

$foo = $_GET['foo'] ?? 'foo';

$bar = $bar ?? 'bar';

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

if (isset($foo)) {
$bar = $foo;
} else {
$bar = 'foo';
}

$fooBar = isset($foo, $bar) ? 'foo' : 'bar';

$baz = ! isset($foo) ? 'foo' : 'baz';
19 changes: 19 additions & 0 deletions tests/input/null_coalesce_operator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

$foo = isset($_GET['foo']) ? $_GET['foo'] : 'foo';

$bar = isset($bar) ? $bar : 'bar';

$bar = isset($bar['baz']) ? $bar['baz'] : 'baz';

if (isset($foo)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kukulich This case is already covered? I guess tests are failing here

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope, it's not. It's a lot more complicated than simple ternary operator.

$bar = $foo;
} else {
$bar = 'foo';
}

$fooBar = isset($foo, $bar) ? 'foo' : 'bar';

$baz = ! isset($foo) ? 'foo' : 'baz';