mirror of
https://github.com/danog/psalm.git
synced 2024-12-04 02:27:59 +01:00
142 lines
4.2 KiB
PHP
142 lines
4.2 KiB
PHP
<?php
|
|
namespace Psalm\Tests;
|
|
|
|
class ConstantTest extends TestCase
|
|
{
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function providerFileCheckerValidCodeParse()
|
|
{
|
|
return [
|
|
'constantInFunction' => [
|
|
'<?php
|
|
useTest();
|
|
const TEST = 2;
|
|
|
|
function useTest(): int {
|
|
return TEST;
|
|
}',
|
|
],
|
|
'constantInClosure' => [
|
|
'<?php
|
|
const TEST = 2;
|
|
|
|
$useTest = function(): int {
|
|
return TEST;
|
|
};
|
|
$useTest();',
|
|
],
|
|
'constantDefinedInFunction' => [
|
|
'<?php
|
|
/**
|
|
* @return void
|
|
*/
|
|
function defineConstant() {
|
|
define("CONSTANT", 1);
|
|
}
|
|
|
|
defineConstant();
|
|
|
|
echo CONSTANT;',
|
|
],
|
|
'magicConstant' => [
|
|
'<?php
|
|
$a = __LINE__;
|
|
$b = __file__;',
|
|
'assertions' => [
|
|
'$a' => 'int',
|
|
'$b' => 'string',
|
|
],
|
|
],
|
|
'getClassConstantValue' => [
|
|
'<?php
|
|
class A {
|
|
const B = [0, 1, 2];
|
|
}
|
|
|
|
$a = A::B[1];',
|
|
],
|
|
'staticConstEval' => [
|
|
'<?php
|
|
abstract class Enum {
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
protected const VALUES = [];
|
|
public static function export(): string
|
|
{
|
|
assert(!empty(static::VALUES));
|
|
$values = array_map(
|
|
function(string $val): string {
|
|
return "\'" . $val . "\'";
|
|
},
|
|
static::VALUES
|
|
);
|
|
return join(",", $values);
|
|
}
|
|
}',
|
|
'assertions' => [],
|
|
'error_levels' => ['MixedArgument'],
|
|
],
|
|
'undefinedConstant' => [
|
|
'<?php
|
|
switch (rand(0, 50)) {
|
|
case FORTY: // Observed a valid UndeclaredConstant warning
|
|
$x = "value";
|
|
break;
|
|
default:
|
|
$x = "other";
|
|
}
|
|
|
|
echo $x;',
|
|
'assertions' => [],
|
|
'error_levels' => ['UndefinedConstant'],
|
|
],
|
|
'suppressUndefinedClassConstant' => [
|
|
'<?php
|
|
class C {}
|
|
|
|
/** @psalm-suppress UndefinedConstant */
|
|
$a = POTATO;
|
|
|
|
/** @psalm-suppress UndefinedConstant */
|
|
$a = C::POTATO;',
|
|
'assertions' => [],
|
|
'error_levels' => ['MixedAssignment'],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
{
|
|
return [
|
|
'constantDefinedInFunctionButNotCalled' => [
|
|
'<?php
|
|
/**
|
|
* @return void
|
|
*/
|
|
function defineConstant() {
|
|
define("CONSTANT", 1);
|
|
}
|
|
|
|
echo CONSTANT;',
|
|
'error_message' => 'UndefinedConstant',
|
|
],
|
|
'undefinedClassConstantInParamDefault' => [
|
|
'<?php
|
|
class A {
|
|
public function doSomething(int $howManyTimes = self::DEFAULT_TIMES): void {}
|
|
}',
|
|
'error_message' => 'UndefinedConstant',
|
|
],
|
|
];
|
|
}
|
|
}
|