From 723dfc51eab190ff940d0b0130b9986eb1b5d36a Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 17 Jan 2022 17:04:00 +0100 Subject: [PATCH] Achieve BenevolentUnionType return type without dynamic return type extension --- conf/config.neon | 10 ----- resources/functionMap.php | 4 +- src/PhpDoc/TypeNodeResolver.php | 6 +++ ...toptFunctionDynamicReturnTypeExtension.php | 26 ------------- .../PdoPrepareDynamicReturnTypeExtension.php | 37 ------------------- 5 files changed, 8 insertions(+), 75 deletions(-) delete mode 100644 src/Type/Php/GetoptFunctionDynamicReturnTypeExtension.php delete mode 100644 src/Type/Php/PdoPrepareDynamicReturnTypeExtension.php diff --git a/conf/config.neon b/conf/config.neon index a7325cb18c..a9699fee47 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1110,11 +1110,6 @@ services: tags: - phpstan.broker.dynamicFunctionReturnTypeExtension - - - class: PHPStan\Type\Php\GetoptFunctionDynamicReturnTypeExtension - tags: - - phpstan.broker.dynamicFunctionReturnTypeExtension - - class: PHPStan\Type\Php\GetParentClassDynamicFunctionReturnTypeExtension tags: @@ -1144,11 +1139,6 @@ services: tags: - phpstan.dynamicFunctionThrowTypeExtension - - - class: PHPStan\Type\Php\PdoPrepareDynamicReturnTypeExtension - tags: - - phpstan.broker.dynamicMethodReturnTypeExtension - - class: PHPStan\Type\Php\ReflectionClassConstructorThrowTypeExtension tags: diff --git a/resources/functionMap.php b/resources/functionMap.php index bb085e7f29..9a29b02e0a 100644 --- a/resources/functionMap.php +++ b/resources/functionMap.php @@ -3338,7 +3338,7 @@ 'getmyinode' => ['int|false'], 'getmypid' => ['int|false'], 'getmyuid' => ['int|false'], -'getopt' => ['array|array|array>|false', 'options'=>'string', 'longopts='=>'array', '&w_optind='=>'int'], +'getopt' => ['__benevolent|array|array>|false>', 'options'=>'string', 'longopts='=>'array', '&w_optind='=>'int'], 'getprotobyname' => ['int|false', 'name'=>'string'], 'getprotobynumber' => ['string|false', 'proto'=>'int'], 'getrandmax' => ['int'], @@ -8448,7 +8448,7 @@ 'PDO::pgsqlLOBCreate' => ['string'], 'PDO::pgsqlLOBOpen' => ['resource', 'oid'=>'string', 'mode='=>'string'], 'PDO::pgsqlLOBUnlink' => ['bool', 'oid'=>'string'], -'PDO::prepare' => ['PDOStatement|false', 'statement'=>'string', 'options='=>'array'], +'PDO::prepare' => ['__benevolent', 'statement'=>'string', 'options='=>'array'], 'PDO::query' => ['PDOStatement|false', 'sql'=>'string'], 'PDO::query\'1' => ['PDOStatement|false', 'sql'=>'string', 'fetch_column'=>'int', 'colno'=>'int'], 'PDO::query\'2' => ['PDOStatement|false', 'sql'=>'string', 'fetch_class'=>'int', 'classname'=>'string', 'ctorargs'=>'array'], diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index 4501609639..e7efa0a2a3 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -70,6 +70,7 @@ use PHPStan\Type\Type; use PHPStan\Type\TypeAliasResolver; use PHPStan\Type\TypeCombinator; +use PHPStan\Type\TypeUtils; use PHPStan\Type\TypeWithClassName; use PHPStan\Type\UnionType; use PHPStan\Type\VoidType; @@ -526,6 +527,11 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na return $genericTypes[0]->getIterableValueType(); } + return new ErrorType(); + } elseif ($mainTypeName === '__benevolent') { + if (count($genericTypes) === 1) { + return TypeUtils::toBenevolentUnion($genericTypes[0]); + } return new ErrorType(); } diff --git a/src/Type/Php/GetoptFunctionDynamicReturnTypeExtension.php b/src/Type/Php/GetoptFunctionDynamicReturnTypeExtension.php deleted file mode 100644 index becf64a90e..0000000000 --- a/src/Type/Php/GetoptFunctionDynamicReturnTypeExtension.php +++ /dev/null @@ -1,26 +0,0 @@ -getName() === 'getopt'; - } - - public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type - { - return TypeUtils::toBenevolentUnion(ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType()); - } - -} diff --git a/src/Type/Php/PdoPrepareDynamicReturnTypeExtension.php b/src/Type/Php/PdoPrepareDynamicReturnTypeExtension.php deleted file mode 100644 index e5d417ad49..0000000000 --- a/src/Type/Php/PdoPrepareDynamicReturnTypeExtension.php +++ /dev/null @@ -1,37 +0,0 @@ -getName() === 'prepare'; - } - - public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type - { - $returnType = ParametersAcceptorSelector::selectFromArgs( - $scope, - $methodCall->getArgs(), - $methodReflection->getVariants(), - )->getReturnType(); - - return TypeUtils::toBenevolentUnion($returnType); - } - -}