Skip to content

Commit

Permalink
skip so many types (#5802)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Mar 8, 2021
1 parent 80c4fc8 commit 8a0d6d9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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;
Expand Down Expand Up @@ -89,16 +91,8 @@ public function refactor(Node $node): ?Node

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

// skip big arrays and mixed[] constants
if ($constType instanceof ConstantArrayType) {
$currentVarType = $phpDocInfo->getVarType();
if ($currentVarType instanceof ArrayType && $currentVarType->getItemType() instanceof MixedType) {
return null;
}

if ($this->hasTwoAndMoreGenericClassStringTypes($constType)) {
return null;
}
if ($this->shouldSkipConstantArrayType($constType, $phpDocInfo)) {
return null;
}

if ($this->typeComparator->isSubtype($constType, $phpDocInfo->getVarType())) {
Expand Down Expand Up @@ -130,4 +124,33 @@ private function hasTwoAndMoreGenericClassStringTypes(ConstantArrayType $constan

return $genericTypeNodeCount > 1;
}

/**
* Skip big arrays and mixed[] constants
*/
private function shouldSkipConstantArrayType(Type $constType, PhpDocInfo $phpDocInfo): bool
{
if (! $constType instanceof ConstantArrayType) {
return false;
}

$currentVarType = $phpDocInfo->getVarType();
if ($currentVarType instanceof ArrayType && $currentVarType->getItemType() instanceof MixedType) {
return true;
}

if ($this->hasTwoAndMoreGenericClassStringTypes($constType)) {
return true;
}

if (count($constType->getValueTypes()) > 3) {
foreach ($constType->getValueTypes() as $constValueType) {
if ($constValueType instanceof ConstantArrayType) {
return true;
}
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;

use PHP_CodeSniffer\Fixer;
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseConstantSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\UpperCaseConstantSniff;
use PHP_CodeSniffer\Standards\PSR12\Sniffs\Files\FileHeaderSniff;
use PhpCsFixer\Fixer\Casing\ConstantCaseFixer;
use PhpCsFixer\Fixer\Casing\LowercaseConstantsFixer;
use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer;
use PhpCsFixer\Fixer\Phpdoc\NoBlankLinesAfterPhpdocFixer;
use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;

final class SkipSoManyTypes
{
/**
* These groups do the opposite of each other, e.g. Yoda vs NoYoda.
*
* @var array<array<class-string<FixerInterface|Fixer>|string>>
*/
private const CONFLICTING_CHECKER_GROUPS = [
['SlevomatCodingStandard\Sniffs\ControlStructures\DisallowYodaComparisonSniff', YodaStyleFixer::class],
[LowerCaseConstantSniff::class, UpperCaseConstantSniff::class],
[LowercaseConstantsFixer::class, UpperCaseConstantSniff::class],
[ConstantCaseFixer::class, UpperCaseConstantSniff::class],
['SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff', DeclareEqualNormalizeFixer::class],
['SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff', BlankLineAfterOpeningTagFixer::class],
[FileHeaderSniff::class, NoBlankLinesAfterPhpdocFixer::class],
];
}

0 comments on commit 8a0d6d9

Please sign in to comment.