Skip to content

Commit

Permalink
Fix glob CallMap and stub to prevent crash on alpine (and possibly ot…
Browse files Browse the repository at this point in the history
…her systems)
  • Loading branch information
ygottschalk committed Jun 21, 2023
1 parent 8e63608 commit 2020d0f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dictionaries/CallMap.php
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
Expand Up @@ -10728,7 +10728,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
14 changes: 14 additions & 0 deletions src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php
Expand Up @@ -514,6 +514,20 @@ 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
Expand Up @@ -1716,31 +1716,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
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 2020d0f

Please sign in to comment.