Skip to content

MemberAccessOnNonObject ​

error native on by default

Group: Probable bugs

Report a method call or property access on a value that the code itself shows is never an object. Calling a method or writing a property on a scalar or an array throws an Error; reading a property raises a warning and yields null.

Example ​

php
<?php
function label(int $id): string
{
    return $id->name;
}

Reported:

  • line 4: Access this member on an object, not int.

Detection ​

  • A method call or a property read or write through -> or ?->.
  • The type of the receiver comes from native declarations only (typed parameters, properties and returns, literals, operations), and without null every member of it is an int, a float, a string, a bool or an array.

Exceptions (no report) ​

  • A receiver of unknown type, or one that may be mixed, object, iterable, callable or a class.
  • A receiver that can only be null, which PossiblyNullAccess covers.
  • A type that comes only from PHPDoc, which DocumentedMemberAccessOnNonObject covers.
  • A property read inside isset(), empty() or on the left of ??, which PHP evaluates without complaint.

Fix ​

None.

PHP versions ​

No gating.

Configure ​

In custos.json:

json
{
  "rules": {
    "MemberAccessOnNonObject": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file):

php
// @custos-ignore MemberAccessOnNonObject

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.