Skip to content

Commit fbcb5d4

Browse files
committed
[DowngradePhp80] Add DowngradeSubstrFalsyRector
1 parent 388ab1f commit fbcb5d4

File tree

11 files changed

+226
-0
lines changed

11 files changed

+226
-0
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: 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 SkipDifferentFuncCall
6+
{
7+
public function run()
8+
{
9+
return strlen('a');
10+
}
11+
}
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+
* empty can be false or empty string ""
7+
*/
8+
class SkipEmpty
9+
{
10+
public function run()
11+
{
12+
return empty(substr("a", 2));
13+
}
14+
}
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 SkipNeverEmpty
6+
{
7+
public function run()
8+
{
9+
return substr("abc", 2);
10+
}
11+
}
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 SkipTernaryFalsy
6+
{
7+
public function run()
8+
{
9+
return substr('a', 2) ?: 'default';
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeSubstrFalsyRector::class);
10+
};

0 commit comments

Comments
 (0)