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 authored and danog committed Jan 29, 2021
1 parent 0934988 commit 68526c3
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,13 @@ function sayHello(string $format): void {
[],
'7.4'
],
'pregSplit' => [
'<?php
/** @return non-empty-list */
function foo(string $s) {
return preg_split("/ /", $s);
}'
],
];
}

Expand Down Expand Up @@ -1856,6 +1863,14 @@ function foo(array $a) {
}',
'error_message' => 'ArgumentTypeCoercion',
],
'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 68526c3

Please sign in to comment.