phpstan-drupal
phpstan-drupal copied to clipboard
Protect against https://www.drupal.org/project/drupal/issues/3013802
I have seen this issue quite a few times in custom code: $url->getRouteName() is called to check whether it's a certain entity route -- but if the url points to a since deleted entity then this bombs. It's nowhere evident you want to run $url->isRouted() && $url->getRouteName(). This should be a mild warning nothing like the world is burning if it's not there but it'd be nice to warn.
I think we need to add a few object types for PHPStan's type system. When we encounter Url::fromRoute we can call it a UrlRouted otherwise UrlUnrouted. Then whenever getRouteName is called, we can check the object type. If it's not UrlRouted via no or maybe trinary logic, we can return the error if isRouted hasn't been called. I think.
Problem is, you will likely hit something like $link_uri = Url::fromUri($paragraph->field_link->uri); -- the very problem is that you can't tell what that becomes because the link widget stores that as internal: and routes in fromUri and returns either a routed or an unrouted URL depending on whether the path validator liked it or not.