diff --git a/src/Rule/ForbidVariableTypeOverwritingRule.php b/src/Rule/ForbidVariableTypeOverwritingRule.php index acacbb9..d7106c2 100644 --- a/src/Rule/ForbidVariableTypeOverwritingRule.php +++ b/src/Rule/ForbidVariableTypeOverwritingRule.php @@ -60,10 +60,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - if ( - !$previousVariableType->isSuperTypeOf($newVariableType)->yes() // allow narrowing - && !$newVariableType->isSuperTypeOf($previousVariableType)->yes() // allow generalization - ) { + $narrowing = $previousVariableType->accepts($newVariableType, true)->yes(); + $generalization = $newVariableType->accepts($previousVariableType, true)->yes(); + + if (!$narrowing && !$generalization) { $error = RuleErrorBuilder::message("Overwriting variable \$$variableName while changing its type from {$previousVariableType->describe(VerbosityLevel::precise())} to {$newVariableType->describe(VerbosityLevel::precise())}") ->identifier('shipmonk.variableTypeOverwritten') ->build(); diff --git a/tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php b/tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php index 9131f2b..da2cce5 100644 --- a/tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php +++ b/tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php @@ -151,3 +151,11 @@ function testEnumCaseChange() { function testIntToInt(int $positive, int $negative) { $positive = $negative; } + +function testFloatToInt(float $float, int $int) { + $float = $int; +} + +function testFIntToFloat(int $int, float $float) { + $int = $float; +}