2022-07-06 11:56:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\TypeDeclaration\Rector\Property;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2022-11-29 20:22:22 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2024-02-22 08:46:08 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
|
2024-02-25 00:28:21 +00:00
|
|
|
use PHPStan\Type\ObjectType;
|
2024-02-22 08:46:08 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
|
|
|
|
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
2022-07-06 11:56:20 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2023-09-29 12:48:57 +00:00
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2022-07-06 11:56:20 +00:00
|
|
|
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\ValueObject\MethodName;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-07-06 11:56:20 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector\TypedPropertyFromStrictSetUpRectorTest
|
|
|
|
*/
|
|
|
|
final class TypedPropertyFromStrictSetUpRector extends AbstractRector implements MinPhpVersionInterface
|
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-07-06 11:56:20 +00:00
|
|
|
* @var \Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer
|
|
|
|
*/
|
|
|
|
private $trustedClassMethodPropertyTypeInferer;
|
2023-09-29 12:48:57 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
2024-02-22 08:46:08 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
|
|
|
|
*/
|
|
|
|
private $docBlockUpdater;
|
|
|
|
public function __construct(TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, StaticTypeMapper $staticTypeMapper, PhpDocInfoFactory $phpDocInfoFactory, DocBlockUpdater $docBlockUpdater)
|
2022-07-06 11:56:20 +00:00
|
|
|
{
|
|
|
|
$this->trustedClassMethodPropertyTypeInferer = $trustedClassMethodPropertyTypeInferer;
|
2023-09-29 12:48:57 +00:00
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
2024-02-22 08:46:08 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
|
|
|
$this->docBlockUpdater = $docBlockUpdater;
|
2022-07-06 11:56:20 +00:00
|
|
|
}
|
|
|
|
public function getRuleDefinition() : RuleDefinition
|
|
|
|
{
|
|
|
|
return new RuleDefinition('Add strict typed property based on setUp() strict typed assigns in TestCase', [new CodeSample(<<<'CODE_SAMPLE'
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
final class SomeClass extends TestCase
|
|
|
|
{
|
|
|
|
private $value;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->value = 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
final class SomeClass extends TestCase
|
|
|
|
{
|
|
|
|
private int $value;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->value = 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-11-29 20:22:22 +00:00
|
|
|
return [Class_::class];
|
2022-07-06 11:56:20 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-11-29 20:22:22 +00:00
|
|
|
* @param Class_ $node
|
2022-07-06 11:56:20 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node) : ?Node
|
|
|
|
{
|
2022-11-29 20:22:22 +00:00
|
|
|
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
|
|
|
|
if (!$setUpClassMethod instanceof ClassMethod) {
|
2022-07-06 11:56:20 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-11-29 20:22:22 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->getProperties() as $property) {
|
|
|
|
// type is already set
|
|
|
|
if ($property->type !== null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// is not private? we cannot be sure about other usage
|
|
|
|
if (!$property->isPrivate()) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-12 21:57:31 +00:00
|
|
|
$propertyType = $this->trustedClassMethodPropertyTypeInferer->inferProperty($node, $property, $setUpClassMethod);
|
2022-11-29 20:22:22 +00:00
|
|
|
$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY);
|
|
|
|
if (!$propertyTypeNode instanceof Node) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-22 08:46:08 +00:00
|
|
|
if ($propertyType instanceof ObjectType && $propertyType->getClassName() === 'PHPUnit\\Framework\\MockObject\\MockObject') {
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
|
|
|
$varTag = $phpDocInfo->getVarTagValueNode();
|
|
|
|
$varType = $phpDocInfo->getVarType();
|
|
|
|
if ($varTag instanceof VarTagValueNode && $varType instanceof ObjectType && $varType->getClassName() !== 'PHPUnit\\Framework\\MockObject\\MockObject') {
|
|
|
|
$varTag->type = new IntersectionTypeNode([new FullyQualifiedIdentifierTypeNode($propertyType->getClassName()), new FullyQualifiedIdentifierTypeNode($varType->getClassName())]);
|
|
|
|
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
|
|
|
|
}
|
|
|
|
}
|
2022-11-29 20:22:22 +00:00
|
|
|
$property->type = $propertyTypeNode;
|
|
|
|
$hasChanged = \true;
|
2022-07-06 11:56:20 +00:00
|
|
|
}
|
2022-11-29 20:22:22 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2022-07-06 11:56:20 +00:00
|
|
|
}
|
2022-11-29 20:22:22 +00:00
|
|
|
return null;
|
2022-07-06 11:56:20 +00:00
|
|
|
}
|
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
|
|
|
return PhpVersionFeature::TYPED_PROPERTIES;
|
|
|
|
}
|
|
|
|
}
|