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
10 changes: 9 additions & 1 deletion packages/NodeTypeResolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,15 @@ public function isObjectType(Node $node, $requiredType): bool
return true;
}

return $resolvedType->isSuperTypeOf($requiredType)->yes();
if ($resolvedType instanceof UnionType) {
foreach ($resolvedType->getTypes() as $unionedType) {
if ($unionedType->equals($requiredType)) {
return true;
}
}
}

return false;
}

public function getObjectType(Node $node): Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ private function processStringType(Node $node, int $argumentPosition, Node $firs
return;
}

if ($firstArgument instanceof FuncCall && $this->isName($firstArgument, 'sprintf')) {
if ($this->isFunctionNamed($firstArgument, 'sprintf')) {
/** @var FuncCall $firstArgument */
$arrayNode = $this->nodeTransformer->transformSprintfToArray($firstArgument);
if ($arrayNode !== null) {
$node->args[$argumentPosition]->value = $arrayNode;
Expand All @@ -131,11 +132,10 @@ private function processPreviousAssign(Node $node, Node $firstArgument): void
/** @var Assign|null $createdNode */
$createdNode = $this->findPreviousNodeAssign($node, $firstArgument);

if ($createdNode instanceof Assign && $createdNode->expr instanceof FuncCall && $this->isName(
$createdNode->expr,
'sprintf'
)) {
$arrayNode = $this->nodeTransformer->transformSprintfToArray($createdNode->expr);
if ($createdNode instanceof Assign && $this->isFunctionNamed($createdNode->expr, 'sprintf')) {
/** @var FuncCall $funcCall */
$funcCall = $createdNode->expr;
$arrayNode = $this->nodeTransformer->transformSprintfToArray($funcCall);
if ($arrayNode !== null) {
$createdNode->expr = $arrayNode;
}
Expand All @@ -160,4 +160,13 @@ private function findPreviousNodeAssign(Node $node, Node $firstArgument): ?Assig
return $checkedNode;
});
}

private function isFunctionNamed(Node $node, string $name): bool
{
if (! $node instanceof FuncCall) {
return false;
}

return $this->isName($node, $name);
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,4 @@ parameters:
- '#In method "Rector\\Rector\\AbstractRector\:\:isDoctrineEntityClass", parameter \$class has no type\-hint and no @param annotation\. More info\: http\://bit\.ly/usetypehint#'
- '#In method "Rector\\FileSystemRector\\Rector\\AbstractFileSystemRector\:\:moveFile", parameter \$nodes type is "array"\. Please provide a @param annotation to further specify the type of the array\. For instance\: @param int\[\] \$nodes\. More info\: http\://bit\.ly/typehintarray#'
- '#Parameter \#1 \$items of class PhpParser\\Node\\Expr\\Array_ constructor expects array<PhpParser\\Node\\Expr\\ArrayItem\>, array<PhpParser\\Node\\Expr\\ArrayItem\|null\> given#'
- '#Parameter \#1 \$sprintfFuncCall of method Rector\\PhpParser\\NodeTransformer\:\:transformSprintfToArray\(\) expects PhpParser\\Node\\Expr\\FuncCall, PhpParser\\Node given#'