Skip to content

DocumentedMemberAccessOnNonObject ​

warning native off by default

Group: Probable bugs

Report a method call or property access on a value that PHPDoc says is never an object. The code may still run, since PHP does not check PHPDoc, but the access and the documentation disagree. Receivers the code alone proves are never objects are MemberAccessOnNonObject's.

Example ​

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

Reported:

  • line 5: 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 is the one PHPDoc-aware inference gives: @param, @var, @return and @property tags count.
  • Without null, every member of that type is an int, a float, a string, a bool or an array.

Exceptions (no report) ​

  • Receivers MemberAccessOnNonObject reports.
  • 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 property read inside isset(), empty() or on the left of ??, which PHP evaluates without complaint.
  • A receiver whose PHPDoc type the code contradicts: when the code alone gives the receiver a type that no value of the PHPDoc type fits (/** @var int $x */ over $x = new Order()), the tag is stale.
  • $this inside a closure or an arrow function, which may be bound to an object of another class.

Fix ​

None: either the access or the documentation is wrong.

PHP versions ​

No gating.

Configure ​

In custos.json:

json
{
  "rules": {
    "DocumentedMemberAccessOnNonObject": {
      "enabled": true
    }
  }
}

Suppress ​

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

php
// @custos-ignore DocumentedMemberAccessOnNonObject

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