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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
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
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
* ? (
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
* $limit is int<min, -2>
* ? array<empty, empty>
* : (
* $limit is int<0, 1>
* ? array{lowercase-string}
* : (
* $limit is 2
* ? array{0: lowercase-string, 1?: lowercase-string}
* : (
* $limit is 3
* ? array{0: lowercase-string, 1?: lowercase-string, 2?: lowercase-string}
* : non-empty-list<lowercase-string>
* )
* )
* )
* )
* : (
* $limit is int<min, -2>
* ? array<empty, empty>
* : (
* $limit is int<0, 1>
* ? array{string}
* : (
* $limit is 2
* ? array{0: string, 1?: string}
* : (
* $limit is 3
* ? array{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
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' => 'array<never, never>',
],
],
'explodeWithDynamicLimit' => [
Expand All @@ -433,52 +433,63 @@ public static function Baz($mixed) : string {
*/
$elements = explode(" ", $string, $limit);',
'assertions' => [
'$elements' => 'list<string>',
'$elements' => 'array{0?: string, 1?: string, 2?: string, ...<int<0, max>, string>}',
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
],
],
'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' => 'array{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' => 'array<never, 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' => 'array{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