2022-03-07 11:34:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
2022-03-07 11:34:50 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2022-06-25 20:03:31 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
|
2022-06-25 20:03:31 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\Constant\ConstantArrayType;
|
|
|
|
use PHPStan\Type\Constant\ConstantStringType;
|
|
|
|
use PHPStan\Type\NeverType;
|
|
|
|
use PHPStan\Type\UnionType;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
|
2022-08-09 13:39:17 +00:00
|
|
|
use Rector\Core\Rector\AbstractScopeAwareRector;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\Util\StringUtils;
|
2022-08-29 20:49:28 +00:00
|
|
|
use Rector\PhpDocParser\TypeAnalyzer\ClassMethodReturnTypeResolver;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2022-03-07 11:34:50 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\ArrayShapeFromConstantArrayReturnRectorTest
|
|
|
|
*/
|
2022-08-09 13:39:17 +00:00
|
|
|
final class ArrayShapeFromConstantArrayReturnRector extends AbstractScopeAwareRector
|
2022-03-07 11:34:50 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-08-29 20:49:28 +00:00
|
|
|
* @var \Rector\PhpDocParser\TypeAnalyzer\ClassMethodReturnTypeResolver
|
2022-03-07 11:34:50 +00:00
|
|
|
*/
|
|
|
|
private $classMethodReturnTypeResolver;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-03-07 11:34:50 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
|
|
|
|
*/
|
|
|
|
private $phpDocTypeChanger;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @see https://regex101.com/r/WvUD0m/2
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const SKIPPED_CHAR_REGEX = '#\\W#u';
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ClassMethodReturnTypeResolver $classMethodReturnTypeResolver, PhpDocTypeChanger $phpDocTypeChanger)
|
2022-03-07 11:34:50 +00:00
|
|
|
{
|
|
|
|
$this->classMethodReturnTypeResolver = $classMethodReturnTypeResolver;
|
|
|
|
$this->phpDocTypeChanger = $phpDocTypeChanger;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2022-03-07 11:34:50 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Add array shape exact types based on constant keys of array', [new CodeSample(<<<'CODE_SAMPLE'
|
2022-03-07 11:34:50 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function run(string $name)
|
|
|
|
{
|
|
|
|
return ['name' => $name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return array{name: string}
|
|
|
|
*/
|
|
|
|
public function run(string $name)
|
|
|
|
{
|
|
|
|
return ['name' => $name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class];
|
2022-03-07 11:34:50 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param ClassMethod $node
|
|
|
|
*/
|
2022-08-09 13:39:17 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2022-03-07 11:34:50 +00:00
|
|
|
{
|
2023-02-07 12:26:09 +00:00
|
|
|
if ($this->isInTestCase($scope)) {
|
2022-06-25 20:03:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-03-07 11:34:50 +00:00
|
|
|
/** @var Return_[] $returns */
|
2022-06-07 08:22:29 +00:00
|
|
|
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($node, Return_::class);
|
2022-03-07 11:34:50 +00:00
|
|
|
// exact one shape only
|
|
|
|
if (\count($returns) !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$return = $returns[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$return->expr instanceof Expr) {
|
2022-03-07 11:34:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$returnExprType = $this->getType($return->expr);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$returnExprType instanceof ConstantArrayType) {
|
2022-03-07 11:34:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-11 16:09:13 +00:00
|
|
|
if ($this->shouldSkip($returnExprType)) {
|
2022-03-07 11:34:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-09 13:39:17 +00:00
|
|
|
$returnType = $this->classMethodReturnTypeResolver->resolve($node, $scope);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($returnType instanceof ConstantArrayType) {
|
2022-03-07 11:34:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
2023-06-16 03:34:15 +00:00
|
|
|
$returnExprTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($returnExprType);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($returnExprTypeNode instanceof GenericTypeNode) {
|
2022-03-07 11:34:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($returnExprTypeNode instanceof SpacingAwareArrayTypeNode) {
|
2022-03-07 11:34:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-14 04:39:10 +00:00
|
|
|
$hasChanged = $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $returnExprType);
|
2022-03-07 11:34:50 +00:00
|
|
|
if (!$hasChanged) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkip(ConstantArrayType $constantArrayType) : bool
|
2022-03-26 22:24:44 +00:00
|
|
|
{
|
2022-04-11 16:09:13 +00:00
|
|
|
$keyType = $constantArrayType->getKeyType();
|
2022-03-26 22:24:44 +00:00
|
|
|
// empty array
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($keyType instanceof NeverType) {
|
2022-03-26 22:24:44 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$types = $keyType instanceof UnionType ? $keyType->getTypes() : [$keyType];
|
2022-03-26 22:24:44 +00:00
|
|
|
foreach ($types as $type) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$type instanceof ConstantStringType) {
|
2022-03-26 22:24:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-27 11:19:47 +00:00
|
|
|
$value = $type->getValue();
|
|
|
|
if (\trim($value) === '') {
|
|
|
|
return \true;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (StringUtils::isMatch($value, self::SKIPPED_CHAR_REGEX)) {
|
2022-03-27 11:19:47 +00:00
|
|
|
return \true;
|
2022-03-26 22:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-11 16:09:13 +00:00
|
|
|
$itemType = $constantArrayType->getItemType();
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($itemType instanceof ConstantArrayType) {
|
2022-04-11 16:09:13 +00:00
|
|
|
return $this->shouldSkip($itemType);
|
|
|
|
}
|
2022-03-26 22:24:44 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-25 20:03:31 +00:00
|
|
|
/**
|
|
|
|
* Skip test case, as return methods there are usually with test data only.
|
|
|
|
* Those arrays are hand made and return types are getting complex and messy, so this rule should skip it.
|
|
|
|
*/
|
2023-02-07 12:26:09 +00:00
|
|
|
private function isInTestCase(Scope $scope) : bool
|
2022-06-25 20:03:31 +00:00
|
|
|
{
|
|
|
|
$classReflection = $scope->getClassReflection();
|
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return $classReflection->isSubclassOf('PHPUnit\\Framework\\TestCase');
|
|
|
|
}
|
2022-03-07 11:34:50 +00:00
|
|
|
}
|