Suggestion: typehinting MyObject::get()
I've established that this works and gives you typehinting for dataobjects.
/**
* @method static DataList|Location[] get()
*/
class Loction extends DataObject {}
Do with this what you will :)
Well, besides your typo... That is actually a pretty good idea to add the common static methods to the docblock
Actually I just realised we can add that to DataObject directly.
just DataList|static[]
What typo?
@tractorcow this isn't working for me:
diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php
index 928576fca..beb8cdf45 100644
--- a/src/ORM/DataObject.php
+++ b/src/ORM/DataObject.php
@@ -3030,7 +3030,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*
* @todo $containerClass is Ignored, why?
*
- * @return DataList The objects matching the filter, in the class specified by $containerClass
+ * @return DataList|static[] The objects matching the filter, in the class specified by $containerClass
*/
public static function get(
$callerClass = null,
static on its own works correctly, but it's obviously incorrect for that method
I notice PSR-5 (back in draft again) supports collections e.g. DataList<static> (presumably static would work) but that's not working in my PHPStorm right now.
maybe this is not as relevant with silverstripe 5.2 being released?
@lekoala it looks like they still recommend developers write their own type hints, which this module would assist with: https://docs.silverstripe.org/en/5/changelogs/5.2.0/#generics-return-lists
In your project code, any time you return an instance of SS_List (such as a DataList or ArrayList), you can add a generic typehint to declare what kind of object the returned list contains. This example will hint to the IDE that it returns a DataList containing CarouselItem records:
use App\Model\CarouselItem; use SilverStripe\ORM\DataList; /** * @return DataList<CarouselItem> */ function getCarouselItems(): DataList { return CarouselItem::get(); }