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

Not possible to extend ArrayObject - conflict of ArrayObject/ArrayAccess? #10533

Closed
mbolli opened this issue Jan 9, 2024 · 5 comments · Fixed by #10804
Closed

Not possible to extend ArrayObject - conflict of ArrayObject/ArrayAccess? #10533

mbolli opened this issue Jan 9, 2024 · 5 comments · Fixed by #10804
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement good first issue internal stubs/callmap

Comments

@mbolli
Copy link

mbolli commented Jan 9, 2024

Example:
https://psalm.dev/r/a03704b816

And the real code:

/**
 * Class ControllerDescriptionArray.
 *
 * @extends \ArrayObject<int, ControllerDescription|null>
 */
class ControllerDescriptionArray extends \ArrayObject {
    public function offsetSet(mixed $key, mixed $value): void {
        if ($value instanceof ControllerDescription === false) {
            throw new \InvalidArgumentException('Value must be a ControllerDescription');
        }
        parent::offsetSet($key, $value);
    }
}

output (psalm 5.18.0):

ERROR: ParamNameMismatch
at /var/www/framework/src/controller/description/ControllerDescriptionArray.php:13:37
Argument 1 of mbolli\framework\controller\description\ControllerDescriptionArray::offsetSet has wrong name $key, expecting $offset as defined by ArrayAccess::offsetSet (see https://psalm.dev/230)
    public function offsetSet(mixed $key, mixed $value): void {


ERROR: ParamNameMismatch
at /var/www/framework/src/controller/description/ControllerDescriptionArray.php:13:37
Argument 1 of mbolli\framework\controller\description\ControllerDescriptionArray::offsetSet has wrong name $key, expecting $offset as defined by ArrayObject::offsetSet (see https://psalm.dev/230)
    public function offsetSet(mixed $key, mixed $value): void {

Note: According to https://www.php.net/manual/en/arrayobject.offsetset.php, the ArrayObject::offsetSet() method signature is:

public ArrayObject::offsetSet(mixed $key, mixed $value): void

According to https://www.php.net/manual/en/arrayaccess.offsetset.php, the ArrayAccess::offsetSet() method signature is:

public ArrayAccess::offsetSet(mixed $offset, mixed $value): void
Copy link

I found these snippets:

https://psalm.dev/r/a03704b816
<?php
/**
 * Class ControllerDescriptionArray.
 *
 * @extends \ArrayObject<int, stdClass|null>
 */
class ControllerDescriptionArray extends \ArrayObject {
    public function offsetSet(mixed $key, mixed $value): void {
        if ($value instanceof stdClass === false) {
            throw new \InvalidArgumentException('Value must be a class');
        }
        parent::offsetSet($key, $value);
    }
}
Psalm output (using commit a75d26a):

ERROR: ParamNameMismatch - 8:37 - Argument 1 of ControllerDescriptionArray::offsetSet has wrong name $key, expecting $offset as defined by ArrayAccess::offsetSet

ERROR: ParamNameMismatch - 8:37 - Argument 1 of ControllerDescriptionArray::offsetSet has wrong name $key, expecting $index as defined by ArrayObject::offsetSet

ERROR: ParamNameMismatch - 8:49 - Argument 2 of ControllerDescriptionArray::offsetSet has wrong name $value, expecting $newval as defined by ArrayObject::offsetSet

INFO: MixedArgument - 12:27 - Argument 1 of ArrayObject::offsetSet cannot be mixed, expecting int

@marcreichel
Copy link

Same here. psalm.dev is still showing the errors before #10480 though.

@weirdan
Copy link
Collaborator

weirdan commented Mar 7, 2024

The error is valid and should be fixed in PHP. Meanwhile, we should probably annotate ArrayObject::offset* methods as @no-named-arguments.

@weirdan weirdan added enhancement easy problems Issues that can be fixed without background knowledge of Psalm internal stubs/callmap good first issue labels Mar 7, 2024
@weirdan
Copy link
Collaborator

weirdan commented Mar 9, 2024

Psalm does not complain if you name the argument $offset: https://psalm.dev/r/ad9554f91a

Copy link

I found these snippets:

https://psalm.dev/r/ad9554f91a
<?php
/**
 * Class ControllerDescriptionArray.
 *
 * @extends \ArrayObject<int, stdClass|null>
 */
class ControllerDescriptionArray extends \ArrayObject {
    public function offsetSet(mixed $offset, mixed $value): void {
        if ($value instanceof stdClass === false) {
            throw new \InvalidArgumentException('Value must be a class');
        }
        parent::offsetSet((int)$offset, $value);
    }
}
Psalm output (using commit ef3b018):

No issues!

weirdan added a commit to weirdan/psalm that referenced this issue Mar 9, 2024
weirdan added a commit to weirdan/psalm that referenced this issue Mar 9, 2024
weirdan added a commit to weirdan/psalm that referenced this issue Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement good first issue internal stubs/callmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants