1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #867 - make some system constant types more broad

This commit is contained in:
Matt Brown 2018-07-06 13:35:36 -04:00
parent 7e09f59825
commit a887d09ca9
3 changed files with 80 additions and 16 deletions

View File

@ -6,6 +6,7 @@ use Psalm\Checker\ClassLikeChecker;
use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\Checker\StatementsChecker;
use Psalm\Checker\TraitChecker;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\InaccessibleClassConstant;
@ -222,4 +223,77 @@ class ConstFetchChecker
return null;
}
/**
* @param Codebase $codebase
* @param string $fq_const_name
* @param string $const_name
*
* @return Type\Union|null
*/
public static function getGlobalConstType(
Codebase $codebase,
$fq_const_name,
$const_name
) {
$predefined_constants = $codebase->config->getPredefinedConstants();
if (isset($predefined_constants[$fq_const_name ?: $const_name])) {
switch ($fq_const_name ?: $const_name) {
case 'PHP_VERSION':
case 'DIRECTORY_SEPARATOR':
case 'PATH_SEPARATOR':
case 'PEAR_EXTENSION_DIR':
case 'PEAR_INSTALL_DIR':
case 'PHP_BINARY':
case 'PHP_BINDIR':
case 'PHP_CONFIG_FILE_PATH':
case 'PHP_CONFIG_FILE_SCAN_DIR':
case 'PHP_DATADIR':
case 'PHP_EOL':
case 'PHP_EXTENSION_DIR':
case 'PHP_EXTRA_VERSION':
case 'PHP_LIBDIR':
case 'PHP_LOCALSTATEDIR':
case 'PHP_MANDIR':
case 'PHP_OS':
case 'PHP_OS_FAMILY':
case 'PHP_PREFIX':
case 'PHP_SAPI':
case 'PHP_SYSCONFDIR':
return Type::getString();
case 'PHP_MAJOR_VERSION':
case 'PHP_MINOR_VERSION':
case 'PHP_RELEASE_VERSION':
case 'PHP_DEBUG':
case 'PHP_FLOAT_DIG':
case 'PHP_INT_MAX':
case 'PHP_INT_MIN':
case 'PHP_INT_SIZE':
case 'PHP_MAXPATHLEN':
case 'PHP_VERSION_ID':
case 'PHP_ZTS':
return Type::getInt();
case 'PHP_FLOAT_EPSILON':
case 'PHP_FLOAT_MAX':
case 'PHP_FLOAT_MIN':
return Type::getFloat();
}
$type = ClassLikeChecker::getTypeFromValue($predefined_constants[$fq_const_name ?: $const_name]);
return $type;
}
$stubbed_const_type = $codebase->getStubbedConstantType(
$fq_const_name ?: $const_name
);
if ($stubbed_const_type) {
return $stubbed_const_type;
}
return null;
}
}

View File

@ -12,6 +12,7 @@ use Psalm\Checker\Statements\Block\WhileChecker;
use Psalm\Checker\Statements\Expression\Assignment\PropertyAssignmentChecker;
use Psalm\Checker\Statements\Expression\BinaryOpChecker;
use Psalm\Checker\Statements\Expression\CallChecker;
use Psalm\Checker\Statements\Expression\Fetch\ConstFetchChecker;
use Psalm\Checker\Statements\Expression\Fetch\VariableFetchChecker;
use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\Checker\Statements\ReturnChecker;
@ -1192,22 +1193,7 @@ class StatementsChecker extends SourceChecker implements StatementsSource
return $file_storage_provider->get($constant_file_path)->constants[$const_name];
}
$predefined_constants = Config::getInstance()->getPredefinedConstants();
if (isset($predefined_constants[$fq_const_name ?: $const_name])) {
$type = ClassLikeChecker::getTypeFromValue($predefined_constants[$fq_const_name ?: $const_name]);
return $type;
}
$stubbed_const_type = $project_checker->codebase->getStubbedConstantType(
$fq_const_name ?: $const_name
);
if ($stubbed_const_type) {
return $stubbed_const_type;
}
return null;
return ConstFetchChecker::getGlobalConstType($project_checker->codebase, $fq_const_name, $const_name);
}
/**

View File

@ -211,6 +211,10 @@ class ConstantTest extends TestCase
'$b' => 'string',
],
],
'allowConstCheckForDifferentPlatforms' => [
'<?php
if ("phpdbg" === \PHP_SAPI) {}',
],
];
}