Skip to content

Commit

Permalink
Merge pull request #9016 from Ocramius/feature/#5039-more-refined-typ…
Browse files Browse the repository at this point in the history
…es-for-explode-core-function

Refined `explode()` types
  • Loading branch information
orklah committed Dec 29, 2022
2 parents 9770e11 + 45f743f commit 790c309
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 139 deletions.
14 changes: 11 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@6fc9e50b9765d573db796e81522af759bc6987a5">
<files psalm-version="dev-master@dbcfe62c5224603912c94c1eab5d7c31841ada82">
<file src="examples/TemplateChecker.php">
<PossiblyUndefinedIntArrayOffset>
<code>$comment_block-&gt;tags['variablesfrom'][0]</code>
Expand Down Expand Up @@ -296,6 +296,12 @@
<code>$b[$y]</code>
</InvalidArrayOffset>
</file>
<file src="src/Psalm/Internal/ExecutionEnvironment/GitInfoCollector.php">
<PossiblyUndefinedArrayOffset>
<code>$exploded[1]</code>
<code>$url</code>
</PossiblyUndefinedArrayOffset>
</file>
<file src="src/Psalm/Internal/FileManipulation/PropertyDocblockManipulator.php">
<PossiblyUndefinedIntArrayOffset>
<code>$stmt-&gt;props[0]</code>
Expand Down Expand Up @@ -379,9 +385,9 @@
<PossiblyInvalidArrayOffset>
<code>$fixed_type_tokens[$i - 1]</code>
</PossiblyInvalidArrayOffset>
<PossiblyUndefinedIntArrayOffset>
<PossiblyUndefinedArrayOffset>
<code>$source_param_string</code>
</PossiblyUndefinedIntArrayOffset>
</PossiblyUndefinedArrayOffset>
</file>
<file src="src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php">
<PossiblyUndefinedIntArrayOffset>
Expand Down Expand Up @@ -695,6 +701,8 @@
<PossiblyUnusedMethod>
<code>allFloatLiterals</code>
<code>allFloatLiterals</code>
<code>hasLowercaseString</code>
<code>hasLowercaseString</code>
</PossiblyUnusedMethod>
</file>
<file src="tests/Internal/Codebase/InternalCallMapHandlerTest.php">
Expand Down
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Psalm\Internal\Provider\ReturnTypeProvider\ArrayUniqueReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\BasenameReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\DirnameReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ExplodeReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\FilterVarReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\FirstArgStringReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\GetClassMethodsReturnTypeProvider;
Expand Down Expand Up @@ -94,7 +93,6 @@ public function __construct()
$this->registerClass(MktimeReturnTypeProvider::class);
$this->registerClass(BasenameReturnTypeProvider::class);
$this->registerClass(DirnameReturnTypeProvider::class);
$this->registerClass(ExplodeReturnTypeProvider::class);
$this->registerClass(GetObjectVarsReturnTypeProvider::class);
$this->registerClass(GetClassMethodsReturnTypeProvider::class);
$this->registerClass(FirstArgStringReturnTypeProvider::class);
Expand Down

This file was deleted.

