phpstan: Access to an undefined property object::$property_name

object can never have defined properties or methods, but PHPStan complains:

function takesUnknown($foo) {
	echo $foo->bar; // this is fine
}

function takesObject(object $o) {
	echo $o->bar; // Access to an undefined property object::$bar.
}

https://phpstan.org/r/8b92ca2494b5366976d12533aadc7ceb

Suggestion: treat the same as mixed::$bar, and only complain if you complain about mixed property fetching (planned for level 8)

cc @asgrim

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 22 (14 by maintainers)

Most upvoted comments

Consider: is object just a specialisation of mixed where the value is an object, but of an unknown type?

I’m seeing the same problem with current stan on level 4:

foreach ($vcards as $vcard) {
    /** @var $vcard object */
    if (isset($vcard->rawPhoto)) {
        if (preg_match("/JPEG/", strtoupper(substr($vcard->photoData, 0, 256)))) { ...

Access to an undefined property object::$photoData.  

This shouldn’t happen according to whats written above?

I’d advise against a long-running fork because you wouldn’t get all the good stuff that would get contributed to the project 😊 Contributing a setting would be a better decision.

On Mon, 15 Oct 2018 at 05:09, Matthew Brown notifications@github.com wrote:

At the moment, PHPStan fails for me so I either am forced to change the docblock to something I don’t feel is as appropriate or remove PHPStan to ensure my CI passes. I’m not keen on either approach and would prefer a fix to this issue.

Your forgot the ever-present third option - you can fork it. Forking gives you some power, relieves the original project’s maintainer from having to cater to every whim of that project’s userbase, and gives you the opportunity to contribute in the form of a PR if you still think you’re right. I’m aware of at least one org that forked a package I created because they didn’t like a specific decision I made, and I’m really glad of it.

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/phpstan/phpstan/issues/928#issuecomment-429695243, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZuM4OBNQDChHvIIdrC1lJ_IGwSf93ks5uk_xngaJpZM4TYIpQ .

Ondřej Mirtes

At the moment, PHPStan fails for me so I either am forced to change the docblock to something I don’t feel is as appropriate or remove PHPStan to ensure my CI passes. I’m not keen on either approach and would prefer a fix to this issue.

Your forgot the ever-present third option - you can fork it. Forking gives you some power, relieves the original project’s maintainer from having to cater to every whim of that project’s userbase, and gives you the opportunity to contribute in the form of a PR if you still think you’re right. I’m aware of at least one org that forked a package I created because they didn’t like a specific decision I made, and I’m really glad of it.