Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhpRenderer's __get method #14

Open
bitwombat opened this issue Jun 4, 2020 · 2 comments
Open

PhpRenderer's __get method #14

bitwombat opened this issue Jun 4, 2020 · 2 comments
Labels
question Further information is requested

Comments

@bitwombat
Copy link
Contributor

bitwombat commented Jun 4, 2020

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
Copy link
Owner

Slamdunk commented Jun 4, 2020

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 Slamdunk added the question Further information is requested label Jun 4, 2020
@bitwombat
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants