Is there a way to enforce the type of an object returned by a function that could return anything?
I am initializing a class where the constructor requires a WP_Post object. The object I would like to pass comes from get_queried_object()
which could return almost anything. I am using is_a()
to make sure I have the right type, which "works", but my IDE does not recognize that I have constrained the type.
Is there a way to make it clear to the IDE that I have done my due diligence? I don't want to get in the habit of ignoring my IDE. It has been so nice to me in the past and saved me from so many mistakes. :)
$queried_object = get_queried_object();
if ( is_singular() is_a( $queried_object, 'WP_Post' ) ) {
// Initialize class that requires WP_Post object.
$class = new ClassThatOnlyAcceptsPostObject( $queried_object );
// ...
}