From 8f8cc6aed54e8e26b096456873295bf959f9b98f Mon Sep 17 00:00:00 2001 From: Brown Date: Sat, 4 Apr 2020 09:27:11 -0400 Subject: [PATCH] Move conditional return type tests --- tests/AnnotationTest.php | 175 ----------------- tests/Template/ConditionalReturnTypeTest.php | 195 +++++++++++++++++++ 2 files changed, 195 insertions(+), 175 deletions(-) create mode 100644 tests/Template/ConditionalReturnTypeTest.php diff --git a/tests/AnnotationTest.php b/tests/AnnotationTest.php index dc7151ff2..21a678139 100644 --- a/tests/AnnotationTest.php +++ b/tests/AnnotationTest.php @@ -1119,181 +1119,6 @@ class AnnotationTest extends TestCase return false; }' ], - 'conditionalReturnType' => [ - ' */ - private array $itemAttr = []; - - /** - * @template T as ?string - * @param T $name - * @return string|string[] - * @psalm-return (T is string ? string : array) - */ - public function getAttribute(?string $name, string $default = "") - { - if (null === $name) { - return $this->itemAttr; - } - return isset($this->itemAttr[$name]) ? $this->itemAttr[$name] : $default; - } - } - - $a = (new A)->getAttribute("colour", "red"); // typed as string - $b = (new A)->getAttribute(null); // typed as array - /** @psalm-suppress MixedArgument */ - $c = (new A)->getAttribute($_GET["foo"]); // typed as string|array', - [ - '$a' => 'string', - '$b' => 'array', - '$c' => 'array|string' - ] - ], - 'nestedConditionalOnIntReturnType' => [ - ' [ - ' [ - ' [ - ' [ - ' 'int', - '$float1' => 'float', - '$float2' => 'float', - '$float3' => 'float', - ] - ], - 'nestedClassConstantConditionalComparison' => [ - 'getDifferentType(0); - $int2 = (new A)->getDifferentType(1); - $bool2 = (new A)->getDifferentType(4);', - [ - '$string' => 'string', - '$int' => 'int', - '$bool' => 'bool', - '$string2' => 'string', - '$int2' => 'int', - '$bool2' => 'bool', - ] - ], ]; } diff --git a/tests/Template/ConditionalReturnTypeTest.php b/tests/Template/ConditionalReturnTypeTest.php new file mode 100644 index 000000000..fa2e57856 --- /dev/null +++ b/tests/Template/ConditionalReturnTypeTest.php @@ -0,0 +1,195 @@ +,error_levels?:string[]}> + */ + public function providerValidCodeParse() + { + return [ + 'conditionalReturnType' => [ + ' */ + private array $itemAttr = []; + + /** + * @template T as ?string + * @param T $name + * @return string|string[] + * @psalm-return (T is string ? string : array) + */ + public function getAttribute(?string $name, string $default = "") + { + if (null === $name) { + return $this->itemAttr; + } + return isset($this->itemAttr[$name]) ? $this->itemAttr[$name] : $default; + } + } + + $a = (new A)->getAttribute("colour", "red"); // typed as string + $b = (new A)->getAttribute(null); // typed as array + /** @psalm-suppress MixedArgument */ + $c = (new A)->getAttribute($_GET["foo"]); // typed as string|array', + [ + '$a' => 'string', + '$b' => 'array', + '$c' => 'array|string' + ] + ], + 'nestedConditionalOnIntReturnType' => [ + ' [ + ' [ + ' [ + ' [ + ' 'int', + '$float1' => 'float', + '$float2' => 'float', + '$float3' => 'float', + ] + ], + 'nestedClassConstantConditionalComparison' => [ + 'getDifferentType(0); + $int2 = (new A)->getDifferentType(1); + $bool2 = (new A)->getDifferentType(4);', + [ + '$string' => 'string', + '$int' => 'int', + '$bool' => 'bool', + '$string2' => 'string', + '$int2' => 'int', + '$bool2' => 'bool', + ] + ], + ]; + } +}