Skip to content

Commit

Permalink
Fixed IncompatibleDefaultParameterTypeRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 10, 2020
1 parent a66d6b1 commit a5beb06
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Rules/Functions/IncompatibleDefaultParameterTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\VerbosityLevel;

/**
Expand Down Expand Up @@ -59,8 +60,9 @@ public function processNode(Node $node, Scope $scope): array

$defaultValueType = $scope->getType($param->default);
$parameterType = $parameters->getParameters()[$paramI]->getType();
$parameterType = TemplateTypeHelper::resolveToBounds($parameterType);

if ($parameterType->isSuperTypeOf($defaultValueType)->yes()) {
if ($parameterType->accepts($defaultValueType, true)->yes()) {
continue;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Rules/Methods/IncompatibleDefaultParameterTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\VerbosityLevel;

/**
Expand Down Expand Up @@ -45,8 +46,9 @@ public function processNode(Node $node, Scope $scope): array

$defaultValueType = $scope->getType($param->default);
$parameterType = $parameters->getParameters()[$paramI]->getType();
$parameterType = TemplateTypeHelper::resolveToBounds($parameterType);

if ($parameterType->isSuperTypeOf($defaultValueType)->yes()) {
if ($parameterType->accepts($defaultValueType, true)->yes()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,30 @@ public function bar(
}

}

class Bar
{

/**
* @param array{name?:string} $settings
*/
public function doFoo(array $settings = []): void
{

}

public function doBar(float $test2 = 0): void
{

}

/**
* @template T
* @param T $a
*/
public function doBaz($a = 'str'): void
{

}

}

0 comments on commit a5beb06

Please sign in to comment.