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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Source\SomeResponse;

final class SameVariableReturnNested
{
public function action()
{
$response = new SomeResponse();

if (mt_rand(0, 1)) {
$response->setBody('... some body content');

return $response;
}

return $response;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Source\SomeResponse;

final class SameVariableReturnNested
{
public function action(): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Source\SomeResponse
{
$response = new SomeResponse();

if (mt_rand(0, 1)) {
$response->setBody('... some body content');

return $response;
}

return $response;
}
}

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

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Source\SomeResponse;

final class SkipTwoVariablesReturnNested
{
public function action()
{
$response = new SomeResponse();
$anotherResponse = new \stdClass();

if (mt_rand(0, 1)) {
$response->setBody('... some body content');

return $response;
}

return $anotherResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,43 @@ public function matchAlwaysReturnVariableNew(ClassMethod|Function_ $functionLike
return null;
}

if (count($returns) !== 1) {
return null;
}
// in case of more returns, we need to check if they all return the same variable

// exact one return of variable
$onlyReturn = $returns[0];
if (! $onlyReturn->expr instanceof Variable) {
return null;
}
$createdVariablesToTypes = $this->resolveCreatedVariablesToTypes($functionLike);

$returnType = $this->nodeTypeResolver->getType($onlyReturn->expr);
$alwaysReturnedClassNames = [];

if (! $returnType instanceof ObjectType) {
return null;
}
foreach ($returns as $return) {
// exact one return of variable
if (! $return->expr instanceof Variable) {
return null;
}

$createdVariablesToTypes = $this->resolveCreatedVariablesToTypes($functionLike);
$returnType = $this->nodeTypeResolver->getType($return->expr);
if (! $returnType instanceof ObjectType) {
return null;
}

$returnedVariableName = $this->nodeNameResolver->getName($return->expr);

$className = $createdVariablesToTypes[$returnedVariableName] ?? null;
if (! is_string($className)) {
return null;
}

$returnedVariableName = $this->nodeNameResolver->getName($onlyReturn->expr);
if ($returnType->getClassName() !== $className) {
return null;
}

$className = $createdVariablesToTypes[$returnedVariableName] ?? null;
if (! is_string($className)) {
return $className;
$alwaysReturnedClassNames[] = $className;
}

if ($returnType->getClassName() === $className) {
return $className;
$uniqueAlwaysReturnedClasses = array_unique($alwaysReturnedClassNames);
if (count($uniqueAlwaysReturnedClasses) !== 1) {
return null;
}

return null;
return $uniqueAlwaysReturnedClasses[0];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function refactor(Node $node): ?Node
}

$returnedNewClassName = $this->strictReturnNewAnalyzer->matchAlwaysReturnVariableNew($node);

if (is_string($returnedNewClassName)) {
$node->returnType = new FullyQualified($returnedNewClassName);

Expand Down