From 1fab75eb04d17574c0f69233eae3364983c7f753 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Mon, 2 Mar 2020 23:20:49 +0100 Subject: [PATCH] Typed callable when mixed used --- .../src/Ast/Type/AttributeAwareCallableTypeNode.php | 6 +++--- .../FixtureCallable/callable_empty_params.txt | 1 + .../PhpDocInfoPrinter/FixtureCallable/callable_mixed.txt | 2 +- .../PhpDocInfoPrinter/FixtureCallable/callable_param.txt | 2 +- .../FixtureCallable/callable_without_types.txt | 1 + 5 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_empty_params.txt create mode 100644 packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_without_types.txt diff --git a/packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareCallableTypeNode.php b/packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareCallableTypeNode.php index 0d97629b854..2d733d07919 100644 --- a/packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareCallableTypeNode.php +++ b/packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareCallableTypeNode.php @@ -34,7 +34,7 @@ final class AttributeAwareCallableTypeNode extends CallableTypeNode implements A } $parameterTypeString = $this->normalizeParameterType($parameterTypeString, $returnTypeAsString); - $returnTypeAsString = $this->normalizeReturnType($returnTypeAsString); + $returnTypeAsString = $this->normalizeReturnType($parameterTypeString, $returnTypeAsString); return sprintf('%s%s%s', $this->identifier->name, $parameterTypeString, $returnTypeAsString); } @@ -51,9 +51,9 @@ final class AttributeAwareCallableTypeNode extends CallableTypeNode implements A return trim($parameterTypeString); } - private function normalizeReturnType(string $returnTypeAsString): string + private function normalizeReturnType(string $parameterTypeString, string $returnTypeAsString): string { - if ($returnTypeAsString === 'mixed') { + if ($returnTypeAsString === 'mixed' && $parameterTypeString === '') { return ''; } diff --git a/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_empty_params.txt b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_empty_params.txt new file mode 100644 index 00000000000..953419c6211 --- /dev/null +++ b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_empty_params.txt @@ -0,0 +1 @@ +/** @var callable():int */ diff --git a/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_mixed.txt b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_mixed.txt index 220b831a535..d8b26e83423 100644 --- a/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_mixed.txt +++ b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_mixed.txt @@ -1 +1 @@ -/** @var callable(array) */ +/** @var callable(array):mixed */ diff --git a/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_param.txt b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_param.txt index effcce956d7..b8ec2aa2bd1 100644 --- a/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_param.txt +++ b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_param.txt @@ -1 +1 @@ -/** @param callable(array) $hitCallback */ +/** @param callable(array):mixed $hitCallback */ diff --git a/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_without_types.txt b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_without_types.txt new file mode 100644 index 00000000000..bec4a5b48da --- /dev/null +++ b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureCallable/callable_without_types.txt @@ -0,0 +1 @@ +/** @var callable */