[BUG] 2.x build()
method breaks polymorphism due to incorrect return type declaration
#2807
Labels
build()
method breaks polymorphism due to incorrect return type declaration
#2807
Expected Behavior
The
build()
methods would declare the return type as the concrete class -static
instead ofself
.Actual behavior
Timber entity classes declare their return type as
self
rather thanstatic
, even though they return an instance of the calling class.Steps to reproduce behavior
build()
method to run additional bootup code, usingparent::build($wp_post)
build()
method from the instance returned by the parentbuild()
methodThe return type declared will issue warnings from code inspectors because they expect the return type to be an instance of the child class (
static
), but the return type declared in the Timber class isself
, which is inaccurate to what is actually returned. The return type should bestatic
.Notes
This doesn't throw an error, because what's actually returned is an extension of Timber\Post, so
self
technically works but is wrong. The return type declared for the build methods should bestatic
since that's what they instantiate via$post = new static()
. PHPStorm (and I'm sure a lot of other code inspection tools) warn about polymorphic calls to child methods not existing because parent return type represents the parent class, which doesn't have the added methods in its signature.What version of Timber are you using?
2.0rc1
What version of WordPress are you using?
No response
What version of PHP are you using?
8.1
How did you install Timber?
Installed or updated Timber through Composer
The text was updated successfully, but these errors were encountered: