Skip to content

Commit d4b1163

Browse files
vokuondrejmirtes
authored andcommitted
Tests for property names as expressions
1 parent fadb439 commit d4b1163

8 files changed

+477
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Properties;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Rules\RuleLevelHelper;
7+
use PHPStan\Testing\RuleTestCase;
8+
9+
/**
10+
* @extends \PHPStan\Testing\RuleTestCase<AccessPropertiesInAssignRule>
11+
*/
12+
class AccessPropertiesFromArrayRuleTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
$broker = $this->createReflectionProvider();
18+
return new AccessPropertiesInAssignRule(
19+
new AccessPropertiesRule($broker, new RuleLevelHelper($broker, true, false, true, false), true)
20+
);
21+
}
22+
23+
public function testRule(): void
24+
{
25+
$this->analyse([__DIR__ . '/data/properties-from-array-into-object.php'], [
26+
[
27+
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.',
28+
42,
29+
],
30+
[
31+
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.',
32+
54,
33+
],
34+
[
35+
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.',
36+
69,
37+
],
38+
[
39+
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.',
40+
110,
41+
],
42+
]);
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Properties;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Rules\RuleLevelHelper;
7+
use PHPStan\Testing\RuleTestCase;
8+
9+
/**
10+
* @extends \PHPStan\Testing\RuleTestCase<AccessPropertiesInAssignRule>
11+
*/
12+
class AccessPropertiesFromVariableRuleTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
$broker = $this->createReflectionProvider();
18+
return new AccessPropertiesInAssignRule(
19+
new AccessPropertiesRule($broker, new RuleLevelHelper($broker, true, false, true, false), true)
20+
);
21+
}
22+
23+
public function testRule(): void
24+
{
25+
$this->analyse([__DIR__ . '/data/properties-from-variable-into-object.php'], [
26+
[
27+
'Access to an undefined property PropertiesFromVariableIntoObject\Foo::$noop.',
28+
26,
29+
],
30+
]);
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Properties;
4+
5+
use PHPStan\Rules\ClassCaseSensitivityCheck;
6+
use PHPStan\Rules\RuleLevelHelper;
7+
8+
/**
9+
* @extends \PHPStan\Testing\RuleTestCase<AccessStaticPropertiesRule>
10+
*/
11+
class AccessStaticPropertiesFromArrayRuleTest extends \PHPStan\Testing\RuleTestCase
12+
{
13+
14+
protected function getRule(): \PHPStan\Rules\Rule
15+
{
16+
$broker = $this->createReflectionProvider();
17+
return new AccessStaticPropertiesRule(
18+
$broker,
19+
new RuleLevelHelper($broker, true, false, true, false),
20+
new ClassCaseSensitivityCheck($broker)
21+
);
22+
}
23+
24+
public function testRule(): void
25+
{
26+
$this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [
27+
[
28+
'Cannot access static property $noop on PropertiesFromArrayIntoStaticObject\Foo.',
29+
29,
30+
],
31+
]);
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Properties;
4+
5+
use PHPStan\Rules\RuleLevelHelper;
6+
7+
/**
8+
* @extends \PHPStan\Testing\RuleTestCase<TypesAssignedToPropertiesRule>
9+
*/
10+
class TypesAssignedToPropertiesFromArrayRuleTest extends \PHPStan\Testing\RuleTestCase
11+
{
12+
13+
protected function getRule(): \PHPStan\Rules\Rule
14+
{
15+
return new TypesAssignedToPropertiesRule(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false), new PropertyDescriptor(), new PropertyReflectionFinder());
16+
}
17+
18+
public function testTypesAssignedToProperties(): void
19+
{
20+
$this->analyse([__DIR__ . '/data/properties-from-array-into-object.php'], [
21+
[
22+
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
23+
42,
24+
],
25+
[
26+
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
27+
54,
28+
],
29+
[
30+
'Property PropertiesFromArrayIntoObject\Foo::$test (int|null) does not accept stdClass.',
31+
66
32+
],
33+
[
34+
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
35+
69,
36+
],
37+
[
38+
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept int.',
39+
73,
40+
],
41+
[
42+
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept float.',
43+
83,
44+
],
45+
[
46+
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
47+
110,
48+
],
49+
[
50+
'Property PropertiesFromArrayIntoObject\FooBar::$foo (string) does not accept float.',
51+
147
52+
]
53+
]);
54+
}
55+
56+
public function testTypesAssignedToStaticProperties(): void
57+
{
58+
$this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [
59+
[
60+
'Static property PropertiesFromArrayIntoStaticObject\Foo::$lall (stdClass|null) does not accept string.',
61+
29,
62+
],
63+
[
64+
'Static property PropertiesFromArrayIntoStaticObject\Foo::$foo (string) does not accept float.',
65+
36
66+
],
67+
[
68+
'Static property PropertiesFromArrayIntoStaticObject\FooBar::$foo (string) does not accept float.',
69+
72
70+
]
71+
]);
72+
}
73+
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PropertiesFromArrayIntoObject;
4+
5+
class Foo
6+
{
7+
/**
8+
* @var string
9+
*/
10+
public $foo = '';
11+
12+
/**
13+
* @var float
14+
*/
15+
public $float_test = 0.0;
16+
17+
/**
18+
* @var int
19+
*/
20+
public $lall = 0;
21+
22+
/**
23+
* @var int|null
24+
*/
25+
public $test;
26+
27+
/**
28+
* @phpstan-return array{float_test: float, foo: 'bar', lall: string, noop: int, test: int}
29+
*/
30+
public function data(): array
31+
{
32+
/** @var mixed $array */
33+
$array = [];
34+
35+
return $array;
36+
}
37+
38+
public function create_simple_0(): self {
39+
$self = new self();
40+
41+
foreach($this->data() as $property => $value) {
42+
$self->{$property} = $value;
43+
}
44+
45+
return $self;
46+
}
47+
48+
public function create_simple_1(): self {
49+
$self = new self();
50+
51+
$data = $this->data();
52+
53+
foreach($data as $property => $value) {
54+
$self->{$property} = $value;
55+
}
56+
57+
return $self;
58+
}
59+
60+
public function create_complex(): self {
61+
$self = new self();
62+
63+
foreach($this->data() as $property => $value) {
64+
if ($property === 'test') {
65+
if ($self->{$property} === null) {
66+
$self->{$property} = new \stdClass();
67+
}
68+
} else {
69+
$self->{$property} = $value;
70+
}
71+
72+
if ($property === 'foo') {
73+
$self->{$property} += 1;
74+
}
75+
if ($property === 'foo') {
76+
$self->{$property} .= ' ';
77+
}
78+
if ($property === 'lall') {
79+
$self->{$property} += 1;
80+
}
81+
$tmp = 1.1;
82+
if ($property === 'foo') {
83+
$self->{$property} += $tmp;
84+
}
85+
}
86+
87+
return $self;
88+
}
89+
90+
public function create_simple_2(): self {
91+
$self = new self();
92+
93+
$data = $this->data();
94+
95+
$property = 'foo';
96+
foreach($data as $value) {
97+
$self->{$property} = $value;
98+
}
99+
100+
return $self;
101+
}
102+
103+
public function create_double_loop(): self {
104+
$self = new self();
105+
106+
$data = $this->data();
107+
108+
foreach($data as $property => $value) {
109+
foreach([1, 2, 3] as $value_2) {
110+
$self->{$property} = $value;
111+
}
112+
}
113+
114+
return $self;
115+
}
116+
}
117+
118+
119+
class FooBar
120+
{
121+
/**
122+
* @var string
123+
*/
124+
public $foo = '';
125+
126+
/**
127+
* @var null|\stdClass
128+
*/
129+
public $lall;
130+
131+
public function data(): array
132+
{
133+
return ['foo' => 'bar', 'lall' => 'lall', 'noop' => 1];
134+
}
135+
136+
public function create(): self {
137+
$self = new self();
138+
139+
foreach($this->data() as $property => $value) {
140+
$this->{$property} = $value;
141+
142+
if ($property === 'lall') {
143+
$this->{$property} = null;
144+
}
145+
146+
if ($property === 'foo') {
147+
$this->{$property} = 1.1;
148+
}
149+
}
150+
151+
return $self;
152+
}
153+
}

0 commit comments

Comments
 (0)