Skip to content
Closed
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
29 changes: 27 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 414 Rules Overview
# 415 Rules Overview

<br>

Expand All @@ -8,7 +8,7 @@

- [CodeQuality](#codequality) (78)

- [CodingStyle](#codingstyle) (39)
- [CodingStyle](#codingstyle) (40)

- [Compatibility](#compatibility) (1)

Expand Down Expand Up @@ -1946,6 +1946,31 @@ Type and name of catch exception should match

<br>

### CleanupUnneededNullsafeOperatorRector

Cleanup unneeded nullsafe operator

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

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

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

-echo $this->get()?->getHelloWorld();
+echo $this->get()->getHelloWorld();
```

<br>

### ConsistentImplodeRector

Changes various implode forms to consistent one
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 @@ -83,6 +82,7 @@
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
use Rector\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\CodingStyle\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector;
use Rector\Config\RectorConfig;
use Rector\Php52\Rector\Property\VarToPublicPropertyRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
Expand Down Expand Up @@ -199,5 +199,6 @@
TernaryEmptyArrayArrayDimFetchToCoalesceRector::class,
OptionalParametersAfterRequiredRector::class,
SimplifyEmptyCheckOnEmptyArrayRector::class,
CleanupUnneededNullsafeOperatorRector::class,
]);
};
1 change: 0 additions & 1 deletion config/set/php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector;

use Rector\Arguments\Rector\FuncCall\FunctionArgumentDefaultValueReplacerRector;
use Rector\Arguments\ValueObject\ArgumentAdder;
use Rector\Arguments\ValueObject\ReplaceFuncCallArgumentDefaultValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\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\CodingStyle\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\Fixture;

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

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

echo $this->get()?->getReplace();

?>
-----
<?php

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

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

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

echo $this->get()->getReplace();

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

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

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

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

echo $this->get()?->getReplaceWithInvalidDoc();

?>
-----
<?php

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

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

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

echo $this->get()->getReplaceWithInvalidDoc();

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

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

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

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

echo $this->get()?->getReplace();

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

declare(strict_types=1);

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(CleanupUnneededNullsafeOperatorRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Rector\NullsafeMethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\TypeAnalyzer\ReturnStrictTypeAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://wiki.php.net/rfc/nullsafe_operator
*
* @see \Rector\Tests\CodingStyle\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector\CleanupUnneededNullsafeOperatorRectorTest
*/
final class CleanupUnneededNullsafeOperatorRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ReturnStrictTypeAnalyzer $returnStrictTypeAnalyzer,
)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Cleanup unneeded nullsafe operator',
[
new CodeSample(
<<<'CODE_SAMPLE'
class HelloWorld {
public function getString(): string
{
return 'hello world';
}
}

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

echo $this->get()?->getHelloWorld();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class HelloWorld {
public function getString(): string
{
return 'hello world';
}
}

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

echo $this->get()->getHelloWorld();
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [NullsafeMethodCall::class];
}

/**
* @param NullsafeMethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (!$node->var instanceof MethodCall) {
return null;
}

$returnNode = $this->returnStrictTypeAnalyzer->resolveMethodCallReturnNode($node->var);

if (null === $returnNode) {
return null;
}

// Remove not needed Nullsafe for method call.
$node = $node->var;

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::NULLSAFE_OPERATOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function collectStrictReturnTypes(array $returns): array
return $this->typeNodeUnwrapper->uniquateNodes($returnedStrictTypeNodes);
}

private function resolveMethodCallReturnNode(MethodCall | StaticCall | FuncCall $call): ?Node
public function resolveMethodCallReturnNode(MethodCall | StaticCall | FuncCall $call): ?Node
{
$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($call);
if ($methodReflection === null) {
Expand Down
5 changes: 5 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ final class PhpVersionFeature
*/
public const STATIC_VISIBILITY_SET_STATE = PhpVersion::PHP_80;

/**
* @var int
*/
public const NULLSAFE_OPERATOR = PhpVersion::PHP_80;

/**
* @var int
*/
Expand Down