Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for property names as expressions
- Loading branch information
1 parent
fadb439
commit d4b1163
Showing
8 changed files
with
477 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
tests/PHPStan/Rules/Properties/AccessPropertiesFromArrayRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Properties; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<AccessPropertiesInAssignRule> | ||
*/ | ||
class AccessPropertiesFromArrayRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$broker = $this->createReflectionProvider(); | ||
return new AccessPropertiesInAssignRule( | ||
new AccessPropertiesRule($broker, new RuleLevelHelper($broker, true, false, true, false), true) | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/properties-from-array-into-object.php'], [ | ||
[ | ||
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.', | ||
42, | ||
], | ||
[ | ||
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.', | ||
54, | ||
], | ||
[ | ||
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.', | ||
69, | ||
], | ||
[ | ||
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.', | ||
110, | ||
], | ||
]); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
tests/PHPStan/Rules/Properties/AccessPropertiesFromVariableRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Properties; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<AccessPropertiesInAssignRule> | ||
*/ | ||
class AccessPropertiesFromVariableRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$broker = $this->createReflectionProvider(); | ||
return new AccessPropertiesInAssignRule( | ||
new AccessPropertiesRule($broker, new RuleLevelHelper($broker, true, false, true, false), true) | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/properties-from-variable-into-object.php'], [ | ||
[ | ||
'Access to an undefined property PropertiesFromVariableIntoObject\Foo::$noop.', | ||
26, | ||
], | ||
]); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
tests/PHPStan/Rules/Properties/AccessStaticPropertiesFromArrayRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Properties; | ||
|
||
use PHPStan\Rules\ClassCaseSensitivityCheck; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<AccessStaticPropertiesRule> | ||
*/ | ||
class AccessStaticPropertiesFromArrayRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
$broker = $this->createReflectionProvider(); | ||
return new AccessStaticPropertiesRule( | ||
$broker, | ||
new RuleLevelHelper($broker, true, false, true, false), | ||
new ClassCaseSensitivityCheck($broker) | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [ | ||
[ | ||
'Cannot access static property $noop on PropertiesFromArrayIntoStaticObject\Foo.', | ||
29, | ||
], | ||
]); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesFromArrayRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Properties; | ||
|
||
use PHPStan\Rules\RuleLevelHelper; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<TypesAssignedToPropertiesRule> | ||
*/ | ||
class TypesAssignedToPropertiesFromArrayRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new TypesAssignedToPropertiesRule(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false), new PropertyDescriptor(), new PropertyReflectionFinder()); | ||
} | ||
|
||
public function testTypesAssignedToProperties(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/properties-from-array-into-object.php'], [ | ||
[ | ||
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.', | ||
42, | ||
], | ||
[ | ||
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.', | ||
54, | ||
], | ||
[ | ||
'Property PropertiesFromArrayIntoObject\Foo::$test (int|null) does not accept stdClass.', | ||
66 | ||
], | ||
[ | ||
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.', | ||
69, | ||
], | ||
[ | ||
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept int.', | ||
73, | ||
], | ||
[ | ||
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept float.', | ||
83, | ||
], | ||
[ | ||
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.', | ||
110, | ||
], | ||
[ | ||
'Property PropertiesFromArrayIntoObject\FooBar::$foo (string) does not accept float.', | ||
147 | ||
] | ||
]); | ||
} | ||
|
||
public function testTypesAssignedToStaticProperties(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [ | ||
[ | ||
'Static property PropertiesFromArrayIntoStaticObject\Foo::$lall (stdClass|null) does not accept string.', | ||
29, | ||
], | ||
[ | ||
'Static property PropertiesFromArrayIntoStaticObject\Foo::$foo (string) does not accept float.', | ||
36 | ||
], | ||
[ | ||
'Static property PropertiesFromArrayIntoStaticObject\FooBar::$foo (string) does not accept float.', | ||
72 | ||
] | ||
]); | ||
} | ||
|
||
} |
153 changes: 153 additions & 0 deletions
153
tests/PHPStan/Rules/Properties/data/properties-from-array-into-object.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PropertiesFromArrayIntoObject; | ||
|
||
class Foo | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $foo = ''; | ||
|
||
/** | ||
* @var float | ||
*/ | ||
public $float_test = 0.0; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $lall = 0; | ||
|
||
/** | ||
* @var int|null | ||
*/ | ||
public $test; | ||
|
||
/** | ||
* @phpstan-return array{float_test: float, foo: 'bar', lall: string, noop: int, test: int} | ||
*/ | ||
public function data(): array | ||
{ | ||
/** @var mixed $array */ | ||
$array = []; | ||
|
||
return $array; | ||
} | ||
|
||
public function create_simple_0(): self { | ||
$self = new self(); | ||
|
||
foreach($this->data() as $property => $value) { | ||
$self->{$property} = $value; | ||
} | ||
|
||
return $self; | ||
} | ||
|
||
public function create_simple_1(): self { | ||
$self = new self(); | ||
|
||
$data = $this->data(); | ||
|
||
foreach($data as $property => $value) { | ||
$self->{$property} = $value; | ||
} | ||
|
||
return $self; | ||
} | ||
|
||
public function create_complex(): self { | ||
$self = new self(); | ||
|
||
foreach($this->data() as $property => $value) { | ||
if ($property === 'test') { | ||
if ($self->{$property} === null) { | ||
$self->{$property} = new \stdClass(); | ||
} | ||
} else { | ||
$self->{$property} = $value; | ||
} | ||
|
||
if ($property === 'foo') { | ||
$self->{$property} += 1; | ||
} | ||
if ($property === 'foo') { | ||
$self->{$property} .= ' '; | ||
} | ||
if ($property === 'lall') { | ||
$self->{$property} += 1; | ||
} | ||
$tmp = 1.1; | ||
if ($property === 'foo') { | ||
$self->{$property} += $tmp; | ||
} | ||
} | ||
|
||
return $self; | ||
} | ||
|
||
public function create_simple_2(): self { | ||
$self = new self(); | ||
|
||
$data = $this->data(); | ||
|
||
$property = 'foo'; | ||
foreach($data as $value) { | ||
$self->{$property} = $value; | ||
} | ||
|
||
return $self; | ||
} | ||
|
||
public function create_double_loop(): self { | ||
$self = new self(); | ||
|
||
$data = $this->data(); | ||
|
||
foreach($data as $property => $value) { | ||
foreach([1, 2, 3] as $value_2) { | ||
$self->{$property} = $value; | ||
} | ||
} | ||
|
||
return $self; | ||
} | ||
} | ||
|
||
|
||
class FooBar | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $foo = ''; | ||
|
||
/** | ||
* @var null|\stdClass | ||
*/ | ||
public $lall; | ||
|
||
public function data(): array | ||
{ | ||
return ['foo' => 'bar', 'lall' => 'lall', 'noop' => 1]; | ||
} | ||
|
||
public function create(): self { | ||
$self = new self(); | ||
|
||
foreach($this->data() as $property => $value) { | ||
$this->{$property} = $value; | ||
|
||
if ($property === 'lall') { | ||
$this->{$property} = null; | ||
} | ||
|
||
if ($property === 'foo') { | ||
$this->{$property} = 1.1; | ||
} | ||
} | ||
|
||
return $self; | ||
} | ||
} |
Oops, something went wrong.