Skip to content

Commit daad186

Browse files
committed
skip zero offset
1 parent d4490af commit daad186

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
/**
6+
* zero offset with no length can't be false
7+
*/
8+
class SkipZeroOffsetWithNoLength
9+
{
10+
public function run(string $name)
11+
{
12+
return substr($name, 0);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
/**
6+
* zero offset with postive length can't be false
7+
*/
8+
class SkipZeroOffsetWithPositiveLength
9+
{
10+
public function run(string $name)
11+
{
12+
return substr($name, 0, 10);
13+
}
14+
}

rules/DowngradePhp80/Rector/FuncCall/DowngradeSubstrFalsyRector.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\DowngradePhp80\Rector\FuncCall;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
89
use PhpParser\Node\Expr;
910
use PhpParser\Node\Expr\BinaryOp\Identical;
1011
use PhpParser\Node\Expr\BooleanNot;
@@ -13,6 +14,7 @@
1314
use PhpParser\Node\Expr\Empty_;
1415
use PhpParser\Node\Expr\FuncCall;
1516
use PhpParser\Node\Expr\Ternary;
17+
use PHPStan\Type\Constant\ConstantIntegerType;
1618
use Rector\PhpParser\Node\Value\ValueResolver;
1719
use Rector\Rector\AbstractRector;
1820
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -94,6 +96,23 @@ public function refactor(Node $node): ?Node
9496
return null;
9597
}
9698

99+
$offset = $node->getArg('offset', 1);
100+
101+
if ($offset instanceof Arg) {
102+
$offsetType = $this->getType($offset->value);
103+
if ($offsetType instanceof ConstantIntegerType && $offsetType->getValue() === 0) {
104+
return null;
105+
}
106+
107+
$length = $node->getArg('length', 2);
108+
if ($length instanceof Arg) {
109+
$lengthType = $this->getType($length->value);
110+
if ($lengthType instanceof ConstantIntegerType && $lengthType->getValue() >= 0) {
111+
return null;
112+
}
113+
}
114+
}
115+
97116
return new String_($node);
98117
}
99118
}

0 commit comments

Comments
 (0)