Skip to content

Commit

Permalink
[Types] Make AddVoidReturnTypeWhereNoReturnRector work only with type…
Browse files Browse the repository at this point in the history
…-declaration, as reliable (#4720)
  • Loading branch information
TomasVotruba committed Aug 8, 2023
1 parent 3de7c69 commit 0a8ffe3
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 163 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,37 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\NeverType;
use PHPStan\Type\VoidType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnVendorLockResolver;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector\AddVoidReturnTypeWhereNoReturnRectorTest
*/
final class AddVoidReturnTypeWhereNoReturnRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface
final class AddVoidReturnTypeWhereNoReturnRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @api
* @var string using phpdoc instead of a native void type can ease the migration path for consumers of code being processed.
*/
public const USE_PHPDOC = 'use_phpdoc';

private bool $usePhpdoc = false;

public function __construct(
private readonly SilentVoidResolver $silentVoidResolver,
private readonly ClassMethodReturnVendorLockResolver $classMethodReturnVendorLockResolver,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly ReflectionResolver $reflectionResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add return type void to function like without any return', [
new ConfiguredCodeSample(
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
Expand All @@ -71,10 +63,6 @@ public function getValues(): void
}
}
CODE_SAMPLE
,
[
self::USE_PHPDOC => false,
]
),
]);
}
Expand Down Expand Up @@ -104,15 +92,6 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->usePhpdoc) {
$hasChanged = $this->changePhpDocToVoidIfNotNever($node);
if ($hasChanged) {
return $node;
}

return null;
}

if ($node instanceof ClassMethod && $this->classMethodReturnVendorLockResolver->isVendorLocked($node)) {
return null;
}
Expand All @@ -126,28 +105,6 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::VOID_TYPE;
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
$usePhpdoc = $configuration[self::USE_PHPDOC] ?? (bool) current($configuration);
Assert::boolean($usePhpdoc);

$this->usePhpdoc = $usePhpdoc;
}

private function changePhpDocToVoidIfNotNever(ClassMethod|Function_|Closure $node): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

if ($phpDocInfo->getReturnType() instanceof NeverType) {
return false;
}

return $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, new VoidType());
}

private function shouldSkipClassMethod(ClassMethod|Function_|Closure $functionLike): bool
{
if (! $functionLike instanceof ClassMethod) {
Expand Down

0 comments on commit 0a8ffe3

Please sign in to comment.