2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\Rector\ClassConst;
|
2019-05-26 10:40:35 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\ClassConst;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
|
|
|
|
use PHPStan\Type\ArrayType;
|
|
|
|
use PHPStan\Type\Constant\ConstantArrayType;
|
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
|
|
|
use Rector\Privatization\TypeManipulator\TypeNormalizer;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\ClassConst\VarConstantCommentRector\VarConstantCommentRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class VarConstantCommentRector extends AbstractRector
|
2019-05-26 10:40:35 +00:00
|
|
|
{
|
2021-01-10 18:57:16 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
2021-01-10 18:57:16 +00:00
|
|
|
*/
|
|
|
|
private $typeComparator;
|
2021-01-18 20:06:02 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
|
2021-01-18 20:06:02 +00:00
|
|
|
*/
|
|
|
|
private $phpDocTypeChanger;
|
2021-12-30 16:07:15 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Privatization\TypeManipulator\TypeNormalizer
|
|
|
|
*/
|
|
|
|
private $typeNormalizer;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(TypeComparator $typeComparator, PhpDocTypeChanger $phpDocTypeChanger, TypeNormalizer $typeNormalizer)
|
2021-01-10 18:57:16 +00:00
|
|
|
{
|
|
|
|
$this->typeComparator = $typeComparator;
|
2021-01-18 20:06:02 +00:00
|
|
|
$this->phpDocTypeChanger = $phpDocTypeChanger;
|
2021-12-30 16:07:15 +00:00
|
|
|
$this->typeNormalizer = $typeNormalizer;
|
2021-01-10 18:57:16 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-05-26 10:40:35 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Constant should have a @var comment with type', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-05-26 10:40:35 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
const HI = 'hi';
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-05-26 10:40:35 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const HI = 'hi';
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-05-26 10:40:35 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-05-26 10:40:35 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-05-26 10:40:35 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassConst::class];
|
2019-05-26 10:40:35 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassConst $node
|
2019-05-26 10:40:35 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-05-26 10:40:35 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($node->consts) > 1) {
|
2019-05-26 10:40:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-07 17:46:41 +00:00
|
|
|
$constType = $this->getType($node->consts[0]->value);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($constType instanceof MixedType) {
|
2019-05-26 10:40:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-12-30 16:07:15 +00:00
|
|
|
// generalize false/true type to bool, as mostly default value but accepts both
|
|
|
|
$constType = $this->typeNormalizer->generalizeConstantBoolTypes($constType);
|
2021-01-18 20:06:02 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
2021-03-08 19:36:51 +00:00
|
|
|
if ($this->shouldSkipConstantArrayType($constType, $phpDocInfo)) {
|
|
|
|
return null;
|
2020-04-15 06:52:53 +00:00
|
|
|
}
|
2021-01-19 23:29:52 +00:00
|
|
|
if ($this->typeComparator->isSubtype($constType, $phpDocInfo->getVarType())) {
|
2021-01-10 18:57:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-01-18 20:06:02 +00:00
|
|
|
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $constType);
|
2022-02-28 06:43:27 +00:00
|
|
|
if (!$phpDocInfo->hasChanged()) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-01 20:04:49 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function hasTwoAndMoreGenericClassStringTypes(ConstantArrayType $constantArrayType) : bool
|
2021-02-21 00:05:22 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($constantArrayType, TypeKind::RETURN);
|
|
|
|
if (!$typeNode instanceof ArrayTypeNode) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-02-21 00:05:22 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$typeNode->type instanceof UnionTypeNode) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-02-21 00:05:22 +00:00
|
|
|
}
|
|
|
|
$genericTypeNodeCount = 0;
|
|
|
|
foreach ($typeNode->type->types as $unionedTypeNode) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($unionedTypeNode instanceof GenericTypeNode) {
|
2021-02-21 00:05:22 +00:00
|
|
|
++$genericTypeNodeCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $genericTypeNodeCount > 1;
|
|
|
|
}
|
2021-03-08 19:36:51 +00:00
|
|
|
/**
|
|
|
|
* Skip big arrays and mixed[] constants
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipConstantArrayType(Type $constType, PhpDocInfo $phpDocInfo) : bool
|
2021-03-08 19:36:51 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$constType instanceof ConstantArrayType) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-03-08 19:36:51 +00:00
|
|
|
}
|
|
|
|
$currentVarType = $phpDocInfo->getVarType();
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($currentVarType instanceof ArrayType && $currentVarType->getItemType() instanceof MixedType) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-03-08 19:36:51 +00:00
|
|
|
}
|
|
|
|
if ($this->hasTwoAndMoreGenericClassStringTypes($constType)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-03-08 19:36:51 +00:00
|
|
|
}
|
2021-03-08 19:50:03 +00:00
|
|
|
return $this->isHugeNestedConstantArrayTyp($constType);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isHugeNestedConstantArrayTyp(ConstantArrayType $constantArrayType) : bool
|
2021-03-08 19:50:03 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($constantArrayType->getValueTypes()) <= 3) {
|
|
|
|
return \false;
|
2021-03-08 19:50:03 +00:00
|
|
|
}
|
|
|
|
foreach ($constantArrayType->getValueTypes() as $constValueType) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($constValueType instanceof ConstantArrayType) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-03-08 19:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-03-08 19:36:51 +00:00
|
|
|
}
|
2019-05-26 10:40:35 +00:00
|
|
|
}
|