rector/rules/TypeDeclaration/Rector/ClassMethod/AddArrayReturnDocTypeRector.php
2023-02-02 09:24:12 +00:00

80 lines
2.0 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use Rector\Core\Contract\Rector\DeprecatedRectorInterface;
use Rector\Core\Rector\AbstractScopeAwareRector;
use RectorPrefix202302\Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @deprecated Use specific rules working with strict type declarations instead of this docs blocks non-reliable one
*/
final class AddArrayReturnDocTypeRector extends AbstractScopeAwareRector implements DeprecatedRectorInterface
{
/**
* @readonly
* @var \Symfony\Component\Console\Style\SymfonyStyle
*/
private $symfonyStyle;
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Adds @return annotation to array parameters inferred from the rest of the code', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var int[]
*/
private $values;
public function getValues(): array
{
return $this->values;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var int[]
*/
private $values;
/**
* @return int[]
*/
public function getValues(): array
{
return $this->values;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
$this->symfonyStyle->error('The AddArrayReturnDocTypeRector rule is deprecated, as it works with doc block types that are not reliable and might infer incorrect types');
\sleep(5);
return null;
}
}