From b229ac875dcd00b3442cc3b40792482b483f8e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Bate=C4=8Dko?= Date: Sat, 2 Nov 2019 10:10:38 +0100 Subject: [PATCH] Improve return types of explode() with limit (#2286) --- .../Internal/Analyzer/FunctionAnalyzer.php | 16 +++- tests/FunctionCallTest.php | 92 +++++++++++++++++-- 2 files changed, 96 insertions(+), 12 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/FunctionAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionAnalyzer.php index 707910f80..2b2563582 100644 --- a/src/Psalm/Internal/Analyzer/FunctionAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionAnalyzer.php @@ -249,20 +249,30 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer break; case 'explode': - if (count($call_args) === 2) { + if (count($call_args) >= 2) { + $can_return_empty = isset($call_args[2]) + && ( + !$call_args[2]->value instanceof PhpParser\Node\Scalar\LNumber + || $call_args[2]->value->value < 0 + ); + if ($call_args[0]->value instanceof PhpParser\Node\Scalar\String_) { if ($call_args[0]->value->value === '') { return Type::getFalse(); } return new Type\Union([ - new Type\Atomic\TNonEmptyList(Type::getString()) + $can_return_empty + ? new Type\Atomic\TList(Type::getString()) + : new Type\Atomic\TNonEmptyList(Type::getString()) ]); } elseif (isset($call_args[0]->value->inferredType) && $call_args[0]->value->inferredType->hasString() ) { $falsable_array = new Type\Union([ - new Type\Atomic\TNonEmptyList(Type::getString()), + $can_return_empty + ? new Type\Atomic\TList(Type::getString()) + : new Type\Atomic\TNonEmptyList(Type::getString()), new Type\Atomic\TFalse ]); diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index 1787ef9ca..bd06e4fb1 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -945,19 +945,93 @@ class FunctionCallTest extends TestCase '$c' => 'int', ], ], + 'explode' => [ + ' [ + '$elements' => 'non-empty-list', + ], + ], + 'explodeWithPositiveLimit' => [ + ' [ + '$elements' => 'non-empty-list', + ], + ], + 'explodeWithNegativeLimit' => [ + ' [ + '$elements' => 'list', + ], + ], + 'explodeWithDynamicLimit' => [ + ' [ + '$elements' => 'list', + ], + ], + 'explodeWithDynamicDelimiter' => [ + ' [ + '$elements' => 'false|non-empty-list', + ], + ], + 'explodeWithDynamicDelimiterAndPositiveLimit' => [ + ' [ + '$elements' => 'false|non-empty-list', + ], + ], + 'explodeWithDynamicDelimiterAndNegativeLimit' => [ + ' [ + '$elements' => 'false|list', + ], + ], + 'explodeWithDynamicDelimiterAndLimit' => [ + ' [ + '$elements' => 'false|list', + ], + ], 'explodeWithPossiblyFalse' => [ ' */ - function exploder(string $s) : array { - return explode(" ", $s); + /** @return non-empty-list */ + function exploder(string $d, string $s) : array { + return explode($d, $s); }', ], - 'explodeWithThirdArg' => [ - ' [ '