Skip to content

Commit

Permalink
Tests for property names as expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
voku authored and ondrejmirtes committed Oct 18, 2020
1 parent fadb439 commit d4b1163
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 0 deletions.
@@ -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,
],
]);
}

}
@@ -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,
],
]);
}

}
@@ -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,
],
]);
}

}
@@ -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
]
]);
}

}
@@ -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;
}
}

0 comments on commit d4b1163

Please sign in to comment.