Skip to content

Commit 2c4aa18

Browse files
[DowngradePhp80] Add DowngradeSubstrFalsyRector (#339)
* [DowngradePhp80] Add DowngradeSubstrFalsyRector * [DowngradePhp80] Add DowngradeSubstrFalsyRector * [DowngradePhp80] Add DowngradeSubstrFalsyRector * [DowngradePhp80] Add DowngradeSubstrFalsyRector * skip negated * skip casted bool * skip compared to false * [ci-review] Rector Rectify * skip compared to false * skip zero offset * skip zero offset * skip negative offset * skip with concat * [ci-review] Rector Rectify * skip allow falsy arg * use MethodCall and StaticCall as current ReflectionResolver can get * skip append * skip direct on if cond * skip direct on if cond * skip direct on while and do cond * add fixture for skip sprintf arg * skip as array key * skip as array dim fetch dim * skip in binary op * skip new arg falsy * update constant * add failing fixture zero offset, negative length * add failing fixture zero offset, negative length * fix --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 23a17d4 commit 2c4aa18

28 files changed

+610
-2
lines changed

config/set/downgrade-php80.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrContainsRector;
2424
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrEndsWithRector;
2525
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrStartsWithRector;
26+
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector;
2627
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector;
2728
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector;
2829
use Rector\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector;
@@ -91,5 +92,6 @@
9192
RemoveReturnTypeDeclarationFromCloneRector::class,
9293
DowngradeEnumToConstantListClassRector::class,
9394
DowngradeInstanceofStringableRector::class,
95+
DowngradeSubstrFalsyRector::class,
9496
]);
9597
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeSubstrFalsyRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class ByVariableString
6+
{
7+
public function run(string $name)
8+
{
9+
return substr($name, 2);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
18+
19+
class ByVariableString
20+
{
21+
public function run(string $name)
22+
{
23+
return (string) substr($name, 2);
24+
}
25+
}
26+
27+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class Fixture
6+
{
7+
public function run()
8+
{
9+
return substr("a", 2);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
18+
19+
class Fixture
20+
{
21+
public function run()
22+
{
23+
return (string) substr("a", 2);
24+
}
25+
}
26+
27+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class SkipAlreadyCasted
6+
{
7+
public function run()
8+
{
9+
return (string) substr("a", 2);
10+
}
11+
}
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\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class SkipAppend
6+
{
7+
public function run(string $a)
8+
{
9+
$a .=substr('a', 2);
10+
11+
return $a;
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class SkipAsArrayDimFetchDim
6+
{
7+
public function run()
8+
{
9+
$data[substr('a', 2)] = 'foo';
10+
var_dump($data);
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class SkipAsArrayKey
6+
{
7+
public function run()
8+
{
9+
$data = [
10+
substr('a', 2) => 'foo'
11+
];
12+
13+
var_dump($data);
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class SkipAsFalsyArg
6+
{
7+
public function run()
8+
{
9+
return $this->execute(substr('a', 2));
10+
}
11+
12+
private function execute(false|string $value)
13+
{
14+
return $value;
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class SkipCastedBool
6+
{
7+
public function run(string $name)
8+
{
9+
return (bool) substr($name, 2);
10+
}
11+
}

0 commit comments

Comments
 (0)