Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
13c02b8
[DowngradePhp80] Add DowngradeSubstrFalsyRector
samsonasik Oct 13, 2025
6d51910
[DowngradePhp80] Add DowngradeSubstrFalsyRector
samsonasik Oct 13, 2025
12d80c8
[DowngradePhp80] Add DowngradeSubstrFalsyRector
samsonasik Oct 13, 2025
edb3a4f
[DowngradePhp80] Add DowngradeSubstrFalsyRector
samsonasik Oct 13, 2025
d963510
skip negated
samsonasik Oct 13, 2025
e5b2a2e
skip casted bool
samsonasik Oct 13, 2025
121104e
skip compared to false
samsonasik Oct 13, 2025
1535735
[ci-review] Rector Rectify
actions-user Oct 13, 2025
d653bba
skip compared to false
samsonasik Oct 13, 2025
e0d8b56
skip zero offset
samsonasik Oct 13, 2025
4b37a51
skip zero offset
samsonasik Oct 13, 2025
c5feda8
skip negative offset
samsonasik Oct 13, 2025
f1e8eea
skip with concat
samsonasik Oct 13, 2025
0569d22
[ci-review] Rector Rectify
actions-user Oct 13, 2025
a3efa29
skip allow falsy arg
samsonasik Oct 13, 2025
12077fb
use MethodCall and StaticCall as current ReflectionResolver can get
samsonasik Oct 13, 2025
6abd075
skip append
samsonasik Oct 13, 2025
b76a519
skip direct on if cond
samsonasik Oct 15, 2025
0c65912
skip direct on if cond
samsonasik Oct 15, 2025
1ca7c16
skip direct on while and do cond
samsonasik Oct 15, 2025
47562f0
add fixture for skip sprintf arg
samsonasik Oct 15, 2025
4089265
skip as array key
samsonasik Oct 17, 2025
7e86548
skip as array dim fetch dim
samsonasik Oct 17, 2025
0acd09f
skip in binary op
samsonasik Oct 17, 2025
1272b81
skip new arg falsy
samsonasik Oct 17, 2025
2a1de4c
update constant
samsonasik Oct 20, 2025
7180a0a
add failing fixture zero offset, negative length
samsonasik Oct 20, 2025
d722c32
add failing fixture zero offset, negative length
samsonasik Oct 20, 2025
04657b2
fix
samsonasik Oct 20, 2025
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
2 changes: 2 additions & 0 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrContainsRector;
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrEndsWithRector;
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrStartsWithRector;
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector;
Expand Down Expand Up @@ -91,5 +92,6 @@
RemoveReturnTypeDeclarationFromCloneRector::class,
DowngradeEnumToConstantListClassRector::class,
DowngradeInstanceofStringableRector::class,
DowngradeSubstrFalsyRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeSubstrFalsyRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class ByVariableString
{
public function run(string $name)
{
return substr($name, 2);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class ByVariableString
{
public function run(string $name)
{
return (string) substr($name, 2);
}
}

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

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class Fixture
{
public function run()
{
return substr("a", 2);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class Fixture
{
public function run()
{
return (string) substr("a", 2);
}
}

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

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipAlreadyCasted
{
public function run()
{
return (string) substr("a", 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipAppend
{
public function run(string $a)
{
$a .=substr('a', 2);

return $a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipAsArrayDimFetchDim
{
public function run()
{
$data[substr('a', 2)] = 'foo';
var_dump($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipAsArrayKey
{
public function run()
{
$data = [
substr('a', 2) => 'foo'
];

var_dump($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipAsFalsyArg
{
public function run()
{
return $this->execute(substr('a', 2));
}

private function execute(false|string $value)
{
return $value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipCastedBool
{
public function run(string $name)
{
return (bool) substr($name, 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipComparedToFalse
{
public function run(string $name)
{
substr($name, 2) === false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipDifferentFuncCall
{
public function run()
{
return strlen('a');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipDirectCond
{
public function run(string $name)
{
if (substr($name, 2)) {
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

/**
* empty can be false or empty string ""
*/
class SkipEmpty
{
public function run()
{
return empty(substr("a", 2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipInBinaryOp
{
public function run($a)
{
return substr("a", 2) && $a === true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

/**
* negation can be false or empty string ""
*/
class SkipNegated
{
public function run()
{
return ! substr('a', 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

/**
* zero offset with no length can't be false
*/
class SkipNegativeOffset
{
public function run(string $name)
{
return substr($name, -1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipNeverEmpty
{
public function run()
{
return substr("abc", 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipNewArgFalsy
{
public function __construct(false|string $value)
{
}

public function run()
{
new self(substr('a', 2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

/**
* sprintf %s format converts false to empty string
*/
class SkipSprintf
{
public function run(string $name)
{
return sprintf('Value: %s', substr($name, 2));
}

public function run2(string $name)
{
printf('Value: %s', substr($name, 2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

class SkipTernaryFalsy
{
public function run()
{
return substr('a', 2) ?: 'default';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

/**
* falsy concat become empty string
*/
class SkipWithConcat
{
public function run()
{
return substr('a', 2) . substr('b', 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

/**
* zero offset with no length can't be false
*/
class SkipZeroOffsetWithNoLength
{
public function run(string $name)
{
return substr($name, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;

/**
* zero offset with postive length can't be false
*/
class SkipZeroOffsetWithPositiveLength
{
public function run(string $name)
{
return substr($name, 0, 10);
}
}
Loading