Skip to content

Commit 81a8d34

Browse files
committed
Fix for respecting rule levels for property.nameNotString
1 parent 74e8432 commit 81a8d34

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

src/Rules/Properties/AccessPropertiesCheck.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@ public function check(PropertyFetch $node, Scope $scope, bool $write): array
6262
$scope,
6363
$node->name,
6464
'',
65-
static fn (Type $type) => $type->toString()->isString()->yes(),
65+
static fn (Type $type) => !$type->toString() instanceof ErrorType && $type->toString()->isString()->yes(),
6666
);
6767
$nameType = $nameTypeResult->getType();
68-
if ($nameType instanceof ErrorType || $nameType->toString() instanceof ErrorType || !$nameType->toString()->isString()->yes()) {
68+
if (
69+
!$nameType instanceof ErrorType
70+
&& (
71+
$nameType->toString() instanceof ErrorType
72+
|| !$nameType->toString()->isString()->yes()
73+
)
74+
) {
6975
$originalNameType = $scope->getType($node->name);
7076
$className = $scope->getType($node->var)->describe(VerbosityLevel::typeOnly());
7177
$errors[] = RuleErrorBuilder::message(sprintf('Property name for %s must be a string, but %s was given.', $className, $originalNameType->describe(VerbosityLevel::precise())))

tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -996,10 +996,6 @@ public function testDynamicStringableAccess(): void
996996
'Property name for $this(DynamicStringableAccess\Foo) must be a string, but $this(DynamicStringableAccess\Foo) was given.',
997997
16,
998998
],
999-
[
1000-
'Property name for $this(DynamicStringableAccess\Foo) must be a string, but object was given.',
1001-
17,
1002-
],
1003999
[
10041000
'Property name for $this(DynamicStringableAccess\Foo) must be a string, but array was given.',
10051001
18,
@@ -1013,10 +1009,6 @@ public function testDynamicStringableAccess(): void
10131009
'Property name for DynamicStringableAccess\Foo must be a string, but $this(DynamicStringableAccess\Foo) was given.',
10141010
31,
10151011
],
1016-
[
1017-
'Property name for $this(DynamicStringableAccess\Foo) must be a string, but object was given.',
1018-
32,
1019-
],
10201012
]);
10211013
}
10221014

@@ -1044,10 +1036,6 @@ public function testDynamicStringableNullsafeAccess(): void
10441036
'Property name for $this(DynamicStringableNullsafeAccess\Foo) must be a string, but $this(DynamicStringableNullsafeAccess\Foo) was given.',
10451037
16,
10461038
],
1047-
[
1048-
'Property name for $this(DynamicStringableNullsafeAccess\Foo) must be a string, but object was given.',
1049-
17,
1050-
],
10511039
]);
10521040
}
10531041

@@ -1100,4 +1088,20 @@ public function testPropertyExists(): void
11001088
$this->analyse([__DIR__ . '/data/property-exists.php'], []);
11011089
}
11021090

1091+
public function testDiscussion13274(): void
1092+
{
1093+
$this->checkThisOnly = false;
1094+
$this->checkUnionTypes = true;
1095+
$this->checkDynamicProperties = true;
1096+
$this->analyse([__DIR__ . '/data/discussion-13274.php'], []);
1097+
}
1098+
1099+
public function testBug4117(): void
1100+
{
1101+
$this->checkThisOnly = false;
1102+
$this->checkUnionTypes = true;
1103+
$this->checkDynamicProperties = true;
1104+
$this->analyse([__DIR__ . '/data/bug-4117.php'], []);
1105+
}
1106+
11031107
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug4117;
6+
7+
function (): void {
8+
$object = new class{
9+
public string $example_one = "";
10+
public string $example_two = "";
11+
};
12+
13+
$field = rand() > 0.5 ? 'example_one' : 'example_two';
14+
$result = $object->$field;
15+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Discussion13274;
6+
7+
class A {}
8+
9+
$a = new A;
10+
11+
/** @var mixed $foo */
12+
$a->{$foo} = 'bar';

0 commit comments

Comments
 (0)