silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
720 stars 820 forks source link

Docblock comments on DataList shadow SS_List's more useful PHPDoc types in PHPStorm (::first, ::byID, ::find, ::filter, etc.) #11247

Closed MasonD closed 4 weeks ago

MasonD commented 1 month ago

Module version(s) affected

5.2.1

Description

I'm not sure on the exact behaviour of phpstan, but at least PHPStorm's inbuilt analyser does not correctly type the following snippet:

$member = Member::get()->filter(['Email' => 'example@example.com])->first();

This is because PHPStorm is ignoring SS_List's typings when a docblock is present on a method in DataList. PHPStorm correctly identifies the type of $member if the docblock comments of filter and first are removed, or if @inheretDoc is added to both methods' docblocks.

How to reproduce

$member = Member::get()->filter(['Email' => 'example@example.com])->first();

in a fresh install of silverstripe & PHPStorm. PHPStorm doesn't recognise the type of $member and won't provide completion or recognise method calls for refactoring etc.

Possible Solution

Either add @inheretDoc to all the offending methods in DataList and its subclasses, or copy the phpdoc types from SS_List into the docblock comments directly.

Additional Context

No response

Validations

PRs

emteknetnz commented 1 month ago

Closing because this is an issue with PHPStorm, not with Silverstripe

The type hinting works correctly in VSCode, at least with the PHP Intelephense plugin - with the example above I was correctly type hinted for a Member. Type hinting was greatly improved in 5.2 with generic type hints

MasonD commented 1 month ago

@emteknetnz I don't understand this stance? I agree that 5.2 is a huge improvement, and was running my own stubs with generic type hints before 5.2. But the reason for adding the generic type hints is to improve dev experience, and the current types don't work with PHPStorm's analysis but would work with just the inclusion of @inheritDoc.

Is there something I'm missing about @inheritDoc that would make its inclusion in the docblocks of DataList methods in order to support PHPStorm unfeasible? If it degrades the experience in other editors I'd concede that that's not a trade to make, but if all it does is improve PHPStorm support, I think that's worth it. I'd be willing to create the PR.

kinglozzer commented 1 month ago

Seems like a pretty trivial fix, and given the prevalence of PHPStorm I think it’s worthwhile. Some of the comments in DataList do contain a bit more (DataObject-specific) info than the methods they’re overriding, so rather than using @inheritDoc it might be better to copy the return types down in some places. Do you want to have a crack at a PR @MasonD?

MasonD commented 1 month ago

Sure. Which branch would you like me to target it on?

kinglozzer commented 1 month ago

I think 5 rather than 5.2, because I suppose this is more an enhancement for PHPStorm users than an bug fix

emteknetnz commented 1 month ago

Targetting 5.2 is fine for this particular enhancement, it's not changing the functional behaviour of any classes.

Means you'll get your patch release immediately instead of having to wait several months.

emteknetnz commented 4 weeks ago

Linked PR has been merged, it will be automatically tagged shortly