40 changes: 40 additions & 0 deletions stubs/CoreGenericFunctions.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,46 @@ function join($separator, array $array = []): string
/**
* @psalm-pure
*
* @param non-empty-string $separator
*
* @return (
* $string is lowercase-string
* ? (
* $limit is int<min, -2>
* ? list<empty>
* : (
* $limit is int<0, 1>
* ? list{lowercase-string}
* : (
* $limit is 2
* ? list{0: lowercase-string, 1?: lowercase-string}
* : (
* $limit is 3
* ? list{0: lowercase-string, 1?: lowercase-string, 2?: lowercase-string}
* : non-empty-list<lowercase-string>
* )
* )
* )
* )
* : (
* $limit is int<min, -2>
* ? list<empty>
* : (
* $limit is int<0, 1>
* ? list{string}
* : (
* $limit is 2
* ? list{0: string, 1?: string}
* : (
* $limit is 3
* ? list{0: string, 1?: string, 2?: string}
* : non-empty-list<string>
* )
* )
* )
* )
* )
*
* @psalm-flow ($string) -(array-assignment)-> return
*/
function explode(string $separator, string $string, int $limit = -1) : array {}
Expand Down
2 changes: 1 addition & 1 deletion tests/AssertAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ function isStringTuple($value): void {
}
}
$s = "";
$s = "Hello World!";
$parts = explode(":", $s, 2);
Expand Down
46 changes: 25 additions & 21 deletions tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public static function Baz($mixed) : string {
/** @var string $string */
$elements = explode(" ", $string, -5);',
'assertions' => [
'$elements' => 'list<string>',
'$elements' => 'list<never>',
],
],
'explodeWithDynamicLimit' => [
Expand All @@ -433,52 +433,63 @@ public static function Baz($mixed) : string {
*/
$elements = explode(" ", $string, $limit);',
'assertions' => [
'$elements' => 'list<string>',
'$elements' => 'list{0?: string, 1?: string, 2?: string, ...<int<0, max>, string>}',
],
],
'explodeWithDynamicDelimiter' => [
'code' => '<?php
/**
* @var string $delim
* @var non-empty-string $delim
* @var string $string
*/
$elements = explode($delim, $string);',
'assertions' => [
'$elements' => 'false|non-empty-list<string>',
'$elements' => 'non-empty-list<string>',
],
],
'explodeWithDynamicDelimiterAndSmallPositiveLimit' => [
'code' => '<?php
/**
* @var non-empty-string $delim
* @var string $string
*/
$elements = explode($delim, $string, 2);',
'assertions' => [
'$elements' => 'list{0: string, 1?: string}',
],
],
'explodeWithDynamicDelimiterAndPositiveLimit' => [
'code' => '<?php
/**
* @var string $delim
* @var non-empty-string $delim
* @var string $string
*/
$elements = explode($delim, $string, 5);',
'assertions' => [
'$elements' => 'false|non-empty-list<string>',
'$elements' => 'non-empty-list<string>',
],
],
'explodeWithDynamicDelimiterAndNegativeLimit' => [
'code' => '<?php
/**
* @var string $delim
* @var non-empty-string $delim
* @var string $string
*/
$elements = explode($delim, $string, -5);',
'assertions' => [
'$elements' => 'false|list<string>',
'$elements' => 'list<never>',
],
],
'explodeWithDynamicDelimiterAndLimit' => [
'code' => '<?php
/**
* @var string $delim
* @var non-empty-string $delim
* @var string $string
* @var int $limit
*/
$elements = explode($delim, $string, $limit);',
'assertions' => [
'$elements' => 'false|list<string>',
'$elements' => 'list{0?: string, 1?: string, 2?: string, ...<int<0, max>, string>}',
],
],
'explodeWithDynamicNonEmptyDelimiter' => [
Expand All @@ -502,19 +513,12 @@ public static function Baz($mixed) : string {
'$elements' => 'non-empty-list<string>',
],
],
'explodeWithLiteralEmptyDelimiter' => [
'explodeWithPossiblyFalse' => [
'code' => '<?php
/**
* @var string $string
* @param non-empty-string $d
* @return non-empty-list<string>
*/
$elements = explode("", $string);',
'assertions' => [
'$elements' => 'false',
],
],
'explodeWithPossiblyFalse' => [
'code' => '<?php
/** @return non-empty-list<string> */
function exploder(string $d, string $s) : array {
return explode($d, $s);
}',
Expand Down Expand Up @@ -2232,7 +2236,7 @@ function a($b): int
function exploder(string $s) : array {
return explode("", $s);
}',
'error_message' => 'FalsableReturnStatement',
'error_message' => 'InvalidArgument',
],
'complainAboutArrayToIterable' => [
'code' => '<?php
Expand Down
9 changes: 0 additions & 9 deletions tests/TypeReconciliation/IssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1065,15 +1065,6 @@ public function test() : bool {
}',
'error_message' => 'InvalidArrayOffset',
],
'listDestructuringErrorSuppress' => [
'code' => '<?php
function foo(string $s) : string {
/** @psalm-suppress PossiblyUndefinedArrayOffset */
@list($port) = explode(":", $s, -1);
return $port;
}',
'error_message' => 'NullableReturnStatement',
],
'undefinedVarInNullCoalesce' => [
'code' => '<?php
function bar(): void {
Expand Down

0 comments on commit 790c309

Please sign in to comment.