Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip so many types #5802

Merged
merged 1 commit into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
];
}