Skip to content

Commit a92f244

Browse files
authored
feat: add greater and smaller support to StrlenZeroToIdenticalEmptyStringRector (#7329)
1 parent d64f8d8 commit a92f244

File tree

5 files changed

+85
-36
lines changed

5 files changed

+85
-36
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Fixture;
4+
5+
use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable;
6+
7+
function (string $string, $mixed, Stringable $stringable) {
8+
strlen($string) > 0;
9+
0 < strlen($string);
10+
11+
strlen($mixed) > 0;
12+
0 < strlen($mixed);
13+
14+
strlen($stringable) > 0;
15+
0 < strlen($stringable);
16+
};
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Fixture;
23+
24+
use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable;
25+
26+
function (string $string, $mixed, Stringable $stringable) {
27+
$string !== '';
28+
$string !== '';
29+
30+
(string) $mixed !== '';
31+
(string) $mixed !== '';
32+
33+
(string) $stringable !== '';
34+
(string) $stringable !== '';
35+
};
36+
37+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Fixture;
4+
5+
function (string $string) {
6+
strlen($string) < 0;
7+
0 > strlen($string);
8+
};

rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/stringable_object.php.inc

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,13 @@
22

33
namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Fixture;
44

5-
class Stringable {
6-
private string $string = '';
7-
8-
public function __toString() : string {
9-
return $this->string;
10-
}
11-
}
5+
use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable;
126

137
class StringableObject
148
{
15-
public function run()
9+
public function run(Stringable $value)
1610
{
17-
$value = new Stringable();
18-
1911
$empty = strlen($value) === 0;
20-
2112
$empty = 0 === strlen($value);
2213
}
2314
}
@@ -28,22 +19,13 @@ class StringableObject
2819

2920
namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Fixture;
3021

31-
class Stringable {
32-
private string $string = '';
33-
34-
public function __toString() : string {
35-
return $this->string;
36-
}
37-
}
22+
use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable;
3823

3924
class StringableObject
4025
{
41-
public function run()
26+
public function run(Stringable $value)
4227
{
43-
$value = new Stringable();
44-
4528
$empty = (string) $value === '';
46-
4729
$empty = (string) $value === '';
4830
}
4931
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source;
4+
5+
class Stringable
6+
{
7+
private string $string = '';
8+
9+
public function __toString() : string
10+
{
11+
return $this->string;
12+
}
13+
}

rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\BinaryOp\Greater;
910
use PhpParser\Node\Expr\BinaryOp\Identical;
11+
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
12+
use PhpParser\Node\Expr\BinaryOp\Smaller;
1013
use PhpParser\Node\Expr\FuncCall;
1114
use PhpParser\Node\Scalar\String_;
1215
use Rector\PhpParser\Node\Value\ValueResolver;
@@ -59,27 +62,24 @@ public function run(string $value)
5962
*/
6063
public function getNodeTypes(): array
6164
{
62-
return [Identical::class];
65+
return [Greater::class, Smaller::class, Identical::class];
6366
}
6467

6568
/**
66-
* @param Identical $node
69+
* @param Greater|Smaller|Identical $node
6770
*/
6871
public function refactor(Node $node): ?Node
6972
{
7073
if ($node->left instanceof FuncCall) {
71-
return $this->processIdentical($node->right, $node->left);
72-
}
73-
74-
if ($node->right instanceof FuncCall) {
75-
return $this->processIdentical($node->left, $node->right);
74+
$funcCall = $node->left;
75+
$expr = $node->right;
76+
} elseif ($node->right instanceof FuncCall) {
77+
$expr = $node->left;
78+
$funcCall = $node->right;
79+
} else {
80+
return null;
7681
}
7782

78-
return null;
79-
}
80-
81-
private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical
82-
{
8383
if (! $this->isName($funcCall, 'strlen')) {
8484
return null;
8585
}
@@ -92,6 +92,13 @@ private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical
9292
return null;
9393
}
9494

95+
if (
96+
($node instanceof Greater && $node->right instanceof FuncCall)
97+
|| ($node instanceof Smaller && $node->left instanceof FuncCall)
98+
) {
99+
return null;
100+
}
101+
95102
$variable = $funcCall->getArgs()[0]
96103
->value;
97104

@@ -102,9 +109,11 @@ private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical
102109
->yes();
103110

104111
if (! $isStringType) {
105-
return new Identical(new Expr\Cast\String_($variable), new String_(''));
112+
$variable = new Expr\Cast\String_($variable);
106113
}
107114

108-
return new Identical($variable, new String_(''));
115+
return $node instanceof Identical
116+
? new Identical($variable, new String_(''))
117+
: new NotIdentical($variable, new String_(''));
109118
}
110119
}

0 commit comments

Comments
 (0)