Is there a way to enforce the type of an object returned by a function that could return anything?
In addition to what Tom already said, using instanceof has worked quite well for me. (Actually never heard of is_a() before.) if (is_singular() && $queried_object instanceof \WP_Post) { // do something } Both PHPStan and my IDE know that after this check, $queried_object is an instance of the WP_Post class.