Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

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

use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable;

function (string $string, $mixed, Stringable $stringable) {
strlen($string) > 0;
0 < strlen($string);

strlen($mixed) > 0;
0 < strlen($mixed);

strlen($stringable) > 0;
0 < strlen($stringable);
};

?>
-----
<?php

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

use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable;

function (string $string, $mixed, Stringable $stringable) {
$string !== '';
$string !== '';

(string) $mixed !== '';
(string) $mixed !== '';

(string) $stringable !== '';
(string) $stringable !== '';
};

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

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

function (string $string) {
strlen($string) < 0;
0 > strlen($string);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@

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

class Stringable {
private string $string = '';

public function __toString() : string {
return $this->string;
}
}
use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable;

class StringableObject
{
public function run()
public function run(Stringable $value)
{
$value = new Stringable();

$empty = strlen($value) === 0;

$empty = 0 === strlen($value);
}
}
Expand All @@ -28,22 +19,13 @@ class StringableObject

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

class Stringable {
private string $string = '';

public function __toString() : string {
return $this->string;
}
}
use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable;

class StringableObject
{
public function run()
public function run(Stringable $value)
{
$value = new Stringable();

$empty = (string) $value === '';

$empty = (string) $value === '';
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source;

class Stringable
{
private string $string = '';

public function __toString() : string
{
return $this->string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\PhpParser\Node\Value\ValueResolver;
Expand Down Expand Up @@ -59,27 +62,24 @@ public function run(string $value)
*/
public function getNodeTypes(): array
{
return [Identical::class];
return [Greater::class, Smaller::class, Identical::class];
}

/**
* @param Identical $node
* @param Greater|Smaller|Identical $node
*/
public function refactor(Node $node): ?Node
{
if ($node->left instanceof FuncCall) {
return $this->processIdentical($node->right, $node->left);
}

if ($node->right instanceof FuncCall) {
return $this->processIdentical($node->left, $node->right);
$funcCall = $node->left;
$expr = $node->right;
} elseif ($node->right instanceof FuncCall) {
$expr = $node->left;
$funcCall = $node->right;
} else {
return null;
}

return null;
}

private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical
{
if (! $this->isName($funcCall, 'strlen')) {
return null;
}
Expand All @@ -92,6 +92,13 @@ private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical
return null;
}

if (
($node instanceof Greater && $node->right instanceof FuncCall)
|| ($node instanceof Smaller && $node->left instanceof FuncCall)
) {
return null;
}

$variable = $funcCall->getArgs()[0]
->value;

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

if (! $isStringType) {
return new Identical(new Expr\Cast\String_($variable), new String_(''));
$variable = new Expr\Cast\String_($variable);
}

return new Identical($variable, new String_(''));
return $node instanceof Identical
? new Identical($variable, new String_(''))
: new NotIdentical($variable, new String_(''));
}
}
Loading