Skip to content

Commit

Permalink
Merge pull request #113 from eliashaeussler/fix/request-attribute-def…
Browse files Browse the repository at this point in the history
…ault

[BUGFIX] Include default value passed to `$request->getAttribute()`
  • Loading branch information
sascha-egerer authored Mar 19, 2023
2 parents 9861cdc + bfb0286 commit 4f3a47b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Type/RequestDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,28 @@ public function getTypeFromMethodCall(
): Type
{
$argument = $methodCall->getArgs()[0] ?? null;
$defaultArgument = $methodCall->getArgs()[1] ?? null;

if ($argument === null || !($argument->value instanceof \PhpParser\Node\Scalar\String_)) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if ($argument === null
|| !($argument->value instanceof \PhpParser\Node\Scalar\String_)
|| !isset($this->requestGetAttributeMapping[$argument->value->value])
) {
$type = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

if ($defaultArgument === null) {
return $type;
}

return TypeCombinator::union($type, $scope->getType($defaultArgument->value));
}

if (isset($this->requestGetAttributeMapping[$argument->value->value])) {
return TypeCombinator::addNull($this->typeStringResolver->resolve($this->requestGetAttributeMapping[$argument->value->value]));
$type = $this->typeStringResolver->resolve($this->requestGetAttributeMapping[$argument->value->value]);

if ($defaultArgument === null) {
return TypeCombinator::addNull($type);
}

return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return TypeCombinator::union($type, $scope->getType($defaultArgument->value));
}

public function isMethodSupported(
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Type/data/request-get-attribute-return-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Site\Entity\NullSite;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use function PHPStan\Testing\assertType;
Expand All @@ -21,6 +22,7 @@ public function getAttributeTests(ServerRequestInterface $request): void
);
assertType(SiteLanguage::class . '|null', $request->getAttribute('language'));
assertType(Site::class . '|null', $request->getAttribute('site'));
assertType(NullSite::class . '|' . Site::class, $request->getAttribute('site', new NullSite()));
assertType(NormalizedParams::class . '|null', $request->getAttribute('normalizedParams'));
assertType('1|2|4|8|16|null', $request->getAttribute('applicationType'));
assertType('FlowdGmbh\\MyProject\\Http\\MyAttribute|null', $request->getAttribute('myCustomAttribute'));
Expand Down

0 comments on commit 4f3a47b

Please sign in to comment.