Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
49 changes: 27 additions & 22 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

- [Arguments](#arguments) (6)

- [CodeQuality](#codequality) (77)
- [CodeQuality](#codequality) (78)

- [CodingStyle](#codingstyle) (38)
- [CodingStyle](#codingstyle) (37)

- [Compatibility](#compatibility) (1)

Expand Down Expand Up @@ -528,6 +528,31 @@ Change `array_push()` to direct variable assign

<br>

### CleanupUnneededNullsafeOperatorRector

Cleanup unneeded nullsafe operator

- class: [`Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector`](../rules/CodeQuality/Rector/NullsafeMethodCall/CleanupUnneededNullsafeOperatorRector.php)

```diff
class HelloWorld {
public function getString(): string
{
return 'hello world';
}
}

function get(): HelloWorld
{
return new HelloWorld();
}

-echo get()?->getString();
+echo get()->getString();
```

<br>

### CombineIfRector

Merges nested if statements
Expand Down Expand Up @@ -2357,26 +2382,6 @@ return static function (RectorConfig $rectorConfig): void {

<br>

### RemoveDoubleUnderscoreInMethodNameRector

Non-magic PHP object methods cannot start with "__"

- class: [`Rector\CodingStyle\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector`](../rules/CodingStyle/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector.php)

```diff
class SomeClass
{
- public function __getName($anotherObject)
+ public function getName($anotherObject)
{
- $anotherObject->__getSurname();
+ $anotherObject->getSurname();
}
}
```

<br>

### RemoveFinalFromConstRector

Remove final from constants in classes defined as final
Expand Down
3 changes: 2 additions & 1 deletion config/set/code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector;

use Rector\CodeQuality\Rector\Assign\CombinedAssignRector;
use Rector\CodeQuality\Rector\Assign\SplitListAssignToSeparateLineRector;
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
Expand Down Expand Up @@ -82,6 +81,7 @@
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
use Rector\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector;
use Rector\Config\RectorConfig;
use Rector\Php52\Rector\Property\VarToPublicPropertyRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
Expand Down Expand Up @@ -197,5 +197,6 @@
OptionalParametersAfterRequiredRector::class,
SimplifyEmptyCheckOnEmptyArrayRector::class,
SwitchTrueToIfRector::class,
CleanupUnneededNullsafeOperatorRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector;

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

final class CleanupUnneededNullsafeOperatorRectorTest 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,41 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\Fixture;

class Replace
{
public function getString(): string
{
return 'hello world';
}
}

function get(): Replace
{
return new Replace();
}

echo get()?->getString();

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\Fixture;

class Replace
{
public function getString(): string
{
return 'hello world';
}
}

function get(): Replace
{
return new Replace();
}

echo get()->getString();

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

namespace Rector\Tests\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\Fixture;

class ReplaceOnMethodCall
{
public function getString2(): string
{
return 'hello world';
}

/**
* @return InvalidReturn
*/
public function get2(): ReplaceOnMethodCall
{
return new ReplaceOnMethodCall();
}
}

(new ReplaceOnMethodCall())->get2()?->getString2();

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\Fixture;

class ReplaceOnMethodCall
{
public function getString2(): string
{
return 'hello world';
}

/**
* @return InvalidReturn
*/
public function get2(): ReplaceOnMethodCall
{
return new ReplaceOnMethodCall();
}
}

(new ReplaceOnMethodCall())->get2()->getString2();

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

namespace Rector\Tests\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\Fixture;

class ReplaceWithInvalidDoc
{
public function getString(): string
{
return 'hello world';
}
}

/**
* @return ReplaceWithInvalidDoc|null
*/
function get(): ReplaceWithInvalidDoc
{
return new ReplaceWithInvalidDoc();
}

echo get()?->getString();

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\Fixture;

class ReplaceWithInvalidDoc
{
public function getString(): string
{
return 'hello world';
}
}

/**
* @return ReplaceWithInvalidDoc|null
*/
function get(): ReplaceWithInvalidDoc
{
return new ReplaceWithInvalidDoc();
}

echo get()->getString();

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

namespace Rector\Tests\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\Fixture;

class SkipNullable
{
public function getString(): string
{
return 'hello world';
}
}

function run(): ?SkipNullable
{
return (rand(0, 1)) ? new SkipNullable() : null;
}

echo run()?->getString();

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

declare(strict_types=1);

use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(CleanupUnneededNullsafeOperatorRector::class);
};
Loading