2017-01-15 21:58:40 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2017-01-15 21:58:40 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2022-01-31 19:23:29 +01:00
|
|
|
use Psalm\Context;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2022-01-31 19:23:29 +01:00
|
|
|
use function getcwd;
|
|
|
|
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ConstantTest extends TestCase
|
2017-01-15 21:58:40 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-01-15 21:58:40 +01:00
|
|
|
|
2022-01-20 23:51:55 +01:00
|
|
|
// TODO: Waiting for https://github.com/vimeo/psalm/issues/7125
|
|
|
|
// public function testKeyofSelfConstDoesntImplyKeyofStaticConst(): void
|
|
|
|
// {
|
|
|
|
// $this->expectException(CodeException::class);
|
|
|
|
// $this->expectExceptionMessage("PossiblyUndefinedIntArrayOffset");
|
|
|
|
|
|
|
|
// $this->testConfig->ensure_array_int_offsets_exist = true;
|
|
|
|
|
|
|
|
// $file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
// $this->addFile(
|
|
|
|
// $file_path,
|
|
|
|
// '<?php
|
|
|
|
// class Foo
|
|
|
|
// {
|
|
|
|
// /** @var array<int, int> */
|
|
|
|
// public const CONST = [1, 2, 3];
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * @param key-of<self::CONST> $key
|
|
|
|
// */
|
|
|
|
// public function bar(int $key): int
|
|
|
|
// {
|
|
|
|
// return static::CONST[$key];
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// '
|
|
|
|
// );
|
|
|
|
|
|
|
|
// $this->analyzeFile($file_path, new Context());
|
|
|
|
// }
|
|
|
|
|
2022-01-31 19:23:29 +01:00
|
|
|
public function testUseObjectConstant(): void
|
|
|
|
{
|
|
|
|
$file1 = getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'file1.php';
|
|
|
|
$file2 = getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'file2.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file1,
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
final class Bar {}
|
|
|
|
const bar = new Bar();
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2022-01-31 19:23:29 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file2,
|
|
|
|
'<?php
|
|
|
|
namespace Baz;
|
|
|
|
|
|
|
|
use Foo\Bar;
|
|
|
|
use const Foo\bar;
|
|
|
|
|
|
|
|
require("tests/file1.php");
|
|
|
|
|
|
|
|
function bar(): Bar
|
|
|
|
{
|
|
|
|
return bar;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2022-01-31 19:23:29 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file1, new Context());
|
|
|
|
$this->analyzeFile($file2, new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-01-15 21:58:40 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'constantInFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
useTest();
|
|
|
|
const TEST = 2;
|
2017-12-06 06:05:01 +01:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function useTest(): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return TEST;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'constantInClosure' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
const TEST = 2;
|
2017-12-06 06:05:01 +01:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
$useTest = function(): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return TEST;
|
|
|
|
};
|
2017-05-27 02:05:57 +02:00
|
|
|
$useTest();',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'constantDefinedInFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function defineConstant() {
|
|
|
|
define("CONSTANT", 1);
|
|
|
|
}
|
2017-12-06 06:05:01 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
defineConstant();
|
2017-12-06 06:05:01 +01:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
echo CONSTANT;',
|
|
|
|
],
|
2017-12-06 06:05:01 +01:00
|
|
|
'magicConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-06 06:05:01 +01:00
|
|
|
$a = __LINE__;
|
|
|
|
$b = __file__;',
|
|
|
|
'assertions' => [
|
2023-06-17 12:57:06 +02:00
|
|
|
'$a' => 'int<1, max>',
|
2017-12-06 06:05:01 +01:00
|
|
|
'$b' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2017-12-19 15:48:01 +01:00
|
|
|
'getClassConstantValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-19 15:48:01 +01:00
|
|
|
class A {
|
|
|
|
const B = [0, 1, 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = A::B[1];',
|
|
|
|
],
|
2018-01-25 16:47:15 +01:00
|
|
|
'staticConstEval' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-25 16:47:15 +01:00
|
|
|
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' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedArgument'],
|
2018-01-25 16:47:15 +01:00
|
|
|
],
|
2018-02-08 02:26:26 +01:00
|
|
|
'undefinedConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-08 02:26:26 +01:00
|
|
|
switch (rand(0, 50)) {
|
|
|
|
case FORTY: // Observed a valid UndeclaredConstant warning
|
|
|
|
$x = "value";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$x = "other";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $x;',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['UndefinedConstant'],
|
2018-02-08 02:26:26 +01:00
|
|
|
],
|
2018-05-03 19:20:42 +02:00
|
|
|
'suppressUndefinedClassConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-03 19:20:42 +02:00
|
|
|
class C {}
|
|
|
|
|
|
|
|
/** @psalm-suppress UndefinedConstant */
|
|
|
|
$a = POTATO;
|
|
|
|
|
|
|
|
/** @psalm-suppress UndefinedConstant */
|
|
|
|
$a = C::POTATO;',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment'],
|
2018-05-03 19:20:42 +02:00
|
|
|
],
|
2018-05-10 19:01:55 +02:00
|
|
|
'hardToDefineClassConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-10 19:01:55 +02:00
|
|
|
class A {
|
2018-05-10 20:12:50 +02:00
|
|
|
const C = [
|
|
|
|
self::B => 4,
|
|
|
|
"name" => 3
|
|
|
|
];
|
2018-05-10 19:01:55 +02:00
|
|
|
|
2018-05-10 20:12:50 +02:00
|
|
|
const B = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo A::C[4];',
|
|
|
|
],
|
|
|
|
'sameNamedConstInOtherClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-10 20:12:50 +02:00
|
|
|
class B {
|
|
|
|
const B = 4;
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
const B = "four";
|
|
|
|
const C = [
|
|
|
|
B::B => "one",
|
|
|
|
];
|
2018-05-10 19:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
echo A::C[4];',
|
|
|
|
],
|
2018-05-12 05:14:44 +02:00
|
|
|
'onlyMatchingConstantOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-12 05:14:44 +02:00
|
|
|
class A {
|
|
|
|
const KEYS = ["one", "two", "three"];
|
|
|
|
const ARR = [
|
|
|
|
"one" => 1,
|
|
|
|
"two" => 2
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (A::KEYS as $key) {
|
|
|
|
if (isset(A::ARR[$key])) {
|
|
|
|
echo A::ARR[$key];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-02-13 22:38:58 +01:00
|
|
|
'stringArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-13 22:38:58 +01:00
|
|
|
class A {
|
|
|
|
const C = [
|
|
|
|
"a" => 1,
|
|
|
|
"b" => 2,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(string $s) : void {
|
|
|
|
if (!isset(A::C[$s])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($s === "Hello") {}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-12 17:17:41 +02:00
|
|
|
'noExceptionsOnMixedArrayKey' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-12 17:17:41 +02:00
|
|
|
function finder(string $id) : ?object {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return new B();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
private const TYPES = [
|
|
|
|
"type1" => A::class,
|
|
|
|
"type2" => B::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function bar(array $data): void
|
|
|
|
{
|
|
|
|
if (!isset(self::TYPES[$data["type"]])) {
|
|
|
|
throw new \InvalidArgumentException("Unknown type");
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = self::TYPES[$data["type"]];
|
|
|
|
|
|
|
|
$ret = finder($data["id"]);
|
|
|
|
|
|
|
|
if (!$ret || !$ret instanceof $class) {
|
|
|
|
throw new \InvalidArgumentException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedArgument', 'MixedArrayOffset', 'MixedAssignment'],
|
2018-05-12 17:17:41 +02:00
|
|
|
],
|
2018-06-28 03:53:25 +02:00
|
|
|
'lateConstantResolution' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-28 03:53:25 +02:00
|
|
|
class A {
|
|
|
|
const FOO = "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
const BAR = [
|
|
|
|
A::FOO
|
|
|
|
];
|
|
|
|
const BAR2 = A::FOO;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = B::BAR[0];
|
|
|
|
$b = B::BAR2;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
'$b' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2021-04-08 03:56:35 +02:00
|
|
|
'lateConstantResolutionParentArrayPlus' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-04-08 03:56:35 +02:00
|
|
|
class A {
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: true, ...} */
|
2021-04-08 03:56:35 +02:00
|
|
|
public const ARR = ["a" => true];
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: true, b: true, ...} */
|
2021-04-08 03:56:35 +02:00
|
|
|
public const ARR = parent::ARR + ["b" => true];
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public const ARR = parent::ARR + ["c" => true];
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @param array{a: true, b: true, c: true} $arg */
|
2021-04-08 03:56:35 +02:00
|
|
|
function foo(array $arg): void {}
|
|
|
|
foo(C::ARR);
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'lateConstantResolutionParentArraySpread' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-04-08 03:56:35 +02:00
|
|
|
class A {
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var list{"a", ...} */
|
2021-04-08 03:56:35 +02:00
|
|
|
public const ARR = ["a"];
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var list{"a", "b", ...} */
|
2021-04-08 03:56:35 +02:00
|
|
|
public const ARR = [...parent::ARR, "b"];
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public const ARR = [...parent::ARR, "c"];
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @param array{"a", "b", "c"} $arg */
|
2021-04-08 03:56:35 +02:00
|
|
|
function foo(array $arg): void {}
|
|
|
|
foo(C::ARR);
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'lateConstantResolutionParentStringConcat' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-04-08 03:56:35 +02:00
|
|
|
class A {
|
2022-01-20 23:51:55 +01:00
|
|
|
/** @var non-empty-string */
|
2021-04-08 03:56:35 +02:00
|
|
|
public const STR = "a";
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2022-01-20 23:51:55 +01:00
|
|
|
/** @var non-empty-string */
|
2021-04-08 03:56:35 +02:00
|
|
|
public const STR = parent::STR . "b";
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
2022-01-20 23:51:55 +01:00
|
|
|
/** @var non-empty-string */
|
2021-04-08 03:56:35 +02:00
|
|
|
public const STR = parent::STR . "c";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param "abc" $foo */
|
|
|
|
function foo(string $foo): void {}
|
|
|
|
foo(C::STR);
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'lateConstantResolutionSpreadEmptyArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-04-08 03:56:35 +02:00
|
|
|
class A {
|
|
|
|
public const ARR = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public const ARR = [...parent::ARR];
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public const ARR = [...parent::ARR];
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:37:47 +02:00
|
|
|
/** @param array<never, never> $arg */
|
2021-04-08 03:56:35 +02:00
|
|
|
function foo(array $arg): void {}
|
|
|
|
foo(C::ARR);
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'classConstConcatEol' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-04-08 03:56:35 +02:00
|
|
|
class Foo {
|
|
|
|
public const BAR = "bar" . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo = Foo::BAR;
|
|
|
|
',
|
|
|
|
'assertions' => ['$foo' => 'string'],
|
|
|
|
],
|
2021-08-16 21:20:20 +02:00
|
|
|
'dynamicClassConstFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-18 20:22:07 +02:00
|
|
|
class Foo
|
2021-08-16 21:20:20 +02:00
|
|
|
{
|
|
|
|
public const BAR = "bar";
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo = new Foo();
|
|
|
|
$_trace = $foo::BAR;',
|
2022-01-16 16:07:56 +01:00
|
|
|
'assertions' => ['$_trace===' => "'bar'"],
|
2021-08-16 21:20:20 +02:00
|
|
|
],
|
2021-08-18 20:22:07 +02:00
|
|
|
'unsafeInferenceClassConstFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-18 20:22:07 +02:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public const BAR = "bar";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Foo $foo */
|
|
|
|
$foo = new stdClass();
|
|
|
|
$_trace = $foo::BAR;',
|
|
|
|
'assertions' => ['$_trace' => 'mixed'],
|
|
|
|
],
|
|
|
|
'FinalInferenceClassConstFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-18 20:22:07 +02:00
|
|
|
final class Foo
|
|
|
|
{
|
|
|
|
public const BAR = "bar";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Foo $foo */
|
|
|
|
$foo = new stdClass();
|
|
|
|
$_trace = $foo::BAR;',
|
2022-01-16 16:07:56 +01:00
|
|
|
'assertions' => ['$_trace===' => "'bar'"],
|
2021-08-18 20:22:07 +02:00
|
|
|
],
|
2021-08-17 18:51:59 +02:00
|
|
|
'dynamicClassConstFetchClassString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-18 20:22:07 +02:00
|
|
|
class C {
|
2021-08-17 18:51:59 +02:00
|
|
|
public const CC = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = C::class;
|
|
|
|
$d = $c::CC;',
|
|
|
|
'assertions' => ['$d===' => '1'],
|
|
|
|
],
|
2018-07-06 19:35:36 +02:00
|
|
|
'allowConstCheckForDifferentPlatforms' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-06 19:35:36 +02:00
|
|
|
if ("phpdbg" === \PHP_SAPI) {}',
|
|
|
|
],
|
2018-07-10 23:40:34 +02:00
|
|
|
'stdinout' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-10 23:40:34 +02:00
|
|
|
echo fread(STDIN, 100);
|
|
|
|
fwrite(STDOUT, "asd");
|
2019-03-23 19:27:54 +01:00
|
|
|
fwrite(STDERR, "zcx");',
|
2018-07-10 23:40:34 +02:00
|
|
|
],
|
2018-08-21 04:25:10 +02:00
|
|
|
'classStringArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-08-21 04:25:10 +02:00
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
const C = [
|
|
|
|
A::class => 1,
|
|
|
|
B::class => 2,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string $s
|
|
|
|
*/
|
|
|
|
function foo(string $s) : void {
|
|
|
|
if (isset(C[$s])) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-09-09 17:18:20 +02:00
|
|
|
'resolveClassConstToCurrentClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-09-09 17:18:20 +02:00
|
|
|
interface I {
|
2021-12-14 00:28:15 +01:00
|
|
|
/** @var string|array */
|
2018-09-09 17:18:20 +02:00
|
|
|
public const C = "a";
|
|
|
|
|
|
|
|
public function getC(): string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A implements I {
|
|
|
|
public function getC(): string {
|
|
|
|
return self::C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public const C = [5];
|
2018-11-06 03:57:36 +01:00
|
|
|
|
2018-09-09 17:18:20 +02:00
|
|
|
public function getA(): array {
|
|
|
|
return self::C;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-28 04:50:36 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
2018-09-09 17:18:20 +02:00
|
|
|
],
|
2019-01-02 19:46:46 +01:00
|
|
|
'resolveCalculatedConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-02 19:46:46 +01:00
|
|
|
interface Types {
|
|
|
|
public const TWO = "two";
|
|
|
|
}
|
|
|
|
|
|
|
|
interface A {
|
|
|
|
public const TYPE_ONE = "one";
|
|
|
|
public const TYPE_TWO = Types::TWO;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A {
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
echo self::TYPE_ONE;
|
|
|
|
echo self::TYPE_TWO;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedArgument'],
|
2019-01-02 19:46:46 +01:00
|
|
|
],
|
2019-01-13 17:54:39 +01:00
|
|
|
'arrayAccessAfterIsset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-13 17:54:39 +01:00
|
|
|
class C {
|
|
|
|
const A = [
|
|
|
|
"b" => ["c" => false],
|
|
|
|
"c" => ["c" => true],
|
|
|
|
"d" => ["c" => true]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
$s = "b";
|
|
|
|
|
|
|
|
if (isset(C::A[$s]["c"]) && C::A[$s]["c"] === false) {}',
|
|
|
|
],
|
2019-01-20 15:52:26 +01:00
|
|
|
'namespacedConstantInsideClosure' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-20 15:52:26 +01:00
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
const FOO_BAR = 1;
|
|
|
|
|
|
|
|
function foo(): \Closure {
|
|
|
|
return function (): int {
|
|
|
|
return FOO_BAR;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo2(): int {
|
|
|
|
return FOO_BAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = function (): \Closure {
|
|
|
|
return function (): int {
|
|
|
|
return FOO_BAR;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$b = function (): int {
|
|
|
|
return FOO_BAR;
|
|
|
|
};',
|
|
|
|
],
|
|
|
|
'rootConstantReferencedInNamespace' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-20 15:52:26 +01:00
|
|
|
namespace Foo;
|
|
|
|
echo DIRECTORY_SEPARATOR;',
|
|
|
|
],
|
2019-02-22 00:19:12 +01:00
|
|
|
'constantDefinedInRootNamespace' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-18 19:17:08 +01:00
|
|
|
namespace {
|
2019-02-22 00:19:12 +01:00
|
|
|
define("ns1\\cons1", 0);
|
2019-02-18 19:17:08 +01:00
|
|
|
|
2019-02-22 00:19:12 +01:00
|
|
|
echo \ns1\cons1;
|
|
|
|
echo ns1\cons1;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'constantDynamicallyDefinedInNamespaceReferencedInSame' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-18 19:17:08 +01:00
|
|
|
namespace ns2 {
|
2019-02-22 00:19:12 +01:00
|
|
|
define(__NAMESPACE__."\\cons2", 0);
|
2019-02-18 19:17:08 +01:00
|
|
|
|
2019-02-22 00:19:12 +01:00
|
|
|
echo \ns2\cons2;
|
|
|
|
echo cons2;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'constantDynamicallyDefinedInNamespaceReferencedInRoot' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-22 00:19:12 +01:00
|
|
|
namespace ns2 {
|
|
|
|
define(__NAMESPACE__."\\cons2", 0);
|
2019-02-18 19:17:08 +01:00
|
|
|
}
|
2019-02-22 00:19:12 +01:00
|
|
|
namespace {
|
|
|
|
echo \ns2\cons2;
|
|
|
|
echo ns2\cons2;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'constantExplicitlyDefinedInNamespaceReferencedInSame' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-22 00:19:12 +01:00
|
|
|
namespace ns2 {
|
|
|
|
define("ns2\\cons2", 0);
|
|
|
|
|
|
|
|
echo \ns2\cons2;
|
|
|
|
echo cons2;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'constantExplicitlyDefinedInNamespaceReferencedInRoot' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-18 19:17:08 +01:00
|
|
|
namespace ns2 {
|
2019-02-22 00:19:12 +01:00
|
|
|
define("ns2\\cons2", 0);
|
2019-02-18 19:17:08 +01:00
|
|
|
}
|
|
|
|
namespace {
|
2019-02-22 00:19:12 +01:00
|
|
|
echo \ns2\cons2;
|
|
|
|
echo ns2\cons2;
|
2019-02-18 19:17:08 +01:00
|
|
|
}',
|
|
|
|
],
|
2019-03-03 22:43:24 +01:00
|
|
|
'allowConstantToBeDefinedInNamespaceNadReferenced' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-03 22:43:24 +01:00
|
|
|
namespace ns;
|
|
|
|
function func(): void {}
|
|
|
|
define(__NAMESPACE__."\\cons", 0);
|
2019-03-23 19:27:54 +01:00
|
|
|
cons;',
|
2019-03-03 22:43:24 +01:00
|
|
|
],
|
2019-08-15 16:41:24 +02:00
|
|
|
'staticConstantInsideFinalClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-15 16:41:24 +02:00
|
|
|
final class A {
|
|
|
|
public const STRING = "1,2,3";
|
|
|
|
public static function foo(): void {
|
|
|
|
print_r(explode(",", static::STRING));
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-08-15 16:41:24 +02:00
|
|
|
],
|
2019-08-18 00:01:24 +02:00
|
|
|
'allowChecksAfterDefined' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-18 00:01:24 +02:00
|
|
|
class A {
|
|
|
|
private const STRING = "x";
|
|
|
|
|
2020-09-05 06:35:48 +02:00
|
|
|
public static function bar(string $s) : bool {
|
|
|
|
return !defined("FOO") && strpos($s, self::STRING) === 0;
|
2019-08-18 00:01:24 +02:00
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-08-18 00:01:24 +02:00
|
|
|
],
|
2019-09-14 20:26:31 +02:00
|
|
|
'resolveOutOfOrderClassConstants' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-14 20:26:31 +02:00
|
|
|
const cons1 = 0;
|
|
|
|
|
|
|
|
class Clazz {
|
|
|
|
const cons2 = cons1;
|
|
|
|
const cons3 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo cons1;
|
|
|
|
echo Clazz::cons2;
|
2022-12-18 17:15:15 +01:00
|
|
|
echo Clazz::cons3;',
|
2019-09-14 20:26:31 +02:00
|
|
|
],
|
|
|
|
'evenMoreOutOfOrderConstants' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-14 20:26:31 +02:00
|
|
|
class A {
|
|
|
|
const X = self::Y;
|
|
|
|
const Y = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
}
|
|
|
|
|
|
|
|
const Z = C::X;
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
const Z = self::X;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-14 20:26:31 +02:00
|
|
|
],
|
2019-09-17 17:29:41 +02:00
|
|
|
'supportTernaries' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-17 17:29:41 +02:00
|
|
|
const cons1 = true;
|
|
|
|
|
|
|
|
class Clazz {
|
2020-03-16 03:23:31 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress RedundantCondition
|
|
|
|
*/
|
2019-09-17 17:29:41 +02:00
|
|
|
const cons2 = (cons1) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo Clazz::cons2;',
|
2019-12-04 19:33:50 +01:00
|
|
|
],
|
|
|
|
'classConstantClassReferencedLazily' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-04 19:33:50 +01:00
|
|
|
/** @return array<string, int> */
|
|
|
|
function getMap(): array {
|
|
|
|
return Mapper::MAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Mapper {
|
|
|
|
public const MAP = [
|
|
|
|
Foo::class => self::A,
|
|
|
|
Foo::BAR => self::A,
|
|
|
|
];
|
|
|
|
|
|
|
|
private const A = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
public const BAR = "bar";
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-12-04 19:33:50 +01:00
|
|
|
],
|
2020-01-14 18:13:41 +01:00
|
|
|
'resolveConstArrayAsList' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-15 17:54:01 +01:00
|
|
|
class Test1 {
|
|
|
|
const VALUES = [
|
2020-01-14 18:13:41 +01:00
|
|
|
"all",
|
|
|
|
"own"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:54:01 +01:00
|
|
|
class Credentials {
|
|
|
|
const ALL = "all";
|
|
|
|
const OWN = "own";
|
|
|
|
const NONE = "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
class Test2 {
|
|
|
|
const VALUES = [
|
|
|
|
Credentials::ALL,
|
|
|
|
Credentials::OWN
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-14 18:13:41 +01:00
|
|
|
/**
|
|
|
|
* @psalm-param list<"all"|"own"|"mine"> $value
|
|
|
|
*/
|
|
|
|
function test($value): void {
|
|
|
|
print_r($value);
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:54:01 +01:00
|
|
|
test(Test1::VALUES);
|
2022-12-18 17:15:15 +01:00
|
|
|
test(Test2::VALUES);',
|
2020-01-14 18:13:41 +01:00
|
|
|
],
|
2020-01-14 22:56:09 +01:00
|
|
|
'resolveConstantFetchViaFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-14 22:56:09 +01:00
|
|
|
const FOO = 1;
|
2022-12-18 17:15:15 +01:00
|
|
|
echo \constant("FOO");',
|
2020-01-14 22:56:09 +01:00
|
|
|
],
|
2020-01-25 16:38:23 +01:00
|
|
|
'tooLongClassConstArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-25 16:38:23 +01:00
|
|
|
class MyTest {
|
|
|
|
const LOOKUP = [
|
|
|
|
"A00" => null,
|
|
|
|
"A01" => null,
|
|
|
|
"A02" => null,
|
|
|
|
"A03" => null,
|
|
|
|
"A04" => null,
|
|
|
|
"A05" => null,
|
|
|
|
"A06" => null,
|
|
|
|
"A07" => null,
|
|
|
|
"A010" => null,
|
|
|
|
"A011" => null,
|
|
|
|
"A012" => null,
|
|
|
|
"A013" => null,
|
|
|
|
"A014" => null,
|
|
|
|
"A015" => null,
|
|
|
|
"A016" => null,
|
|
|
|
"A017" => null,
|
|
|
|
"A020" => null,
|
|
|
|
"A021" => null,
|
|
|
|
"A022" => null,
|
|
|
|
"A023" => null,
|
|
|
|
"A024" => null,
|
|
|
|
"A025" => null,
|
|
|
|
"A026" => null,
|
|
|
|
"A027" => null,
|
|
|
|
"A030" => null,
|
|
|
|
"A031" => null,
|
|
|
|
"A032" => null,
|
|
|
|
"A033" => null,
|
|
|
|
"A034" => null,
|
|
|
|
"A035" => null,
|
|
|
|
"A036" => null,
|
|
|
|
"A037" => null,
|
|
|
|
"A040" => null,
|
|
|
|
"A041" => null,
|
|
|
|
"A042" => null,
|
|
|
|
"A043" => null,
|
|
|
|
"A044" => null,
|
|
|
|
"A045" => null,
|
|
|
|
"A046" => null,
|
|
|
|
"A047" => null,
|
|
|
|
"A050" => null,
|
|
|
|
"A051" => null,
|
|
|
|
"A052" => null,
|
|
|
|
"A053" => null,
|
|
|
|
"A054" => null,
|
|
|
|
"A055" => null,
|
|
|
|
"A056" => null,
|
|
|
|
"A057" => null,
|
|
|
|
"A060" => null,
|
|
|
|
"A061" => null,
|
|
|
|
"A062" => null,
|
|
|
|
"A063" => null,
|
|
|
|
"A064" => self::SUCCEED,
|
|
|
|
"A065" => self::FAIL,
|
|
|
|
];
|
|
|
|
|
|
|
|
const SUCCEED = "SUCCEED";
|
|
|
|
const FAIL = "FAIL";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $code
|
|
|
|
*/
|
|
|
|
public static function will_succeed($code) : bool {
|
|
|
|
// False positive TypeDoesNotContainType - string(SUCCEED) cannot be identical to null
|
|
|
|
// This seems to happen because the array has a lot of entries.
|
|
|
|
return (self::LOOKUP[strtoupper($code)] ?? null) === self::SUCCEED;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-02-07 16:47:42 +01:00
|
|
|
],
|
2020-02-10 21:30:06 +01:00
|
|
|
'keyOf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 16:47:42 +01:00
|
|
|
class A {
|
|
|
|
const C = [
|
|
|
|
1 => "a",
|
|
|
|
2 => "b",
|
|
|
|
3 => "c"
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param key-of<A::C> $i
|
|
|
|
*/
|
|
|
|
public static function foo(int $i) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::foo(1);
|
|
|
|
A::foo(2);
|
|
|
|
A::foo(3);',
|
|
|
|
],
|
|
|
|
'valueOf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 16:47:42 +01:00
|
|
|
class A {
|
|
|
|
const C = [
|
|
|
|
1 => "a",
|
|
|
|
2 => "b",
|
|
|
|
3 => "c"
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param value-of<A::C> $j
|
|
|
|
*/
|
|
|
|
public static function bar(string $j) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::bar("a");
|
|
|
|
A::bar("b");
|
|
|
|
A::bar("c");',
|
|
|
|
],
|
|
|
|
'valueOfDefault' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 16:47:42 +01:00
|
|
|
class A {
|
|
|
|
const C = [
|
|
|
|
1 => "a",
|
|
|
|
2 => "b",
|
|
|
|
3 => "c"
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var value-of<self::C>
|
|
|
|
*/
|
|
|
|
public $foo = "a";
|
|
|
|
}',
|
2020-01-25 16:38:23 +01:00
|
|
|
],
|
2020-02-07 16:54:57 +01:00
|
|
|
'wildcardEnum' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 16:54:57 +01:00
|
|
|
class A {
|
|
|
|
const C_1 = 1;
|
|
|
|
const C_2 = 2;
|
|
|
|
const C_3 = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param self::C_* $i
|
|
|
|
*/
|
|
|
|
public static function foo(int $i) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::foo(1);
|
|
|
|
A::foo(2);
|
|
|
|
A::foo(3);',
|
|
|
|
],
|
|
|
|
'wildcardEnumAnyConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 16:54:57 +01:00
|
|
|
class A {
|
|
|
|
const C_1 = 1;
|
|
|
|
const C_2 = 2;
|
|
|
|
const C_3 = 3;
|
|
|
|
const D_4 = 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param self::* $i
|
|
|
|
*/
|
|
|
|
public static function foo(int $i) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::foo(1);
|
|
|
|
A::foo(2);
|
|
|
|
A::foo(3);
|
|
|
|
A::foo(A::D_4);',
|
|
|
|
],
|
2020-05-30 22:55:18 +02:00
|
|
|
'wildcardEnumAnyTemplateExtendConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-30 22:55:18 +02:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*/
|
|
|
|
interface AInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param T $i
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
public function foo($i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @implements AInterface<A::*>
|
|
|
|
*/
|
|
|
|
class A implements AInterface {
|
|
|
|
const C_1 = 1;
|
|
|
|
const C_2 = 2;
|
|
|
|
const C_3 = 3;
|
|
|
|
const D_4 = 4;
|
|
|
|
|
|
|
|
public function foo($i)
|
|
|
|
{
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo(1);
|
|
|
|
$a->foo(2);
|
|
|
|
$a->foo(3);
|
|
|
|
$a->foo(A::D_4);',
|
|
|
|
],
|
2020-02-10 21:30:06 +01:00
|
|
|
'wildcardVarAndReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-10 21:30:06 +01:00
|
|
|
class Numbers {
|
|
|
|
public const ONE = 1;
|
|
|
|
public const TWO = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Number {
|
|
|
|
/**
|
|
|
|
* @var Numbers::*
|
|
|
|
*/
|
|
|
|
private $number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Numbers::* $number
|
|
|
|
*/
|
|
|
|
public function __construct($number) {
|
|
|
|
$this->number = $number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Numbers::*
|
|
|
|
*/
|
|
|
|
public function get(): int {
|
|
|
|
return $this->number;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-02-10 21:30:06 +01:00
|
|
|
],
|
2020-02-17 23:58:36 +01:00
|
|
|
'lowercaseStringAccessClassConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-17 23:58:36 +01:00
|
|
|
class A {
|
|
|
|
const C = [
|
|
|
|
"a" => 1,
|
|
|
|
"b" => 2,
|
|
|
|
"c" => 3
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param lowercase-string $s
|
|
|
|
*/
|
|
|
|
function foo(string $s, string $t) : void {
|
|
|
|
echo A::C[$t];
|
|
|
|
echo A::C[$s];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-02-17 23:58:36 +01:00
|
|
|
],
|
2020-02-21 21:56:30 +01:00
|
|
|
'getClassConstantOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-21 21:56:30 +01:00
|
|
|
class C {
|
|
|
|
private const A = [ 0 => "string" ];
|
|
|
|
private const B = self::A[0];
|
|
|
|
|
|
|
|
public function foo(): string {
|
|
|
|
return self::B;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-02-21 21:56:30 +01:00
|
|
|
],
|
2020-03-09 05:54:26 +01:00
|
|
|
'bitwiseOrClassConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-09 05:54:26 +01:00
|
|
|
class X {
|
|
|
|
public const A = 1;
|
|
|
|
public const B = 2;
|
|
|
|
public const C = self::A | self::B;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = X::C;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-03-09 05:54:26 +01:00
|
|
|
'$c' => 'int',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2020-03-09 05:54:26 +01:00
|
|
|
],
|
2022-08-04 08:52:43 +02:00
|
|
|
'bitwiseAndClassConstant' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class X {
|
|
|
|
public const A = 1;
|
|
|
|
public const B = 2;
|
|
|
|
public const C = self::A & self::B;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = X::C;',
|
|
|
|
'assertions' => [
|
|
|
|
'$c' => 'int',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2022-08-04 08:52:43 +02:00
|
|
|
],
|
|
|
|
'bitwiseXorClassConstant' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class X {
|
|
|
|
public const A = 1;
|
|
|
|
public const B = 2;
|
|
|
|
public const C = self::A ^ self::B;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = X::C;',
|
|
|
|
'assertions' => [
|
|
|
|
'$c' => 'int',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2022-08-04 08:52:43 +02:00
|
|
|
],
|
|
|
|
'bitwiseNotClassConstant' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class X {
|
|
|
|
public const A = ~0;
|
|
|
|
public const B = ~"aa";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = X::A;
|
|
|
|
$b = X::B;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'string',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2022-08-04 08:52:43 +02:00
|
|
|
],
|
|
|
|
'booleanNotClassConstant' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class X {
|
|
|
|
public const A = !true;
|
|
|
|
public const B = !false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = X::A;
|
|
|
|
$b = X::B;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'false',
|
|
|
|
'$b' => 'true',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2022-08-04 08:52:43 +02:00
|
|
|
],
|
2020-03-29 15:47:29 +02:00
|
|
|
'protectedClassConstantAccessibilitySameNameInChild' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-29 15:47:29 +02:00
|
|
|
class A {
|
2021-12-14 00:28:15 +01:00
|
|
|
/** @var int<1,max> */
|
2020-03-29 15:47:29 +02:00
|
|
|
protected const A = 1;
|
|
|
|
|
|
|
|
public static function test(): void {
|
|
|
|
echo B::A;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
protected const A = 2;
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
A::test();',
|
2020-03-29 15:47:29 +02:00
|
|
|
],
|
2020-04-18 18:39:00 +02:00
|
|
|
'referenceClassConstantWithSelf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-18 18:39:00 +02:00
|
|
|
abstract class A {
|
2021-12-14 00:28:15 +01:00
|
|
|
/** @var array<non-empty-string, non-empty-string> */
|
2020-04-18 18:39:00 +02:00
|
|
|
public const KEYS = [];
|
2021-12-14 00:28:15 +01:00
|
|
|
/** @var array<non-empty-string, non-empty-string> */
|
2020-04-18 18:39:00 +02:00
|
|
|
public const VALUES = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public const VALUES = [\'there\' => self::KEYS[\'hi\']];
|
|
|
|
public const KEYS = [\'hi\' => CONSTANTS::THERE];
|
|
|
|
}
|
|
|
|
|
|
|
|
class CONSTANTS {
|
|
|
|
public const THERE = \'there\';
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
echo B::VALUES["there"];',
|
2020-04-18 18:39:00 +02:00
|
|
|
],
|
2020-05-29 15:09:53 +02:00
|
|
|
'internalConstWildcard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-29 15:09:53 +02:00
|
|
|
/**
|
|
|
|
* @psalm-param \PDO::PARAM_* $type
|
|
|
|
*/
|
2022-12-18 17:15:15 +01:00
|
|
|
function param(int $type): void {}',
|
2020-05-29 15:09:53 +02:00
|
|
|
],
|
2020-06-07 02:02:14 +02:00
|
|
|
'templatedConstantInType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-07 02:02:14 +02:00
|
|
|
/**
|
|
|
|
* @template T of (self::READ_UNCOMMITTED|self::READ_COMMITTED|self::REPEATABLE_READ|self::SERIALIZABLE)
|
|
|
|
*/
|
|
|
|
final class TransactionIsolationLevel {
|
|
|
|
private const READ_UNCOMMITTED = \'read uncommitted\';
|
|
|
|
private const READ_COMMITTED = \'read committed\';
|
|
|
|
private const REPEATABLE_READ = \'repeatable read\';
|
|
|
|
private const SERIALIZABLE = \'serializable\';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-var T
|
|
|
|
*/
|
|
|
|
private string $level;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-param T $level
|
|
|
|
*/
|
|
|
|
private function __construct(string $level)
|
|
|
|
{
|
|
|
|
$this->level = $level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-return self<self::READ_UNCOMMITTED>
|
|
|
|
*/
|
|
|
|
public static function readUncommitted(): self
|
|
|
|
{
|
|
|
|
return new self(self::READ_UNCOMMITTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-return self<self::READ_COMMITTED>
|
|
|
|
*/
|
|
|
|
public static function readCommitted(): self
|
|
|
|
{
|
|
|
|
return new self(self::READ_COMMITTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-return self<self::REPEATABLE_READ>
|
|
|
|
*/
|
|
|
|
public static function repeatableRead(): self
|
|
|
|
{
|
|
|
|
return new self(self::REPEATABLE_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-return self<self::SERIALIZABLE>
|
|
|
|
*/
|
|
|
|
public static function serializable(): self
|
|
|
|
{
|
|
|
|
return new self(self::SERIALIZABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-return T
|
|
|
|
*/
|
|
|
|
public function toString(): string
|
|
|
|
{
|
|
|
|
return $this->level;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-06-07 02:02:14 +02:00
|
|
|
],
|
2020-06-18 15:48:51 +02:00
|
|
|
'dirAndFileInConstInitializersAreNonEmptyString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-18 15:48:51 +02:00
|
|
|
class C {
|
|
|
|
const DIR = __DIR__;
|
|
|
|
const FILE = __FILE__;
|
|
|
|
}
|
|
|
|
$dir = C::DIR;
|
|
|
|
$file = C::FILE;
|
|
|
|
',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-06-18 15:48:51 +02:00
|
|
|
'$dir===' => 'non-empty-string',
|
|
|
|
'$file===' => 'non-empty-string',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2020-06-18 15:48:51 +02:00
|
|
|
],
|
|
|
|
'lineInConstInitializersIsInt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-18 15:48:51 +02:00
|
|
|
class C {
|
|
|
|
const LINE = __LINE__;
|
|
|
|
}
|
|
|
|
$line = C::LINE;
|
|
|
|
',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2023-06-17 12:57:06 +02:00
|
|
|
'$line' => 'int<1, max>',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2020-06-18 15:48:51 +02:00
|
|
|
],
|
|
|
|
'classMethodTraitAndFunctionInConstInitializersAreStrings' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-18 15:48:51 +02:00
|
|
|
class C {
|
|
|
|
const CLS = __CLASS__;
|
|
|
|
const MTD = __METHOD__;
|
|
|
|
const TRT = __TRAIT__;
|
|
|
|
const FCN = __FUNCTION__;
|
|
|
|
}
|
|
|
|
$cls = C::CLS;
|
|
|
|
$mtd = C::MTD;
|
|
|
|
$trt = C::TRT;
|
|
|
|
$fcn = C::FCN;
|
|
|
|
',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-06-18 15:48:51 +02:00
|
|
|
'$cls' => 'string',
|
|
|
|
'$mtd' => 'string',
|
|
|
|
'$trt' => 'string',
|
|
|
|
'$fcn' => 'string',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2020-06-18 15:48:51 +02:00
|
|
|
],
|
|
|
|
'concatWithMagicInConstInitializersIsNoEmptyString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-18 15:48:51 +02:00
|
|
|
class C {
|
|
|
|
const DIR = __DIR__ . " - dir";
|
|
|
|
const FILE = "file:" . __FILE__;
|
|
|
|
}
|
|
|
|
$dir = C::DIR;
|
|
|
|
$file = C::FILE;
|
|
|
|
',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-06-18 15:48:51 +02:00
|
|
|
'$dir===' => 'non-empty-string',
|
|
|
|
'$file===' => 'non-empty-string',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2021-05-13 18:49:33 +02:00
|
|
|
],
|
|
|
|
'noCrashWithStaticInDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-13 18:49:33 +02:00
|
|
|
class Test {
|
|
|
|
const CONST1 = 1;
|
|
|
|
|
|
|
|
public function test(): void
|
|
|
|
{
|
|
|
|
/** @var static::CONST1 */
|
|
|
|
$a = static::CONST1;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-05-13 18:49:33 +02:00
|
|
|
],
|
2021-07-03 10:01:53 +02:00
|
|
|
'FuncAndMethInAllContexts' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-03 10:01:53 +02:00
|
|
|
/** @return \'getMethInFunc\' */
|
|
|
|
function getMethInFunc(): string{
|
|
|
|
return __METHOD__;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return \'getFuncInFunc\' */
|
|
|
|
function getFuncInFunc(): string{
|
|
|
|
return __FUNCTION__;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A{
|
|
|
|
/** @return \'A::getMethInMeth\' */
|
|
|
|
function getMethInMeth(): string{
|
|
|
|
return __METHOD__;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return \'getFuncInMeth\' */
|
|
|
|
function getFuncInMeth(): string{
|
|
|
|
return __FUNCTION__;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-07-03 10:01:53 +02:00
|
|
|
],
|
2021-08-12 00:49:44 +02:00
|
|
|
'arrayUnpack' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-12 00:49:44 +02:00
|
|
|
class C {
|
|
|
|
const A = [...[...[1]], ...[2]];
|
|
|
|
}
|
|
|
|
$arr = C::A;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'$arr===' => 'list{1, 2}',
|
2021-08-12 00:49:44 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'keysInUnpackedArrayAreReset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-12 00:49:44 +02:00
|
|
|
class C {
|
|
|
|
const A = [...[11 => 2]];
|
|
|
|
}
|
|
|
|
$arr = C::A;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'$arr===' => 'list{2}',
|
2021-08-12 00:49:44 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayKeysSequenceContinuesAfterExplicitIntKey' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-12 00:49:44 +02:00
|
|
|
class C {
|
|
|
|
const A = [5 => "a", "z", 10 => "aa", "zz"];
|
|
|
|
}
|
|
|
|
$arr = C::A;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'$arr===' => "array{10: 'aa', 11: 'zz', 5: 'a', 6: 'z'}",
|
2021-08-12 00:49:44 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayKeysSequenceContinuesAfterNonIntKey' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-12 00:49:44 +02:00
|
|
|
class C {
|
|
|
|
const A = [5 => "a", "zz" => "z", "aa"];
|
|
|
|
}
|
|
|
|
$arr = C::A;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'$arr===' => "array{5: 'a', 6: 'aa', zz: 'z'}",
|
2021-08-12 00:49:44 +02:00
|
|
|
],
|
|
|
|
],
|
2021-10-10 09:24:25 +02:00
|
|
|
'unresolvedConstWithUnaryMinus' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-10-10 09:24:25 +02:00
|
|
|
const K = 5;
|
|
|
|
|
|
|
|
abstract class C6 {
|
|
|
|
|
|
|
|
public const M = [
|
|
|
|
1 => -1,
|
|
|
|
K => 6,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $k
|
|
|
|
*/
|
|
|
|
public static function f(int $k): void {
|
|
|
|
$a = self::M;
|
|
|
|
print_r($a);
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
],
|
2021-11-15 04:56:09 +01:00
|
|
|
'classConstantReferencingEnumCase' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-15 04:56:09 +01:00
|
|
|
enum E {
|
|
|
|
case Z;
|
|
|
|
}
|
|
|
|
class C {
|
|
|
|
public const CC = E::Z;
|
|
|
|
}
|
|
|
|
$c = C::CC;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-12-18 17:15:15 +01:00
|
|
|
'$c===' => 'enum(E::Z)',
|
2021-11-15 04:56:09 +01:00
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2021-11-15 04:56:09 +01:00
|
|
|
],
|
2023-02-24 13:32:04 +01:00
|
|
|
'classConstantArrayWithEnumCaseKey' => [
|
2023-02-23 21:20:34 +01:00
|
|
|
'code' => '<?php
|
|
|
|
enum E {
|
|
|
|
case K1;
|
|
|
|
case K2;
|
|
|
|
}
|
|
|
|
enum BEI: int {
|
|
|
|
case K3 = 1;
|
|
|
|
case K4 = 2;
|
|
|
|
}
|
|
|
|
enum BES: string {
|
|
|
|
case K5 = "a";
|
|
|
|
case K6 = "b";
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
public const C = [
|
2023-02-24 13:32:04 +01:00
|
|
|
BEI::K3->value => "e",
|
|
|
|
BEI::K4->value => 5,
|
2023-02-23 21:20:34 +01:00
|
|
|
E::K1->name => "c",
|
|
|
|
E::K2->name => 3,
|
|
|
|
BEI::K3->name => "d",
|
|
|
|
BEI::K4->name => 4,
|
|
|
|
BES::K5->name => "f",
|
|
|
|
BES::K6->name => 6,
|
|
|
|
BES::K5->value => "g",
|
|
|
|
BES::K6->value => 7,
|
|
|
|
];
|
|
|
|
}
|
2023-02-24 13:32:04 +01:00
|
|
|
$c = A::C;
|
2023-02-23 21:20:34 +01:00
|
|
|
',
|
|
|
|
'assertions' => [
|
2023-02-24 13:32:04 +01:00
|
|
|
'$c===' => "array{1: 'e', 2: 5, K1: 'c', K2: 3, K3: 'd', K4: 4, K5: 'f', K6: 6, a: 'g', b: 7}",
|
2023-02-23 21:20:34 +01:00
|
|
|
],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.2',
|
|
|
|
],
|
|
|
|
'classConstantArrayWithEnumCaseKeyEnumDefinedAfterClass' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class A {
|
|
|
|
public const C = [
|
2023-02-24 13:32:04 +01:00
|
|
|
BEI::K3->value => "e",
|
|
|
|
BEI::K4->value => 5,
|
2023-02-23 21:20:34 +01:00
|
|
|
E::K1->name => "c",
|
|
|
|
E::K2->name => 3,
|
|
|
|
BEI::K3->name => "d",
|
|
|
|
BEI::K4->name => 4,
|
|
|
|
BES::K5->name => "f",
|
|
|
|
BES::K6->name => 6,
|
|
|
|
BES::K5->value => "g",
|
|
|
|
BES::K6->value => 7,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
enum E {
|
|
|
|
case K1;
|
|
|
|
case K2;
|
|
|
|
}
|
|
|
|
enum BEI: int {
|
|
|
|
case K3 = 1;
|
|
|
|
case K4 = 2;
|
|
|
|
}
|
|
|
|
enum BES: string {
|
|
|
|
case K5 = "a";
|
|
|
|
case K6 = "b";
|
|
|
|
}
|
2023-02-24 13:32:04 +01:00
|
|
|
$c = A::C;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$c===' => "array{1: 'e', 2: 5, K1: 'c', K2: 3, K3: 'd', K4: 4, K5: 'f', K6: 6, a: 'g', b: 7}",
|
|
|
|
],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.2',
|
|
|
|
],
|
|
|
|
'classConstantArrayWithEnumCaseKeyNamespaced' => [
|
|
|
|
'code' => '<?php
|
|
|
|
namespace OtherNamespace;
|
|
|
|
enum E: int {
|
|
|
|
case K1 = 1;
|
|
|
|
case K2 = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace UsedNamespace;
|
|
|
|
enum E: int {
|
|
|
|
case K3 = 3;
|
|
|
|
case K4 = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace AliasedNamespace;
|
|
|
|
enum E: int {
|
|
|
|
case K5 = 5;
|
|
|
|
case K6 = 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace SameNamespace;
|
|
|
|
use UsedNamespace\E;
|
|
|
|
use AliasedNamespace\E as E2;
|
|
|
|
|
|
|
|
enum E3: int {
|
|
|
|
case K7 = 7;
|
|
|
|
case K8 = 8;
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
public const C = [
|
|
|
|
\OtherNamespace\E::K1->name => "a",
|
|
|
|
\OtherNamespace\E::K2->name => 10,
|
|
|
|
\OtherNamespace\E::K1->value => "b",
|
|
|
|
\OtherNamespace\E::K2->value => 11,
|
|
|
|
E::K3->name => "c",
|
|
|
|
E::K4->name => 12,
|
|
|
|
E::K3->value => "d",
|
|
|
|
E::K4->value => 13,
|
|
|
|
E2::K5->name => "e",
|
|
|
|
E2::K6->name => 14,
|
|
|
|
E2::K5->value => "f",
|
|
|
|
E2::K6->value => 15,
|
|
|
|
E3::K7->name => "g",
|
|
|
|
E3::K8->name => 16,
|
|
|
|
E3::K7->value => "h",
|
|
|
|
E3::K8->value => 17,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$c = A::C;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$c===' => "array{1: 'b', 2: 11, 3: 'd', 4: 13, 5: 'f', 6: 15, 7: 'h', 8: 17, K1: 'a', K2: 10, K3: 'c', K4: 12, K5: 'e', K6: 14, K7: 'g', K8: 16}",
|
|
|
|
],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.2',
|
|
|
|
],
|
|
|
|
'classConstantArrayWithEnumCaseKeyDirectAccess' => [
|
|
|
|
'code' => '<?php
|
|
|
|
enum E {
|
|
|
|
case K1;
|
|
|
|
case K2;
|
|
|
|
}
|
|
|
|
enum BEI: int {
|
|
|
|
case K3 = 1;
|
|
|
|
case K4 = 2;
|
|
|
|
}
|
|
|
|
enum BES: string {
|
|
|
|
case K5 = "a";
|
|
|
|
case K6 = "b";
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
public const C = [
|
|
|
|
E::K1->name => "c",
|
|
|
|
E::K2->name => 3,
|
|
|
|
BEI::K3->name => "d",
|
|
|
|
BEI::K4->name => 4,
|
|
|
|
BEI::K3->value => "e",
|
|
|
|
BEI::K4->value => 5,
|
|
|
|
BES::K5->name => "f",
|
|
|
|
BES::K6->name => 6,
|
|
|
|
BES::K5->value => "g",
|
|
|
|
BES::K6->value => 7,
|
|
|
|
];
|
|
|
|
}
|
2023-02-23 21:20:34 +01:00
|
|
|
$a = A::C[E::K1->name];
|
|
|
|
$b = A::C[E::K2->name];
|
|
|
|
$c = A::C[BEI::K3->name];
|
|
|
|
$d = A::C[BEI::K4->name];
|
|
|
|
$e = A::C[BEI::K3->value];
|
|
|
|
$f = A::C[BEI::K4->value];
|
|
|
|
$g = A::C[BES::K5->name];
|
|
|
|
$h = A::C[BES::K6->name];
|
|
|
|
$i = A::C[BES::K5->value];
|
|
|
|
$j = A::C[BES::K6->value];
|
|
|
|
$k = A::C["K1"];
|
|
|
|
$l = A::C["K2"];
|
|
|
|
$m = A::C["K3"];
|
|
|
|
$n = A::C["K4"];
|
|
|
|
$o = A::C[1];
|
|
|
|
$p = A::C[2];
|
|
|
|
$q = A::C["K5"];
|
|
|
|
$r = A::C["K6"];
|
|
|
|
$s = A::C["a"];
|
|
|
|
$t = A::C["b"];
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$a===' => "'c'",
|
|
|
|
'$b===' => '3',
|
|
|
|
'$c===' => "'d'",
|
|
|
|
'$d===' => '4',
|
|
|
|
'$e===' => "'e'",
|
|
|
|
'$f===' => '5',
|
|
|
|
'$g===' => "'f'",
|
|
|
|
'$h===' => '6',
|
|
|
|
'$i===' => "'g'",
|
|
|
|
'$j===' => '7',
|
|
|
|
'$k===' => "'c'",
|
|
|
|
'$l===' => '3',
|
|
|
|
'$m===' => "'d'",
|
|
|
|
'$n===' => '4',
|
|
|
|
'$o===' => "'e'",
|
|
|
|
'$p===' => '5',
|
|
|
|
'$q===' => "'f'",
|
|
|
|
'$r===' => '6',
|
|
|
|
'$s===' => "'g'",
|
|
|
|
'$t===' => '7',
|
|
|
|
],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.2',
|
|
|
|
],
|
2023-02-24 13:32:04 +01:00
|
|
|
'classConstantNestedArrayWithEnumCaseKey' => [
|
|
|
|
'code' => '<?php
|
|
|
|
enum E: string {
|
|
|
|
case K1 = "a";
|
|
|
|
case K2 = "b";
|
|
|
|
case K3 = "c";
|
|
|
|
case K4 = "d";
|
|
|
|
case K5 = "e";
|
|
|
|
case K6 = "f";
|
|
|
|
case K7 = "g";
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
public const C = [
|
|
|
|
E::K1->name => [
|
|
|
|
E::K2->name => [
|
|
|
|
E::K3->name => "h",
|
|
|
|
E::K4->name => "i",
|
|
|
|
],
|
|
|
|
E::K5->name => [
|
|
|
|
E::K6->name => "j",
|
|
|
|
E::K7->name => "k",
|
|
|
|
],
|
|
|
|
],
|
|
|
|
E::K1->value => [
|
|
|
|
E::K2->value => [
|
|
|
|
E::K3->value => "l",
|
|
|
|
E::K4->value => "m",
|
|
|
|
],
|
|
|
|
E::K5->value => [
|
|
|
|
E::K6->value => "n",
|
|
|
|
E::K7->value => "o",
|
|
|
|
],
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$c = A::C;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$c===' => "array{K1: array{K2: array{K3: 'h', K4: 'i'}, K5: array{K6: 'j', K7: 'k'}}, a: array{b: array{c: 'l', d: 'm'}, e: array{f: 'n', g: 'o'}}}",
|
|
|
|
],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.2',
|
|
|
|
],
|
2023-02-23 21:20:34 +01:00
|
|
|
'constantArrayWithEnumCaseKey' => [
|
|
|
|
'code' => '<?php
|
|
|
|
enum E {
|
|
|
|
case K1;
|
|
|
|
case K2;
|
|
|
|
}
|
|
|
|
enum BEI: int {
|
|
|
|
case K3 = 1;
|
|
|
|
case K4 = 2;
|
|
|
|
}
|
|
|
|
enum BES: string {
|
|
|
|
case K5 = "a";
|
|
|
|
case K6 = "b";
|
|
|
|
}
|
|
|
|
const C = [
|
|
|
|
E::K1->name => "c",
|
|
|
|
E::K2->name => 3,
|
|
|
|
BEI::K3->name => "d",
|
|
|
|
BEI::K4->name => 4,
|
|
|
|
BEI::K3->value => "e",
|
|
|
|
BEI::K4->value => 5,
|
|
|
|
BES::K5->name => "f",
|
|
|
|
BES::K6->name => 6,
|
|
|
|
BES::K5->value => "g",
|
|
|
|
BES::K6->value => 7,
|
|
|
|
];
|
|
|
|
$a = C[E::K1->name];
|
|
|
|
$b = C[E::K2->name];
|
|
|
|
$c = C[BEI::K3->name];
|
|
|
|
$d = C[BEI::K4->name];
|
|
|
|
$e = C[BEI::K3->value];
|
|
|
|
$f = C[BEI::K4->value];
|
|
|
|
$g = C[BES::K5->name];
|
|
|
|
$h = C[BES::K6->name];
|
|
|
|
$i = C[BES::K5->value];
|
|
|
|
$j = C[BES::K6->value];
|
|
|
|
$k = C["K1"];
|
|
|
|
$l = C["K2"];
|
|
|
|
$m = C["K3"];
|
|
|
|
$n = C["K4"];
|
|
|
|
$o = C[1];
|
|
|
|
$p = C[2];
|
|
|
|
$q = C["K5"];
|
|
|
|
$r = C["K6"];
|
|
|
|
$s = C["a"];
|
|
|
|
$t = C["b"];
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$a===' => "'c'",
|
|
|
|
'$b===' => '3',
|
|
|
|
'$c===' => "'d'",
|
|
|
|
'$d===' => '4',
|
|
|
|
'$e===' => "'e'",
|
|
|
|
'$f===' => '5',
|
|
|
|
'$g===' => "'f'",
|
|
|
|
'$h===' => '6',
|
|
|
|
'$i===' => "'g'",
|
|
|
|
'$j===' => '7',
|
|
|
|
'$k===' => "'c'",
|
|
|
|
'$l===' => '3',
|
|
|
|
'$m===' => "'d'",
|
|
|
|
'$n===' => '4',
|
|
|
|
'$o===' => "'e'",
|
|
|
|
'$p===' => '5',
|
|
|
|
'$q===' => "'f'",
|
|
|
|
'$r===' => '6',
|
|
|
|
'$s===' => "'g'",
|
|
|
|
'$t===' => '7',
|
|
|
|
],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.2',
|
|
|
|
],
|
2023-05-28 17:26:07 +02:00
|
|
|
'constantEnumSelfReference' => [
|
2023-05-30 08:43:54 +02:00
|
|
|
'code' => '<?php
|
2023-05-28 17:26:07 +02:00
|
|
|
enum Bar: string {
|
|
|
|
case A = "a";
|
|
|
|
case B = "b";
|
|
|
|
public const STR = self::A->value . self::B->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
public const CONCAT_STR = "a" . Bar::STR . "e";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.2',
|
|
|
|
],
|
2022-01-04 00:14:08 +01:00
|
|
|
'classConstWithParamOut' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-01-04 00:14:08 +01:00
|
|
|
|
|
|
|
class Reconciler
|
|
|
|
{
|
|
|
|
public const RECONCILIATION_OK = 0;
|
|
|
|
public const RECONCILIATION_EMPTY = 1;
|
|
|
|
|
|
|
|
public static function reconcileKeyedTypes(): void
|
|
|
|
{
|
|
|
|
|
|
|
|
$failed_reconciliation = 0;
|
|
|
|
|
|
|
|
self::boo($failed_reconciliation);
|
|
|
|
|
|
|
|
if ($failed_reconciliation === self::RECONCILIATION_EMPTY) {
|
|
|
|
echo "ici";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param-out Reconciler::RECONCILIATION_* $f */
|
|
|
|
public static function boo(
|
|
|
|
?int &$f = self::RECONCILIATION_OK
|
|
|
|
): void {
|
|
|
|
$f = self::RECONCILIATION_EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Reconciler::reconcileKeyedTypes();
|
|
|
|
',
|
|
|
|
],
|
2022-01-20 23:51:55 +01:00
|
|
|
'selfConstUsesInferredType' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
public const BAR = "bar";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return "bar"
|
|
|
|
*/
|
|
|
|
public function bar(): string
|
|
|
|
{
|
|
|
|
return self::BAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'typedClassConst' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
public const BAR = "bar";
|
|
|
|
|
|
|
|
public function bar(): string
|
|
|
|
{
|
|
|
|
return static::BAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
2022-01-20 23:51:55 +01:00
|
|
|
'classConstSuppress' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @psalm-suppress InvalidConstantAssignmentValue
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public const BAR = "bar";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'spreadEmptyArray' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class A {
|
|
|
|
public const ARR = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param array<never, never> $arg */
|
|
|
|
function foo(array $arg): void {}
|
|
|
|
foo([...A::ARR]);
|
|
|
|
',
|
|
|
|
],
|
2021-12-14 00:28:15 +01:00
|
|
|
'classConstCovariant' => [
|
2022-01-24 20:59:11 +01:00
|
|
|
'code' => '<?php
|
2021-12-14 00:28:15 +01:00
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public const COVARIANT = "";
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public const INVARIANT = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class B extends A {}
|
|
|
|
|
|
|
|
abstract class C extends B {
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const COVARIANT = "foo";
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public const INVARIANT = "";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'overrideClassConstFromInterface' => [
|
2022-01-24 20:59:11 +01:00
|
|
|
'code' => '<?php
|
2021-12-14 00:28:15 +01:00
|
|
|
interface Foo
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Bar extends Foo {}
|
|
|
|
|
|
|
|
class Baz implements Bar
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="foobar";
|
|
|
|
}
|
|
|
|
',
|
2022-01-28 04:50:36 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
2021-12-14 00:28:15 +01:00
|
|
|
],
|
|
|
|
'inheritedConstDoesNotOverride' => [
|
2022-01-24 20:59:11 +01:00
|
|
|
'code' => '<?php
|
2021-12-14 00:28:15 +01:00
|
|
|
interface Foo
|
|
|
|
{
|
|
|
|
public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Bar extends Foo {}
|
|
|
|
',
|
|
|
|
],
|
2022-05-20 21:37:35 +02:00
|
|
|
'classConstsUsingFutureFloatDeclarationWithMultipleLevels' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class Foo {
|
|
|
|
public const BAZ = self::BAR + 1.0;
|
|
|
|
public const BAR = self::FOO + 1.0;
|
|
|
|
public const FOO = 1.0;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
2022-11-05 19:15:46 +01:00
|
|
|
'finalConst' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
final public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baz extends Foo
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = Baz::BAR;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-12-18 17:15:15 +01:00
|
|
|
'$a===' => "'baz'",
|
2022-11-05 19:15:46 +01:00
|
|
|
],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-11-05 19:15:46 +01:00
|
|
|
],
|
|
|
|
'finalConstInterface' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface Foo
|
|
|
|
{
|
|
|
|
final public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baz implements Foo
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = Baz::BAR;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-12-18 17:15:15 +01:00
|
|
|
'$a===' => "'baz'",
|
2022-11-05 19:15:46 +01:00
|
|
|
],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-11-05 19:15:46 +01:00
|
|
|
],
|
2023-02-23 08:33:50 +01:00
|
|
|
'constantTypeRespectsLiteralStringLimit' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
|
|
|
|
class A {
|
|
|
|
const T = 'TEXT';
|
|
|
|
}
|
|
|
|
|
|
|
|
class B
|
|
|
|
{
|
|
|
|
const ARRAY = [
|
|
|
|
'a' => A::T,
|
|
|
|
'b' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$z = B::ARRAY['b'];
|
|
|
|
PHP,
|
|
|
|
],
|
2023-03-12 22:47:42 +01:00
|
|
|
'maxIntegerInArrayKey' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
// PHP_INT_MAX
|
|
|
|
public const S = ['9223372036854775807' => 1];
|
|
|
|
public const I = [9223372036854775807 => 1];
|
|
|
|
|
|
|
|
// PHP_INT_MAX + 1
|
|
|
|
public const SO = ['9223372036854775808' => 1];
|
|
|
|
}
|
|
|
|
$s = A::S;
|
|
|
|
$i = A::I;
|
|
|
|
$so = A::SO;
|
|
|
|
PHP,
|
|
|
|
'assertions' => [
|
|
|
|
'$s===' => 'array{9223372036854775807: 1}',
|
|
|
|
'$i===' => 'array{9223372036854775807: 1}',
|
|
|
|
'$so===' => "array{'9223372036854775808': 1}",
|
|
|
|
],
|
|
|
|
],
|
2023-03-13 00:57:14 +01:00
|
|
|
'autoincrementAlmostOverflow' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
public const I = [
|
|
|
|
9223372036854775806 => 0,
|
|
|
|
1, // expected key = PHP_INT_MAX
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$s = A::I;
|
|
|
|
PHP,
|
|
|
|
'assertions' => [
|
|
|
|
'$s===' => 'array{9223372036854775806: 0, 9223372036854775807: 1}',
|
|
|
|
],
|
|
|
|
],
|
2023-04-27 22:39:52 +02:00
|
|
|
'inheritedConstantIsNotAmbiguous' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
interface MainInterface {
|
|
|
|
public const TEST = 'test';
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FooInterface extends MainInterface {}
|
|
|
|
interface BarInterface extends MainInterface {}
|
|
|
|
|
|
|
|
class FooBar implements FooInterface, BarInterface {}
|
|
|
|
PHP,
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-15 21:58:40 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-01-15 21:58:40 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'constantDefinedInFunctionButNotCalled' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function defineConstant() {
|
|
|
|
define("CONSTANT", 1);
|
|
|
|
}
|
2017-12-06 06:05:01 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo CONSTANT;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedConstant',
|
|
|
|
],
|
2018-02-16 02:27:42 +01:00
|
|
|
'undefinedClassConstantInParamDefault' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-16 02:27:42 +01:00
|
|
|
class A {
|
|
|
|
public function doSomething(int $howManyTimes = self::DEFAULT_TIMES): void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedConstant',
|
|
|
|
],
|
2018-05-12 05:14:44 +02:00
|
|
|
'nonMatchingConstantOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-12 05:14:44 +02:00
|
|
|
class A {
|
|
|
|
const KEYS = ["one", "two", "three", "four"];
|
|
|
|
const ARR = [
|
|
|
|
"one" => 1,
|
|
|
|
"two" => 2
|
|
|
|
];
|
|
|
|
|
|
|
|
const ARR2 = [
|
|
|
|
"three" => 3,
|
|
|
|
"four" => 4
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (A::KEYS as $key) {
|
|
|
|
if (isset(A::ARR[$key])) {
|
|
|
|
echo A::ARR2[$key];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2018-05-14 23:39:08 +02:00
|
|
|
'objectLikeConstArrays' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-14 23:39:08 +02:00
|
|
|
class C {
|
|
|
|
const A = 0;
|
|
|
|
const B = 1;
|
|
|
|
|
|
|
|
const ARR = [
|
|
|
|
self::A => "zero",
|
|
|
|
self::B => "two",
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C::ARR[C::A] === "two") {}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2018-06-25 21:02:46 +02:00
|
|
|
'missingClassConstInArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-25 21:02:46 +02:00
|
|
|
class A {
|
|
|
|
const B = 1;
|
|
|
|
const C = [B];
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedConstant',
|
|
|
|
],
|
2018-09-09 17:18:20 +02:00
|
|
|
'resolveConstToCurrentClassWithBadReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-09-09 17:18:20 +02:00
|
|
|
interface I {
|
|
|
|
public const C = "a";
|
|
|
|
|
|
|
|
public function getC(): string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A implements I {
|
|
|
|
public function getC(): string {
|
|
|
|
return self::C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public const C = [5];
|
2018-11-06 03:57:36 +01:00
|
|
|
|
2018-09-09 17:18:20 +02:00
|
|
|
public function getC(): string {
|
|
|
|
return self::C;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2022-01-24 20:59:11 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
2018-09-09 17:18:20 +02:00
|
|
|
],
|
2019-02-22 00:19:12 +01:00
|
|
|
'outOfScopeDefinedConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-22 00:19:12 +01:00
|
|
|
namespace {
|
|
|
|
define("A\\B", 0);
|
|
|
|
}
|
|
|
|
namespace C {
|
|
|
|
echo A\B;
|
|
|
|
}',
|
2019-03-01 21:55:20 +01:00
|
|
|
'error_message' => 'UndefinedConstant',
|
2019-02-22 00:19:12 +01:00
|
|
|
],
|
2019-06-15 18:19:26 +02:00
|
|
|
'preventStaticClassConstWithoutRef' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-15 18:19:26 +02:00
|
|
|
class Foo {
|
|
|
|
public const CONST = 1;
|
|
|
|
|
|
|
|
public function x() : void {
|
|
|
|
echo static::CON;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedConstant',
|
|
|
|
],
|
2019-12-10 22:16:44 +01:00
|
|
|
'noCyclicConstReferences' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-10 22:16:44 +01:00
|
|
|
class A {
|
|
|
|
const FOO = B::FOO;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
const FOO = C::FOO;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
const FOO = A::FOO;
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'CircularReference',
|
2019-12-10 22:16:44 +01:00
|
|
|
],
|
2020-02-07 16:47:42 +01:00
|
|
|
'keyOfBadValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 16:47:42 +01:00
|
|
|
class A {
|
|
|
|
const C = [
|
|
|
|
1 => "a",
|
|
|
|
2 => "b",
|
|
|
|
3 => "c"
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param key-of<A::C> $i
|
|
|
|
*/
|
|
|
|
public static function foo(int $i) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::foo(4);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'valueOfBadValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 16:47:42 +01:00
|
|
|
class A {
|
|
|
|
const C = [
|
|
|
|
1 => "a",
|
|
|
|
2 => "b",
|
|
|
|
3 => "c"
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param value-of<A::C> $j
|
|
|
|
*/
|
|
|
|
public static function bar(string $j) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::bar("d");',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2020-02-07 16:54:57 +01:00
|
|
|
'wildcardEnumBadValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 16:54:57 +01:00
|
|
|
class A {
|
|
|
|
const C_1 = 1;
|
|
|
|
const C_2 = 2;
|
|
|
|
const C_3 = 3;
|
|
|
|
const D_4 = 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param self::C_* $i
|
|
|
|
*/
|
|
|
|
public static function foo(int $i) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::foo(A::D_4);',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2020-02-07 16:54:57 +01:00
|
|
|
],
|
2020-05-30 22:55:18 +02:00
|
|
|
'wildcardEnumAnyTemplateExtendConstantBadValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-30 22:55:18 +02:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*/
|
|
|
|
interface AInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param T $i
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
public function foo($i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @implements AInterface<A::*>
|
|
|
|
*/
|
|
|
|
class A implements AInterface {
|
|
|
|
const C_1 = 1;
|
|
|
|
const C_2 = 2;
|
|
|
|
const C_3 = 3;
|
|
|
|
const D_4 = 4;
|
|
|
|
|
|
|
|
public function foo($i)
|
|
|
|
{
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo(5);
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2020-05-30 22:55:18 +02:00
|
|
|
],
|
2020-11-05 16:13:08 +01:00
|
|
|
'correctMessage' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-05 16:13:08 +01:00
|
|
|
class S {
|
|
|
|
public const ZERO = 0;
|
|
|
|
public const ONE = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param S::* $s
|
|
|
|
*/
|
|
|
|
function foo(int $s): string {
|
|
|
|
return [1 => "a", 2 => "b"][$s];
|
|
|
|
}',
|
2023-04-01 13:32:15 +02:00
|
|
|
'error_message' => "offset value of '0|1",
|
2020-11-05 16:13:08 +01:00
|
|
|
],
|
2021-06-07 19:20:07 +02:00
|
|
|
'constantWithMissingClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-06-07 19:20:07 +02:00
|
|
|
class Subject
|
|
|
|
{
|
|
|
|
public const DATA = [
|
|
|
|
MissingClass::TAG_DATA,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function execute(): void
|
|
|
|
{
|
|
|
|
/** @psalm-suppress InvalidArrayOffset */
|
|
|
|
if (self::DATA["a"]);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2021-11-10 23:44:17 +01:00
|
|
|
'duplicateConstants' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-10 23:44:17 +01:00
|
|
|
class A {
|
|
|
|
public const B = 1;
|
|
|
|
public const B = 2;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'DuplicateConstant',
|
|
|
|
],
|
|
|
|
'constantDuplicatesEnumCase' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-10 23:44:17 +01:00
|
|
|
enum State {
|
|
|
|
case Open;
|
|
|
|
public const Open = 1;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'DuplicateConstant',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
2021-11-10 23:44:17 +01:00
|
|
|
],
|
|
|
|
'enumCaseDuplicatesConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-10 23:44:17 +01:00
|
|
|
enum State {
|
|
|
|
public const Open = 1;
|
|
|
|
case Open;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'DuplicateConstant',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
2021-11-10 23:44:17 +01:00
|
|
|
],
|
2022-01-20 23:51:55 +01:00
|
|
|
'returnValueofNonExistantConstant' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public const BAR = ["bar"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return value-of<self::BAT>
|
|
|
|
*/
|
|
|
|
public function bar(): string
|
|
|
|
{
|
|
|
|
return self::BAR[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'UnresolvableConstant',
|
|
|
|
],
|
|
|
|
'returnValueofStaticConstant' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public const BAR = ["bar"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return value-of<static::BAR>
|
|
|
|
*/
|
|
|
|
public function bar(): string
|
|
|
|
{
|
|
|
|
return static::BAR[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'UnresolvableConstant',
|
|
|
|
],
|
|
|
|
'takeKeyofNonExistantConstant' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public const BAR = ["bar"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param key-of<self::BAT> $key
|
|
|
|
*/
|
|
|
|
public function bar(int $key): string
|
|
|
|
{
|
|
|
|
return static::BAR[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'UnresolvableConstant',
|
|
|
|
],
|
|
|
|
'takeKeyofStaticConstant' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public const BAR = ["bar"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param key-of<static::BAR> $key
|
|
|
|
*/
|
|
|
|
public function bar(int $key): string
|
|
|
|
{
|
|
|
|
return static::BAR[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'UnresolvableConstant',
|
|
|
|
],
|
2022-01-20 23:51:55 +01:00
|
|
|
'invalidConstantAssignmentType' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var int */
|
|
|
|
public const BAR = "bar";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => "InvalidConstantAssignmentValue",
|
|
|
|
],
|
|
|
|
'invalidConstantAssignmentTypeResolvedLate' => [
|
2022-01-20 23:51:55 +01:00
|
|
|
'code' => '<?php
|
2022-01-20 23:51:55 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var int */
|
|
|
|
public const BAR = "bar" . self::BAZ;
|
|
|
|
public const BAZ = "baz";
|
|
|
|
public const BARBAZ = self::BAR . self::BAZ;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => "InvalidConstantAssignmentValue",
|
|
|
|
],
|
2021-12-14 00:28:15 +01:00
|
|
|
'classConstContravariant' => [
|
2022-01-24 20:59:11 +01:00
|
|
|
'code' => '<?php
|
2021-12-14 00:28:15 +01:00
|
|
|
abstract class A {
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const CONTRAVARIANT = "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class B extends A {}
|
|
|
|
|
|
|
|
abstract class C extends B {
|
|
|
|
/** @var string */
|
|
|
|
public const CONTRAVARIANT = "";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => "LessSpecificClassConstantType",
|
|
|
|
],
|
|
|
|
'classConstAmbiguousInherit' => [
|
2022-01-24 20:59:11 +01:00
|
|
|
'code' => '<?php
|
2021-12-14 00:28:15 +01:00
|
|
|
interface Foo
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Bar extends Foo {}
|
|
|
|
|
|
|
|
class Baz
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="foobar";
|
|
|
|
}
|
|
|
|
|
|
|
|
class BarBaz extends Baz implements Bar
|
|
|
|
{
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'AmbiguousConstantInheritance',
|
|
|
|
],
|
|
|
|
'overrideClassConstFromInterface' => [
|
2022-01-24 20:59:11 +01:00
|
|
|
'code' => '<?php
|
2021-12-14 00:28:15 +01:00
|
|
|
interface Foo
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Bar extends Foo {}
|
|
|
|
|
|
|
|
class Baz implements Bar
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="foobar";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'OverriddenInterfaceConstant',
|
|
|
|
],
|
|
|
|
'overrideClassConstFromInterfaceWithInterface' => [
|
2022-01-24 20:59:11 +01:00
|
|
|
'code' => '<?php
|
2021-12-14 00:28:15 +01:00
|
|
|
interface Foo
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Bar extends Foo
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="bar";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'OverriddenInterfaceConstant',
|
|
|
|
],
|
2022-01-28 04:50:36 +01:00
|
|
|
'overrideClassConstFromInterfaceWithExtraIrrelevantInterface' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface Foo
|
|
|
|
{
|
|
|
|
/** @var non-empty-string */
|
|
|
|
public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Bar {}
|
|
|
|
|
|
|
|
class Baz implements Foo, Bar
|
|
|
|
{
|
|
|
|
public const BAR="";
|
|
|
|
}
|
|
|
|
',
|
2022-02-14 00:19:25 +01:00
|
|
|
'error_message' => "InvalidClassConstantType",
|
2022-01-28 04:50:36 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
|
|
|
],
|
2022-02-14 00:19:25 +01:00
|
|
|
'overrideFinalClassConstFromExtendedClass' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
final public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baz extends Foo
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
public const BAR="foobar";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => "OverriddenFinalConstant",
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
|
|
|
],
|
|
|
|
'overrideFinalClassConstFromImplementedInterface' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface Foo
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
final public const BAR="baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baz implements Foo
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
public const BAR="foobar";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => "OverriddenFinalConstant",
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
|
|
|
],
|
|
|
|
'finalConstantIsIllegalBefore8.1' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
final public const BAR="baz";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'ParseError - src' . DIRECTORY_SEPARATOR . 'somefile.php:5:44',
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
|
|
|
],
|
2023-02-15 07:53:47 +01:00
|
|
|
'classStringIsRequiredToAccessClassConstant' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class Foo {
|
|
|
|
public const BAR = "bar";
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = "Foo";
|
|
|
|
|
|
|
|
$class::BAR;
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidStringClass',
|
|
|
|
],
|
2023-03-12 22:47:42 +01:00
|
|
|
'integerOverflowInArrayKey' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
// PHP_INT_MAX + 1
|
|
|
|
public const IO = [9223372036854775808 => 1];
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2023-03-13 00:57:14 +01:00
|
|
|
'autoincrementOverflow' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
public const I = [
|
|
|
|
9223372036854775807 => 0,
|
|
|
|
1, // this is a fatal error
|
|
|
|
];
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
|
|
|
'autoincrementOverflowWithUnpack' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
public const I = [
|
|
|
|
9223372036854775807 => 0,
|
|
|
|
...[1], // this is a fatal error
|
|
|
|
];
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-15 22:43:49 +01:00
|
|
|
}
|
2017-01-15 21:58:40 +01:00
|
|
|
}
|