Skip to content

Commit bdbea69

Browse files
authored
Merge pull request #51 from monojp/fix-shortcode-atts-extension
Fix shortcode_atts extension
2 parents ad364f2 + c54e976 commit bdbea69

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/ShortcodeAttsDynamicFunctionReturnTypeExtension.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\FunctionReflection;
1414
use PHPStan\Reflection\ParametersAcceptorSelector;
15+
use PHPStan\Type\Constant\ConstantArrayType;
16+
use PHPStan\Type\StringType;
1517
use PHPStan\Type\Type;
18+
use PHPStan\Type\TypeCombinator;
1619

1720
final class ShortcodeAttsDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
1821
{
@@ -31,6 +34,21 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3134
)->getReturnType();
3235
}
3336

34-
return $scope->getType($functionCall->args[0]->value);
37+
$type = $scope->getType($functionCall->args[0]->value);
38+
39+
if ($type instanceof ConstantArrayType) {
40+
// shortcode_atts values are coming from the defined defaults or from the actual string shortcode attributes
41+
return new ConstantArrayType(
42+
$type->getKeyTypes(),
43+
array_map(
44+
static function (Type $valueType): Type {
45+
return TypeCombinator::union($valueType, new StringType());
46+
},
47+
$type->getValueTypes()
48+
)
49+
);
50+
}
51+
52+
return $type;
3553
}
3654
}

0 commit comments

Comments
 (0)