Skip to content

Commit

Permalink
[Strict] Fix DisallowedEmptyRuleFixerRector empty() allow string '0' …
Browse files Browse the repository at this point in the history
…check (#5296)

* DisallowedEmptyRuleFixerRector: fix test expectations

* fix

* fix

* [ci-review] Rector Rectify

* more fixture

* also negation

* run

* run

---------

Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
3 people committed Nov 28, 2023
1 parent 765b636 commit c384032
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class UnionWithNull

public function run()
{
if ($this->value === null || $this->value === '') {
if ($this->value === null || $this->value === '' || $this->value === '0') {
return 'empty';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class UnionWithNullFromParam
{
public function run(string|null $value)
{
if ($value === null || $value === '') {
if ($value === null || $value === '' || $value === '0') {
return 'empty';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class EmptyStringNullable
{
public function run(string|null $value)
{
return $value === null || $value === '';
return $value === null || $value === '' || $value === '0';
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;

final class NegationEmptyStringNullable
{
public function run(string|null $value)
{
return ! empty($value);
}
}

?>
-----
<?php

namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;

final class NegationEmptyStringNullable
{
public function run(string|null $value)
{
return $value !== null && $value !== '' && $value !== '0';
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class NullableString
{
public function run(string|null $value)
{
return $value !== null && $value !== '' ? 1 : 2;
return $value !== null && $value !== '' && $value !== '0' ? 1 : 2;
}
}

Expand Down
16 changes: 16 additions & 0 deletions rules/Strict/NodeFactory/ExactCompareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ private function createFromUnionType(
return null;
}

if ($treatAsNotEmpty) {
return new BooleanAnd($toNullNotIdentical, $compareExpr);
}

if ($unionType->isString()->yes()) {
$booleanAnd = new BooleanAnd($toNullNotIdentical, $compareExpr);

return new BooleanAnd($booleanAnd, new NotIdentical($expr, new String_('0')));
}

return new BooleanAnd($toNullNotIdentical, $compareExpr);
}

Expand Down Expand Up @@ -227,6 +237,12 @@ private function createTruthyFromUnionType(
return null;
}

if ($unionType->isString()->yes()) {
$booleanOr = new BooleanOr($toNullIdentical, $scalarFalsyIdentical);

return new BooleanOr($booleanOr, new Identical($expr, new String_('0')));
}

return new BooleanOr($toNullIdentical, $scalarFalsyIdentical);
}
}
2 changes: 1 addition & 1 deletion tests/Issues/SimplifyEmpty/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Fixture {

public function check(): bool
{
return $this->getString() !== null && $this->getString() !== '' && !(($this->getString2() === null || $this->getString2() === '') && !is_numeric($this->getString2()));
return $this->getString() !== null && $this->getString() !== '' && $this->getString() !== '0' && !(($this->getString2() === null || $this->getString2() === '' || $this->getString2() === '0') && !is_numeric($this->getString2()));
}
}

Expand Down

0 comments on commit c384032

Please sign in to comment.