-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement isOffsetAccessLegal #3045
Changes from 5 commits
5277d55
b81633f
461cb89
ed98710
b012509
fbf47dd
6ed2ea6
dbfc3b8
4b4af8e
631a3a1
d7951a7
9b18667
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,11 @@ public function isOffsetAccessible(): TrinaryLogic | |
return TrinaryLogic::createNo(); | ||
} | ||
|
||
public function isOffsetAccessLegal(): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createYes(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most important difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this being added to a trait with a name like this. I'd like a separate trait for this method that would be used in all the places where relevant. Or maybe no trait at all, and just put this implementation in all the places where it applies. |
||
|
||
public function hasOffsetValueType(Type $offsetType): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createNo(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,10 @@ public function testRule(): void | |
'Cannot access offset \'a\' on Closure(): void.', | ||
253, | ||
], | ||
[ | ||
'Cannot access offset \'a\' on array{a: 1, b: 1}|(Closure(): void).', | ||
258, | ||
], | ||
Comment on lines
+118
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the behavior I wanted to revert. |
||
[ | ||
'Offset null does not exist on array<int, string>.', | ||
310, | ||
|
@@ -252,6 +256,10 @@ public function testRuleBleedingEdge(): void | |
'Cannot access offset \'a\' on Closure(): void.', | ||
253, | ||
], | ||
[ | ||
'Cannot access offset \'a\' on array{a: 1, b: 1}|(Closure(): void).', | ||
258, | ||
], | ||
[ | ||
'Offset null does not exist on array<int, string>.', | ||
310, | ||
|
@@ -737,6 +745,17 @@ public function testBug8166(): void | |
]); | ||
} | ||
|
||
public function testBug10926(): void | ||
{ | ||
$this->checkExplicitMixed = true; | ||
$this->analyse([__DIR__ . '/data/bug-10926.php'], [ | ||
[ | ||
'Cannot access offset \'a\' on stdClass.', | ||
10, | ||
], | ||
]); | ||
} | ||
|
||
public function testMixed(): void | ||
{ | ||
$this->checkExplicitMixed = true; | ||
|
@@ -757,6 +776,29 @@ public function testMixed(): void | |
]); | ||
} | ||
|
||
public function testOffsetAccessLegal(): void | ||
{ | ||
$this->checkExplicitMixed = true; | ||
$this->analyse([__DIR__ . '/data/offset-access-legal.php'], [ | ||
[ | ||
'Cannot access offset 0 on Closure(): void.', | ||
7, | ||
], | ||
[ | ||
'Cannot access offset 0 on stdClass.', | ||
12, | ||
], | ||
[ | ||
'Cannot access offset 0 on array{\'test\'}|stdClass.', | ||
96, | ||
], | ||
[ | ||
'Cannot access offset 0 on array{\'test\'}|(Closure(): void).', | ||
98, | ||
], | ||
]); | ||
} | ||
|
||
public function dataReportPossiblyNonexistentArrayOffset(): iterable | ||
{ | ||
yield [false, false, []]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace Bug10926; | ||
|
||
class HelloWorld | ||
{ | ||
public function sayHello(?\stdClass $date): void | ||
{ | ||
$date ??= new \stdClass(); | ||
echo isset($date['a']); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to add a tip for
!$isOffsetAccessibleType->isOffsetAccessLegal()->yes()
case.isOffsetAccessibleType
should be narrowed down to a type excludingObjectType
that do not implementArrayAccess::class
ArrayAccess::Class
should be markedfinal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get this part. It doesn't matter if a class is final when it already implements
ArrayAccess
. What a finality of such class would achieve?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just a mistake. I was mixed up with the fact that we cant say
isOffsetAccessLegal()->no()
if the class is not final, because child class might implement ArrayClass.phpstan/phpstan#10926 (comment)