-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for offset access legal
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
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
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,99 @@ | ||
<?php declare(strict_types=1); // lint >= 8.0 | ||
|
||
namespace OffsetAccessLegal; | ||
|
||
function closure(): void | ||
{ | ||
(function(){})[0] ?? "error"; | ||
} | ||
|
||
function nonArrayAccessibleObject() | ||
{ | ||
(new \stdClass())[0] ?? "error"; | ||
} | ||
|
||
function arrayAccessibleObject() | ||
{ | ||
(new class implements \ArrayAccess { | ||
public function offsetExists($offset) { | ||
return true; | ||
} | ||
|
||
public function offsetGet($offset) { | ||
return $offset; | ||
} | ||
|
||
public function offsetSet($offset, $value) { | ||
} | ||
|
||
public function offsetUnset($offset) { | ||
} | ||
})[0] ?? "error"; | ||
} | ||
|
||
function array_(): void | ||
{ | ||
[0][0] ?? "error"; | ||
} | ||
|
||
function integer(): void | ||
{ | ||
(0)[0] ?? 'ok'; | ||
} | ||
|
||
function float(): void | ||
{ | ||
(0.0)[0] ?? 'ok'; | ||
} | ||
|
||
function null(): void | ||
{ | ||
(null)[0] ?? 'ok'; | ||
} | ||
|
||
function bool(): void | ||
{ | ||
(true)[0] ?? 'ok'; | ||
} | ||
|
||
function void(): void | ||
{ | ||
((function (){})())[0] ?? 'ok'; | ||
} | ||
|
||
function resource(): void | ||
{ | ||
(tmpfile())[0] ?? 'ok'; | ||
} | ||
|
||
function offsetAccessibleMaybeAndLegal(): void | ||
{ | ||
$arrayAccessible = rand() ? (new class implements \ArrayAccess { | ||
public function offsetExists($offset) { | ||
return true; | ||
} | ||
|
||
public function offsetGet($offset) { | ||
return $offset; | ||
} | ||
|
||
public function offsetSet($offset, $value) { | ||
} | ||
|
||
public function offsetUnset($offset) { | ||
} | ||
}) : false; | ||
|
||
($arrayAccessible)[0] ?? "error"; | ||
|
||
(rand() ? "string" : true)[0] ?? "error"; | ||
} | ||
|
||
function offsetAccessibleMaybeAndIllegal(): void | ||
{ | ||
$arrayAccessible = rand() ? new \stdClass() : ['test']; | ||
|
||
($arrayAccessible)[0] ?? "error"; | ||
|
||
(rand() ? function(){} : ['test'])[0] ?? "error"; | ||
} |