Skip to content

Commit

Permalink
Improve configure() method types (#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 28, 2021
1 parent 6ca7133 commit 0a704af
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function isSubtype(Type $checkedType, Type $mainType): bool
private function areAliasedObjectMatchingFqnObject(Type $firstType, Type $secondType): bool
{
if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType && $firstType->getFullyQualifiedName() === $secondType->getClassName()) {
return true;
}

if (! $secondType instanceof AliasedObjectType) {
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,8 @@ parameters:

# waits for enum phpstan to support PHSPtan 1.0
- '#Constant Rector\\(.*?)\\Enum\\(.*?)\:\:(.*?) is unused#'

# type is known - skip asserts for some reason
- '#Property Rector\\Transform\\Rector\\FuncCall\\ArgumentFuncCallToMethodCallRector\:\:\$arrayFunctionsToMethodCalls \(array<Rector\\Transform\\ValueObject\\ArrayFuncCallToMethodCall\>\) does not accept array<Rector\\Transform\\ValueObject\\ArgumentFuncCallToMethodCall\|Rector\\Transform\\ValueObject\\ArrayFuncCallToMethodCall\>#'
- '#Property Rector\\Transform\\Rector\\FuncCall\\ArgumentFuncCallToMethodCallRector\:\:\$argumentFuncCallToMethodCalls \(array<Rector\\Transform\\ValueObject\\ArgumentFuncCallToMethodCall\>\) does not accept array<Rector\\Transform\\ValueObject\\ArgumentFuncCallToMethodCall\|Rector\\Transform\\ValueObject\\ArrayFuncCallToMethodCall\>#'

Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<string, ReturnArrayClassMethodToYield[]> $configuration
*/
public function configure(array $configuration): void
{
Expand Down
11 changes: 8 additions & 3 deletions rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ final class AddLiteralSeparatorToNumberRector extends AbstractRector implements
*/
private const GROUP_SIZE = 3;

private int $limitValue = 1_000_000;
/**
* @var int
*/
private const DEFAULT_LIMIT_VALUE = 1_000_000;

private int $limitValue = self::DEFAULT_LIMIT_VALUE;

/**
* @param mixed[] $configuration
* @param array<string, int> $configuration
*/
public function configure(array $configuration): void
{
$limitValue = $configuration[self::LIMIT_VALUE] ?? 1_000_000;
$limitValue = $configuration[self::LIMIT_VALUE] ?? self::DEFAULT_LIMIT_VALUE;
Assert::integer($limitValue);

$this->limitValue = $limitValue;
Expand Down
9 changes: 7 additions & 2 deletions rules/Renaming/Rector/FuncCall/RenameFunctionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Renaming\Rector\FuncCall\RenameFunctionRector\RenameFunctionRectorTest
Expand Down Expand Up @@ -76,11 +77,15 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<string, array<string, string>> $configuration
*/
public function configure(array $configuration): void
{
$this->oldFunctionToNewFunction = $configuration[self::OLD_FUNCTION_TO_NEW_FUNCTION] ?? [];
$oldFunctionToNewFunction = $configuration[self::OLD_FUNCTION_TO_NEW_FUNCTION] ?? [];
Assert::allString($oldFunctionToNewFunction);
Assert::allString(array_values($oldFunctionToNewFunction));

$this->oldFunctionToNewFunction = $oldFunctionToNewFunction;
}

private function createName(string $newFunction): Name
Expand Down
11 changes: 8 additions & 3 deletions rules/Renaming/Rector/String_/RenameStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @changelog https://github.com/symfony/symfony/pull/35858
Expand All @@ -24,7 +25,7 @@ final class RenameStringRector extends AbstractRector implements ConfigurableRec
public const STRING_CHANGES = 'string_changes';

/**
* @var mixed[]
* @var array<string, string>
*/
private array $stringChanges = [];

Expand Down Expand Up @@ -86,10 +87,14 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<string, array<string, string>> $configuration
*/
public function configure(array $configuration): void
{
$this->stringChanges = $configuration[self::STRING_CHANGES] ?? [];
$stringChanges = $configuration[self::STRING_CHANGES] ?? [];
Assert::allString($stringChanges);
Assert::allString(array_values($stringChanges));

$this->stringChanges = $stringChanges;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,18 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<string, array<ArgumentFuncCallToMethodCall|ArrayFuncCallToMethodCall>> $configuration
*/
public function configure(array $configuration): void
{
$functionToMethodCalls = $configuration[self::FUNCTIONS_TO_METHOD_CALLS] ?? [];
Assert::allIsInstanceOf($functionToMethodCalls, ArgumentFuncCallToMethodCall::class);

$this->argumentFuncCallToMethodCalls = $functionToMethodCalls;

$arrayFunctionsToMethodCalls = $configuration[self::ARRAY_FUNCTIONS_TO_METHOD_CALLS] ?? [];
Assert::allIsInstanceOf($arrayFunctionsToMethodCalls, ArrayFuncCallToMethodCall::class);

$this->arrayFunctionsToMethodCalls = $arrayFunctionsToMethodCalls;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<string, AddParamTypeDeclaration[]> $configuration
*/
public function configure(array $configuration): void
{
$parameterTypehints = $configuration[self::PARAMETER_TYPEHINTS] ?? [];
Assert::allIsInstanceOf($parameterTypehints, AddParamTypeDeclaration::class);

$this->parameterTypehints = $parameterTypehints;
}

Expand Down

0 comments on commit 0a704af

Please sign in to comment.