Skip to content

Commit

Permalink
Merge pull request #9942 from ygottschalk/fix/9886-glob-callmap-stub-…
Browse files Browse the repository at this point in the history
…crash

Fix glob CallMap and stub to prevent crash on alpine
  • Loading branch information
orklah committed Jun 27, 2023
2 parents 16f84c8 + f93d238 commit 086b943
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dictionaries/CallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3330,7 +3330,7 @@
'gettimeofday' => ['array<string, int>'],
'gettimeofday\'1' => ['float', 'as_float='=>'true'],
'gettype' => ['string', 'value'=>'mixed'],
'glob' => ['false|list{0?:string, ...<non-empty-string>}', 'pattern'=>'string', 'flags='=>'int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR>'],
'glob' => ['false|list{0?:string, ...<non-empty-string>}', 'pattern'=>'string', 'flags='=>'int<0, max>'],
'GlobIterator::__construct' => ['void', 'pattern'=>'string', 'flags='=>'int'],
'GlobIterator::count' => ['int'],
'GlobIterator::current' => ['FilesystemIterator|SplFileInfo|string'],
Expand Down
2 changes: 1 addition & 1 deletion dictionaries/CallMap_historical.php
Original file line number Diff line number Diff line change
Expand Up @@ -10764,7 +10764,7 @@
'gettimeofday' => ['array<string, int>'],
'gettimeofday\'1' => ['float', 'as_float='=>'true'],
'gettype' => ['string', 'value'=>'mixed'],
'glob' => ['false|list{0?:string, ...<non-empty-string>}', 'pattern'=>'string', 'flags='=>'int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR>'],
'glob' => ['false|list{0?:string, ...<non-empty-string>}', 'pattern'=>'string', 'flags='=>'int<0, max>'],
'gmdate' => ['string', 'format'=>'string', 'timestamp='=>'int'],
'gmmktime' => ['int|false', 'hour='=>'int', 'minute='=>'int', 'second='=>'int', 'month='=>'int', 'day='=>'int', 'year='=>'int'],
'gmp_abs' => ['GMP', 'num'=>'GMP|string|int'],
Expand Down
16 changes: 16 additions & 0 deletions src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
use ReflectionClass;
use ReflectionFunction;

use function array_merge;
use function array_values;
use function assert;
use function class_exists;
use function function_exists;
use function get_defined_constants;
use function implode;
use function in_array;
use function interface_exists;
Expand Down Expand Up @@ -514,6 +517,19 @@ private static function functionEvaluatesToTrue(
return false;
}

if ($function->name->parts === ['defined']
&& isset($function->getArgs()[0])
&& $function->getArgs()[0]->value instanceof PhpParser\Node\Scalar\String_
) {
$predefined_constants = get_defined_constants(true);
if (isset($predefined_constants['user'])) {
unset($predefined_constants['user']);
}
$predefined_constants = array_merge(...array_values($predefined_constants));

return isset($predefined_constants[$function->getArgs()[0]->value->value]);
}

return null;
}
}
80 changes: 53 additions & 27 deletions stubs/CoreGenericFunctions.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -1717,31 +1717,57 @@ function pg_escape_literal($string1, $string2 = null) {}
*/
function pg_escape_string($string1, $string2 = null) {}

/**
* @psalm-template P of string
* @psalm-template F of int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR>
* @psalm-param P $pattern
* @psalm-param F $flags
* @psalm-return (
* P is ''
* ? (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR>
* ? false|list<never>
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ERR>
* ? false|list{0:''}
* : false|list<never>
* )
* )
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR>
* ? false|list<non-empty-string>
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ERR>
* ? false|list{0:non-empty-string, ...<non-empty-string>}
* : false|list<non-empty-string>
* )
* )
* )
* @psalm-ignore-falsable-return
*/
function glob (string $pattern, int $flags = 0): array|false {}


if (defined('GLOB_BRACE')) {
/**
* @psalm-template P of string
* @psalm-template F of int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR>
* @psalm-param P $pattern
* @psalm-param F $flags
* @psalm-return (
* P is ''
* ? (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR>
* ? false|list<never>
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ERR>
* ? false|list{0:''}
* : false|list<never>
* )
* )
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR>
* ? false|list<non-empty-string>
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ERR>
* ? false|list{0:non-empty-string, ...<non-empty-string>}
* : false|list<non-empty-string>
* )
* )
* )
* @psalm-ignore-falsable-return
*/
function glob (string $pattern, int $flags = 0): array|false {}
} else {
/**
* @psalm-template P of string
* @psalm-template F of int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_ONLYDIR, GLOB_ERR>
* @psalm-param P $pattern
* @psalm-param F $flags
* @psalm-return (
* P is ''
* ? (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOESCAPE, GLOB_ONLYDIR, GLOB_ERR>
* ? false|list<never>
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_ERR>
* ? false|list{0:''}
* : false|list<never>
* )
* )
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOESCAPE, GLOB_ONLYDIR, GLOB_ERR>
* ? false|list<non-empty-string>
* : (F is int-mask<GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_ERR>
* ? false|list{0:non-empty-string, ...<non-empty-string>}
* : false|list<non-empty-string>
* )
* )
* )
* @psalm-ignore-falsable-return
*/
function glob (string $pattern, int $flags = 0): array|false {}
}

6 changes: 6 additions & 0 deletions tests/CoreStubsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,12 @@ function takesList(array $list): void {}
takesList(glob( '' ));
PHP,
];
yield 'glob accepts GLOB_BRACE' => [
'code' => <<<'PHP'
<?php
$globBrace = glob('abc', GLOB_BRACE);
PHP,
];
}

public function providerInvalidCodeParse(): iterable
Expand Down

0 comments on commit 086b943

Please sign in to comment.