Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: typehinting MyObject::get() #113

Open
tractorcow opened this issue Aug 1, 2018 · 6 comments
Open

Suggestion: typehinting MyObject::get() #113

tractorcow opened this issue Aug 1, 2018 · 6 comments

Comments

@tractorcow
Copy link
Contributor

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 :)

@Firesphere
Copy link
Member

Well, besides your typo... That is actually a pretty good idea to add the common static methods to the docblock

@tractorcow
Copy link
Contributor Author

Actually I just realised we can add that to DataObject directly.

just DataList|static[]

@tractorcow
Copy link
Contributor Author

What typo?

@robbieaverill
Copy link
Contributor

@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.

@lekoala
Copy link
Contributor

lekoala commented Apr 26, 2024

maybe this is not as relevant with silverstripe 5.2 being released?

@robbieaverill
Copy link
Contributor

robbieaverill commented Apr 28, 2024

@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();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants