Slamdunk / phpstan-laminas-framework

Laminas Framework 3 extensions for PHPStan
MIT License
16 stars 10 forks source link

PhpRenderer's __get method #14

Open bitwombat opened 4 years ago

bitwombat commented 4 years ago

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?

Slamdunk commented 4 years ago

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.

bitwombat commented 4 years ago

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.