Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refined explode() types #9016

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions psalm-baseline.xml
@@ -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>
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
</PossiblyUnusedMethod>
</file>
<file src="tests/Internal/Codebase/InternalCallMapHandlerTest.php">
Expand Down
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php
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
Expand Up @@ -725,6 +725,46 @@ function join($separator, array $array = []): string
/**
* @psalm-pure
*
* @param non-empty-string $separator
*
* @return (
* $string is lowercase-string
* ? (
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
* $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
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
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>',
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
'$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>}',
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
],
],
'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
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