Skip to content

Commit

Permalink
Forbid named arguments for ArrayAcccess methods
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Mar 9, 2024
1 parent 005e318 commit 00c2fef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions stubs/CoreGenericClasses.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ interface ArrayAccess {
* The return value will be casted to boolean if non-boolean was returned.
*
* @since 5.0.0
* @no-named-arguments because of conflict with ArrayObject
*/
public function offsetExists($offset);

Expand All @@ -94,6 +95,7 @@ interface ArrayAccess {
* @psalm-ignore-nullable-return
*
* @since 5.0.0
* @no-named-arguments because of conflict with ArrayObject
*/
public function offsetGet($offset);

Expand All @@ -106,6 +108,7 @@ interface ArrayAccess {
* @return void
*
* @since 5.0.0
* @no-named-arguments because of conflict with ArrayObject
*/
public function offsetSet($offset, $value);

Expand All @@ -117,6 +120,7 @@ interface ArrayAccess {
* @return void
*
* @since 5.0.0
* @no-named-arguments because of conflict with ArrayObject
*/
public function offsetUnset($offset);
}
Expand Down Expand Up @@ -162,6 +166,7 @@ class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Count
* @return bool true if the requested index exists, otherwise false
*
* @since 5.0.0
* @no-named-arguments because of conflict with ArrayAccess
*/
public function offsetExists($offset) { }

Expand All @@ -173,6 +178,7 @@ class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Count
* @return TValue The value at the specified index or false.
*
* @since 5.0.0
* @no-named-arguments because of conflict with ArrayAccess
*/
public function offsetGet($offset) { }

Expand All @@ -185,6 +191,7 @@ class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Count
* @return void
*
* @since 5.0.0
* @no-named-arguments because of conflict with ArrayAccess
*/
public function offsetSet($offset, $value) { }

Expand All @@ -196,6 +203,7 @@ class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Count
* @return void
*
* @since 5.0.0
* @no-named-arguments because of conflict with ArrayAccess
*/
public function offsetUnset($offset) { }

Expand Down
29 changes: 29 additions & 0 deletions tests/ArrayAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,25 @@ public function f(): void {
'assertions' => [],
'ignored_issues' => ['UndefinedDocblockClass'],
],
'canExtendArrayObjectOffsetSet' => [
'code' => <<<'PHP'
<?php
// parameter names in PHP are messed up:
// ArrayObject::offsetSet(mixed $key, mixed $value) : void;
// ArrayAccess::offsetSet(mixed $offset, mixed $value) : void;
// and yet ArrayObject implements ArrayAccess
/** @extends ArrayObject<int, int> */
class C extends ArrayObject {
public function offsetSet(mixed $key, mixed $value): void {
parent::offsetSet($key, $value);
}
}
PHP,
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0',
],
];
}

Expand Down Expand Up @@ -1560,6 +1579,16 @@ function takesArrayOfFloats(array $arr): void {
if ($x === null) {}',
'error_message' => 'PossiblyUndefinedArrayOffset',
],
'cannotUseNamedArgumentsForArrayAccess' => [
'code' => <<<'PHP'
<?php
/** @param ArrayAccess<int, string> $a */
function f(ArrayAccess $a): void {
echo $a->offsetGet(offset: 0);
}
PHP,
'error_message' => 'NamedArgumentNotAllowed',
],
];
}
}

0 comments on commit 00c2fef

Please sign in to comment.