Skip to content
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

Forbid named arguments for ArrayAcccess methods #10804

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
],
];
}
}