Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Skip possible void and return by docblock on ReturnTypeFromMockObjectRector #6170

Merged
merged 2 commits into from
Jul 22, 2024
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,17 @@
<?php

declare(strict_types=1);

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

use PHPUnit\Framework\TestCase;

final class SkipPossibleVoid extends TestCase
{
public function test()
{
if (rand(0, 1)) {
return $this->createMock('SomeType');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

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

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class SkipReturnByDocblock extends TestCase
{
public function test()
{
/** @var MockObject $x */
return $x;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Type\Type;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand All @@ -38,7 +39,8 @@ final class ReturnTypeFromMockObjectRector extends AbstractScopeAwareRector impl

public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
private readonly ReturnAnalyzer $returnAnalyzer
) {
}

Expand Down Expand Up @@ -101,12 +103,14 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

$soleReturn = $returns[0];
if (! $soleReturn->expr instanceof Expr) {
if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) {
return null;
}

$returnType = $this->getType($soleReturn->expr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whethere there are real use-cases for getType() anymore, or whether this method should be deprecated in favor of getNativeType...?

Copy link
Member Author

@samsonasik samsonasik Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getType() is stil usefull for:

  • get type from php native function/methods, yes, it got mixed on getNativeType()
  • downgrade process
  • get object type from method call / static call
  • verify transformation that too much for only native check, eg,
/** @var string $var */
trim($var)

that cast (string) is not needed on that case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, said differently: its only useful in very few edge cases but the name misleads even rector-src core developers to use it wrongly in lots of situations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given rare context, this is used on purpose here 👍

/** @var Expr $expr */
$expr = $returns[0]->expr;
$returnType = $this->nodeTypeResolver->getNativeType($expr);

if (! $this->isMockObjectType($returnType)) {
return null;
}
Expand Down