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

Fix glob CallMap and stub to prevent crash on alpine #9942

Merged
merged 1 commit into from Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, where / in what context the ExpressionResolver is used. Maybe the stubbed constants in $codebase should be checked, too. Maybe we should mock PHP_VERSION constants to match the values expected for the requested analysis php version.

@orklah What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure either :(

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 @@ -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
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,
];
Comment on lines +409 to +414
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fail on Alpine and probably Solaris with UndefinedConstant

}

public function providerInvalidCodeParse(): iterable
Expand Down