1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add proper types for magic constants

Fixes #362
This commit is contained in:
Matthew Brown 2017-12-06 00:05:01 -05:00
parent 52fc81e4ab
commit 59b50bdcdd
3 changed files with 29 additions and 7 deletions

View File

@ -109,7 +109,21 @@ class ExpressionChecker
} elseif ($stmt instanceof PhpParser\Node\Scalar\EncapsedStringPart) {
// do nothing
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst) {
// do nothing
switch (strtolower($stmt->getName())) {
case '__line__':
$stmt->inferredType = Type::getInt();
break;
case '__file__':
case '__dir__':
case '__function__':
case '__class__':
case '__trait__':
case '__method__':
case '__namespace__':
$stmt->inferredType = Type::getString();
break;
}
} elseif ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
$stmt->inferredType = Type::getInt();
} elseif ($stmt instanceof PhpParser\Node\Scalar\DNumber) {

View File

@ -3,7 +3,6 @@ namespace Psalm\Checker;
use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\CodeLocation;
use Psalm\Issue\PossiblyUndefinedVariable;
use Psalm\Issue\RedundantCondition;
use Psalm\Issue\TypeDoesNotContainNull;
use Psalm\Issue\TypeDoesNotContainType;

View File

@ -16,7 +16,7 @@ class ConstantTest extends TestCase
'<?php
useTest();
const TEST = 2;
function useTest() : int {
return TEST;
}',
@ -24,7 +24,7 @@ class ConstantTest extends TestCase
'constantInClosure' => [
'<?php
const TEST = 2;
$useTest = function() : int {
return TEST;
};
@ -38,11 +38,20 @@ class ConstantTest extends TestCase
function defineConstant() {
define("CONSTANT", 1);
}
defineConstant();
echo CONSTANT;',
],
'magicConstant' => [
'<?php
$a = __LINE__;
$b = __file__;',
'assertions' => [
'$a' => 'int',
'$b' => 'string',
],
],
];
}
@ -60,7 +69,7 @@ class ConstantTest extends TestCase
function defineConstant() {
define("CONSTANT", 1);
}
echo CONSTANT;',
'error_message' => 'UndefinedConstant',
],