Find the best Rector rule to solve your problem. Searching through 671 rules.
Found 10 rules:
Make implicit nullable param to explicit
-function foo(string $param = null) {}
+function foo(?string $param = null) {}
Changes negate of empty comparison of nullable value to explicit === or !== compare
/** @var stdClass|null $value */
-if ($value) {
+if ($value !== null) {
}
-if (!$value) {
+if ($value === null) {
}
Add null default to properties with PHP 7.4 property nullable type
class SomeClass
{
- public ?string $name;
+ public ?string $name = null;
}
Direct return on if nullable check before return
class SomeClass
{
public function run()
{
- $value = $this->get();
- if (! $value instanceof \stdClass) {
- return null;
- }
-
- return $value;
+ return $this->get();
}
public function get(): ?stdClass {
}
}
Change while null compare to strict instanceof check
final class SomeClass
{
public function run(?SomeClass $someClass)
{
- while ($someClass !== null) {
+ while ($someClass instanceof SomeClass) {
// do something
}
}
}
Change && and || between nullable objects to instanceof compares
function someFunction(?SomeClass $someClass)
{
- if ($someClass && $someClass->someMethod()) {
+ if ($someClass instanceof SomeClass && $someClass->someMethod()) {
return 'yes';
}
return 'no';
}
Change empty() on nullable object to instanceof check
class SomeClass
{
public function run(?AnotherObject $anotherObject)
{
- if (empty($anotherObject)) {
+ if (! $anotherObject instanceof AnotherObject) {
return false;
}
return true;
}
}
Add basic ? nullable type to class methods and functions, as of PHP 7.1
final class SomeClass
{
- public function getData()
+ public function getData(): ?int
{
if (rand(0, 1)) {
return null;
}
return 100;
}
}
Change assertNotEmpty() and assertNotNull() on an object to more clear assertInstanceof()
use PHPUnit\Framework\TestCase;
class SomeClass extends TestCase
{
public function test()
{
$someObject = new stdClass();
- $this->assertNotEmpty($someObject);
+ $this->assertInstanceof(stdClass::class, $someObject);
}
}
Add explicit instance assert between nullable object assign and method call on nullable object (spotted by PHPStan)
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function test()
{
$someObject = $this->getSomeObject();
+ $this->assertInstanceOf(SomeClass::class, $someObject);
$value = $someObject->getSomeMethod();
}
private function getSomeObject(): ?SomeClass
{
if (mt_rand(0, 1)) {
return new SomeClass();
}
return null;
}
}