Skip to content

Fix unexpected CommonDBTM::can() $input param-out type#20

Merged
cedric-anne merged 2 commits into
glpi-project:mainfrom
cedric-anne:fix/can-param-out
Apr 30, 2026
Merged

Fix unexpected CommonDBTM::can() $input param-out type#20
cedric-anne merged 2 commits into
glpi-project:mainfrom
cedric-anne:fix/can-param-out

Conversation

@cedric-anne

Copy link
Copy Markdown
Member

When a parameter is passed by reference, PHPStan assumes that its type can be changed inside the called method to any type allowed by the signature.

Our CommonDBTM::can() and CommonDBTM::check() methods have a ?array &$input = null. PHPStan therefore consider that the $input variable can be changed to either null or an array inside the method. For instance, an error is detected in the following code:

if ($item->can(-1, CREATE, $_POST)) {
    $name = $_POST['name']; // Offset 'name' might not exist on array|null. (offsetAccess.notFound)
}

But the reality is these methods always preserve the passed type. With the proposed CommonDBTMCanInputParamOutTypeResolver extension, we will indicate that the type is preserved during the method execution. On the 11.0/bugfixes branch, it fixes 179 errors.

@cedric-anne cedric-anne merged commit 2d0e9c1 into glpi-project:main Apr 30, 2026
2 checks passed
@cedric-anne cedric-anne deleted the fix/can-param-out branch April 30, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants