Skip to content

Commit c5e320a

Browse files
committed
Add tests
1 parent b7fba75 commit c5e320a

File tree

7 files changed

+169
-0
lines changed

7 files changed

+169
-0
lines changed

Diff for: tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,24 @@ public function testClassConstantAccessedOnTrait(): void
435435
]);
436436
}
437437

438+
public function testDynamicAccess(): void
439+
{
440+
if (PHP_VERSION_ID < 80200) {
441+
$this->markTestSkipped('Test requires PHP 8.2.');
442+
}
443+
444+
$this->phpVersion = PHP_VERSION_ID;
445+
446+
$this->analyse([__DIR__ . '/data/dynamic-constant-access.php'], [
447+
[
448+
'Access to undefined constant ClassConstantDynamicAccess\Foo::FOO.',
449+
20,
450+
],
451+
[
452+
'Access to undefined constant ClassConstantDynamicAccess\Foo::BUZ.',
453+
20,
454+
],
455+
]);
456+
}
457+
438458
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.2
2+
3+
namespace ClassConstantDynamicAccess;
4+
5+
final class Foo
6+
{
7+
8+
private const BAR = 'FOO';
9+
10+
/** @var 'FOO'|'BAR'|'BUZ' */
11+
public $name;
12+
13+
public function test(string $string, object $obj): void
14+
{
15+
$bar = 'FOO';
16+
17+
echo self::{$foo};
18+
echo self::{$string};
19+
echo self::{$obj};
20+
echo self::{$this->name};
21+
}
22+
23+
}

Diff for: tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -3552,4 +3552,27 @@ public function testBug6828(): void
35523552
$this->analyse([__DIR__ . '/data/bug-6828.php'], []);
35533553
}
35543554

3555+
public function testDynamicCall(): void
3556+
{
3557+
$this->checkThisOnly = false;
3558+
$this->checkNullables = true;
3559+
$this->checkUnionTypes = true;
3560+
$this->checkExplicitMixed = true;
3561+
3562+
$this->analyse([__DIR__ . '/data/dynamic-call.php'], [
3563+
[
3564+
'Call to an undefined method MethodsDynamicCall\Foo::bar().',
3565+
23,
3566+
],
3567+
[
3568+
'Call to an undefined method MethodsDynamicCall\Foo::bar().',
3569+
26,
3570+
],
3571+
[
3572+
'Call to an undefined method MethodsDynamicCall\Foo::buz().',
3573+
26,
3574+
],
3575+
]);
3576+
}
3577+
35553578
}

Diff for: tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -862,4 +862,26 @@ public function testBug12015(): void
862862
$this->analyse([__DIR__ . '/data/bug-12015.php'], []);
863863
}
864864

865+
public function testDynamicCall(): void
866+
{
867+
$this->checkThisOnly = false;
868+
$this->checkExplicitMixed = true;
869+
$this->checkImplicitMixed = true;
870+
871+
$this->analyse([__DIR__ . '/data/dynamic-call.php'], [
872+
[
873+
'Call to an undefined static method MethodsDynamicCall\Foo::bar().',
874+
33,
875+
],
876+
[
877+
'Call to an undefined static method MethodsDynamicCall\Foo::bar().',
878+
36,
879+
],
880+
[
881+
'Call to an undefined static method MethodsDynamicCall\Foo::buz().',
882+
36,
883+
],
884+
]);
885+
}
886+
865887
}

Diff for: tests/PHPStan/Rules/Methods/data/dynamic-call.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace MethodsDynamicCall;
4+
5+
final class Foo
6+
{
7+
8+
/** @var 'foo'|'bar'|'buz'|'qux' */
9+
public static $name;
10+
11+
public function foo(): void
12+
{
13+
}
14+
15+
public static function qux(): void
16+
{
17+
}
18+
19+
public function test(string $string, object $obj): void
20+
{
21+
$foo = 'bar';
22+
23+
echo $this->$foo();
24+
echo $this->$string();
25+
echo $this->$obj();
26+
echo $this->{self::$name}();
27+
}
28+
29+
public function testStaticCall(string $string, object $obj): void
30+
{
31+
$foo = 'bar';
32+
33+
echo self::$foo();
34+
echo self::$string();
35+
echo self::$obj();
36+
echo self::{self::$name}();
37+
}
38+
}

Diff for: tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -1098,4 +1098,26 @@ public function testPropertyHooks(): void
10981098
]);
10991099
}
11001100

1101+
public function testDynamicAccess(): void
1102+
{
1103+
$this->cliArgumentsVariablesRegistered = true;
1104+
$this->polluteScopeWithLoopInitialAssignments = true;
1105+
$this->checkMaybeUndefinedVariables = true;
1106+
$this->polluteScopeWithAlwaysIterableForeach = true;
1107+
$this->analyse([__DIR__ . '/data/dynamic-access.php'], [
1108+
[
1109+
'Undefined variable: $bar',
1110+
15,
1111+
],
1112+
[
1113+
'Undefined variable: $bar',
1114+
18,
1115+
],
1116+
[
1117+
'Undefined variable: $buz',
1118+
18,
1119+
],
1120+
]);
1121+
}
1122+
11011123
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace VariablesDynamicAccess;
4+
5+
final class Foo
6+
{
7+
8+
/** @var 'foo'|'bar'|'buz' */
9+
public $name;
10+
11+
public function test(string $string, object $obj): void
12+
{
13+
$foo = 'bar';
14+
15+
echo $$foo;
16+
echo $$string;
17+
echo $$obj;
18+
echo ${$this->name};
19+
}
20+
21+
}

0 commit comments

Comments
 (0)