Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dynamically type getters and hassers for parameters #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
ondrejmirtes merged 7 commits into phpstan:master from Kocal:GH-127
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: prefer create the default type manually instead
  • Loading branch information
Kocal committed May 1, 2021
commit c2d3f798e84fc9949f60ff28f45f09dc9d6cebe4
4 changes: 2 additions & 2 deletions composer.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"phpstan/phpstan-phpunit": "^0.12.16",
"phpstan/phpstan-strict-rules": "^0.12.5",
"phpunit/phpunit": "^7.5.20",
"symfony/console": "^4.0 || ^5.0",
"symfony/config": "^4.2 || ^5.0",
"symfony/framework-bundle": "^4.0 || ^5.0",
"symfony/console": "^4.0 || ^5.0",
"symfony/framework-bundle": "^4.4 || ^5.0",
"symfony/http-foundation": "^4.0 || ^5.0",
"symfony/messenger": "^4.2 || ^5.0",
"symfony/serializer": "^4.0 || ^5.0"
Expand Down
19 changes: 18 additions & 1 deletion src/Type/Symfony/ParameterDynamicReturnTypeExtension.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Symfony\ParameterMap;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use function in_array;

final class ParameterDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
Expand Down Expand Up @@ -71,7 +79,16 @@ private function getGetTypeFromMethodCall(
Scope $scope
): Type
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
// We don't use the method's return type because this won't work properly with lowest and
// highest versions of Symfony ("mixed" for lowest, "array|bool|float|integer|string|null" for highest).
$returnType = new UnionType([
new ArrayType(new MixedType(), new MixedType()),
new BooleanType(),
new FloatType(),
new IntegerType(),
new StringType(),
new NullType(),
]);
if (!isset($methodCall->args[0])) {
return $returnType;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/Symfony/data/ExampleAbstractController.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function services(): void
public function parameters(ContainerInterface $container, ParameterBagInterface $parameterBag): void
{
assertType('array|bool|float|int|string|null', $container->getParameter('unknown'));
assertType('mixed', $parameterBag->get('unknown'));
assertType('array|bool|float|int|string|null', $parameterBag->get('unknown'));
assertType('array|bool|float|int|string|null', $this->getParameter('unknown'));
assertType("'abcdef'", $container->getParameter('app.string'));
assertType("'abcdef'", $parameterBag->get('app.string'));
Expand Down

AltStyle によって変換されたページ (->オリジナル) /