phpstan-laminas-framework icon indicating copy to clipboard operation
phpstan-laminas-framework copied to clipboard

PhpRenderer's __get method

Open bitwombat opened this issue 4 years ago • 2 comments

As you know, a zend-mvc view has fields that __get can return, used like this:

$page = $this->getView()->page;

PHPStan says Access to an undefined property which makes sense. Ondre recommends a @property above the class, which doesn't work in this case, because I don't own PhpRenderer.php

Before I trudge down the PropertiesClassReflectionExtension path, have you done any work on this, or do you have an easier solution?

bitwombat avatar Jun 04 '20 07:06 bitwombat

I've never used View specific attributes outside templates, as I consider it a smell of bad design.

By the way, there would be no solution to what you're trying to accomplish: view properties depend on runtime behaviors, and PHPStan can not and should not know runtime behaviors.

I suggest something like:

$view = $this->getView();

\Webmozart\Assert\Assert::true(isset($view->page));
// Or better
$page = $view->vars('page');
\Webmozart\Assert\Assert::notNull($page);

Magic methods are evil.

Slamdunk avatar Jun 04 '20 07:06 Slamdunk

This is legacy code, and while I'm taking full ownership, there are still a few bad practices I haven't spotted, so thanks.

This is in a view helper, though, so does that change whether or not it's a smell to you? The view property is part of the model, and the view helper needs to pull some things from that.

By the way, there would be no solution to what you're trying to accomplish

Couldn't a one-off, project-specific extension say, effectively, "if you do see this magic __get call, assume it's this type"?

Thanks much for the dialog.

bitwombat avatar Jun 04 '20 21:06 bitwombat