1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +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

@ -43,6 +43,15 @@ class ConstantTest extends TestCase
echo CONSTANT;',
],
'magicConstant' => [
'<?php
$a = __LINE__;
$b = __file__;',
'assertions' => [
'$a' => 'int',
'$b' => 'string',
],
],
];
}