Skip to content

Commit

Permalink
Add better return type for preg_split
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 12, 2020
1 parent 3887506 commit 58426d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions stubs/CoreGenericFunctions.phpstub
Expand Up @@ -602,6 +602,17 @@ function implode($glue, array $pieces = []) : string {}
*/
function explode(string $delimiter, string $string, int $limit = -1) : array {}

/**
* @psalm-pure
*
* @psalm-flow ($subject) -(array-assignment)-> return
*
* @return ($flags is 1|3|5|7 ? list<string>|false : non-empty-list<string>|false)
*
* @psalm-ignore-falsable-return
*/
function preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0) {}

/**
* @param array $input
*
Expand Down
15 changes: 15 additions & 0 deletions tests/FunctionCallTest.php
Expand Up @@ -1347,6 +1347,13 @@ function sayHello(string $format): void {
if (strpos("abcdefghijklmno", $format)) {}
}',
],
'pregSplit' => [
'<?php
/** @return non-empty-list */
function foo(string $s) {
return preg_split("/ /", $s);
}'
],
];
}

Expand Down Expand Up @@ -1832,6 +1839,14 @@ function sayHello(string $format): void {
}',
'error_message' => 'InvalidLiteralArgument',
],
'pregSplitNoEmpty' => [
'<?php
/** @return non-empty-list */
function foo(string $s) {
return preg_split("/ /", $s, -1, PREG_SPLIT_NO_EMPTY);
}',
'error_message' => 'InvalidReturnStatement'
],
];
}
}

0 comments on commit 58426d3

Please sign in to comment.