2017-01-15 15:58:40 -05:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
class ConstantTest extends TestCase
|
2017-01-15 15:58:40 -05:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-01-15 15:58:40 -05:00
|
|
|
|
|
|
|
/**
|
2017-04-24 23:45:02 -04:00
|
|
|
* @return array
|
2017-01-15 15:58:40 -05:00
|
|
|
*/
|
2017-04-24 23:45:02 -04:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-01-15 15:58:40 -05:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
return [
|
|
|
|
'constantInFunction' => [
|
|
|
|
'<?php
|
|
|
|
useTest();
|
|
|
|
const TEST = 2;
|
2017-12-06 00:05:01 -05:00
|
|
|
|
2018-01-11 15:50:45 -05:00
|
|
|
function useTest(): int {
|
2017-04-24 23:45:02 -04:00
|
|
|
return TEST;
|
2017-05-26 20:05:57 -04:00
|
|
|
}',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'constantInClosure' => [
|
|
|
|
'<?php
|
|
|
|
const TEST = 2;
|
2017-12-06 00:05:01 -05:00
|
|
|
|
2018-01-11 15:50:45 -05:00
|
|
|
$useTest = function(): int {
|
2017-04-24 23:45:02 -04:00
|
|
|
return TEST;
|
|
|
|
};
|
2017-05-26 20:05:57 -04:00
|
|
|
$useTest();',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'constantDefinedInFunction' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function defineConstant() {
|
|
|
|
define("CONSTANT", 1);
|
|
|
|
}
|
2017-12-06 00:05:01 -05:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
defineConstant();
|
2017-12-06 00:05:01 -05:00
|
|
|
|
2017-05-26 20:05:57 -04:00
|
|
|
echo CONSTANT;',
|
|
|
|
],
|
2017-12-06 00:05:01 -05:00
|
|
|
'magicConstant' => [
|
|
|
|
'<?php
|
|
|
|
$a = __LINE__;
|
|
|
|
$b = __file__;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2017-12-19 09:48:01 -05:00
|
|
|
'getClassConstantValue' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
const B = [0, 1, 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = A::B[1];',
|
|
|
|
],
|
2018-01-25 10:47:15 -05:00
|
|
|
'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'],
|
|
|
|
],
|
2018-02-07 20:26:26 -05:00
|
|
|
'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'],
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
];
|
2017-01-15 15:58:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-24 23:45:02 -04:00
|
|
|
* @return array
|
2017-01-15 15:58:40 -05:00
|
|
|
*/
|
2017-04-24 23:45:02 -04:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2017-01-15 15:58:40 -05:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
return [
|
|
|
|
'constantDefinedInFunctionButNotCalled' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function defineConstant() {
|
|
|
|
define("CONSTANT", 1);
|
|
|
|
}
|
2017-12-06 00:05:01 -05:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
echo CONSTANT;',
|
2017-05-26 20:05:57 -04:00
|
|
|
'error_message' => 'UndefinedConstant',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
];
|
2017-01-15 16:43:49 -05:00
|
|
|
}
|
2017-01-15 15:58:40 -05:00
|
|
|
}
|