Skip to content
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
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

final class DefaultArrayWithKnownType
{
public function go()
{
$this->run(['item1', 'item2']);
}

private function run(array $items = [])
{
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

final class DefaultArrayWithKnownType
{
public function go()
{
$this->run(['item1', 'item2']);
}

/**
* @param string[] $items
*/
private function run(array $items = [])
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

final class SkipNonEmptyArrayContradictingDefault
{
public function callee(array $row = [])
{
return $row;
}

public function caller(array $row)
{
return $row['state'] && $this->callee($row);
}

public function caller2()
{
return $this->callee(['state' => 5, 'name' => 'x']);
}
}
3 changes: 2 additions & 1 deletion rules/TypeDeclarationDocblocks/NodeDocblockTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ private function isArrayMixed(Type $type): bool
return false;
}

return $type->getKeyType() instanceof IntegerType;
// both plain "mixed[]" (integer key) and a fully mixed-keyed array carry no useful value
return $type->getKeyType() instanceof IntegerType || $type->getKeyType() instanceof MixedType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\TypeDeclarationDocblocks\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
Expand Down Expand Up @@ -130,6 +131,14 @@ public function refactor(Node $node): ?Node
// in case of array type declaration, null cannot be passed or is already casted
$resolvedParameterType = TypeCombinator::removeNull($resolvedParameterType);

// the param default value must always be accepted; a locally inferred, flow-narrowed type such as
// "non-empty-array" would otherwise contradict an "= []" default - unite with the default type so
// the resulting @param never conflicts with the method signature
if ($param->default instanceof Expr) {
$defaultType = $this->nodeTypeResolver->getType($param->default);
$resolvedParameterType = TypeCombinator::union($resolvedParameterType, $defaultType);
}

$hasClassMethodChanged = $this->nodeDocblockTypeDecorator->decorateGenericIterableParamType(
$resolvedParameterType,
$classMethodPhpDocInfo,
Expand Down
Loading