Phpinspectionsea: Code hint for OffsetOperationsInspection

Created on 14 Feb 2017  路  3Comments  路  Source: kalessil/phpinspectionsea

Hi,

I am getting hint:

'$option' may not support offset operations (or it's type not annotated properly: [0, \ArrayAccess]).
Analyzes offset operations (adding to index, index-based read, write, etc.) and reports issues found.

on code like this, note that I have even annotated the var as type ArrayAccess

/** @var ArrayAccess $option */
$option = $sets[0];
$label = array_key_exists('label', $option) ? $option['label'] : '';

What would be the proper way to use: $option['label'] here to not get the error inspection ?

Thanks in advance!

question

Most helpful comment

Hi,
found the problem. I needed to add also type hinting to $sets variable. Realize of that after to be sure that type hinting was enough.

Assigned values from uncertain variables types make this error happens I guess. Once all variables have types resolved then error is gone.

Thanks!

All 3 comments

The given typehint should be sufficient (e.g. I can not reproduce in PS 2016.3.2).
Would you please provide a code for potentially a bug analysis?

Hi,
found the problem. I needed to add also type hinting to $sets variable. Realize of that after to be sure that type hinting was enough.

Assigned values from uncertain variables types make this error happens I guess. Once all variables have types resolved then error is gone.

Thanks!

Super, thanks for an update!

Was this page helpful?
0 / 5 - 0 ratings