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

proper return types #11149

Closed
4 tasks done
lekoala opened this issue Feb 16, 2024 · 4 comments
Closed
4 tasks done

proper return types #11149

lekoala opened this issue Feb 16, 2024 · 4 comments

Comments

@lekoala
Copy link
Contributor

lekoala commented Feb 16, 2024

Description

I've been using more and more phpstan recently but I've been a bit disappointed by inconsistent return types that exist across the framework.

For example

* @return DataObject
*/
public function getRecord()
{
if ($this->record) {
return $this->record;
}
if (!$this->getForm()) {
return null;
}

Obviously should be return ?DataObject or DataObject|null depending on preferences

Should these type of issues always be distinct PR or "one big sweep" across the framework would be better ?

Additional context or points of discussion

Another typical pain point are Extensions that are not easily detected

A typical situation: you have a MyMember extension, defining a return type Member&MyMember doesn't work so well in most ide, Member|MyMember is an obvious - incorrect - hack that works sometimes

Dynamic extensions, in general, don't help with proper static analysis :-) (a phpstan plugin could help there, see https://github.com/syntro-opensource/silverstripe-phpstan)

Injector is not always friendly. I've been using a simple enough work around that looks basically like this:

/**
 * @template T
 * @param class-string<T> $class
 * @return T|mixed
 */
function ss_inject(string $class)
{
    return SilverStripe\Core\Injector\Injector::inst()->get($class);
}

This also apply to DataObject in general as soon as you do something else that MyDataObject::get_by_id($id)... I do miss something like MyDataObject::get_by_field($field, $id) or get_by_index (which is a really typical use case) and have $list->first() return something valid

Validations

  • You intend to implement the feature yourself
  • You have read the contributing guide
  • You strongly believe this feature should be in core, rather than being its own community module
  • You have checked for existing issues or pull requests related to this feature (and didn't find any)
@maxime-rainville
Copy link
Contributor

We pondered strongly-typing everything in CMS 5, but we ended up putting it in the too hard basket. We'll have more lead up time to CMS 6, so we might be able to do it there.

Should these type of issues always be distinct PR or "one big sweep" across the framework would be better ?

If you did occasional PRs to adjust the PHPDoc typing for specific method or class, we should be able to merge that.

Doing the entirety of Framework, might be a bit too much to swallow all at once.

Explicitly defining types could be a breaking API change depending on the context. So I would avoid jumping on those.

Generics supports

We're adding support for generic typehints in CMS 5.2. You might want to have a look at that.

@lekoala
Copy link
Contributor Author

lekoala commented Feb 21, 2024

@maxime-rainville great news for 5.2, can't wait to see that :-) it's going to be very helpful
ok I may then make some small pr for the parts i'm mostly using

@GuySartorelli
Copy link
Member

GuySartorelli commented Feb 28, 2024

Explicitly defining types could be a breaking API change depending on the context. So I would avoid jumping on those.

The next major branch has been created, so any PRs targetting that branch with strong-typing could be reviewed and merged if we agree with the typing.

@lekoala
Copy link
Contributor Author

lekoala commented Feb 29, 2024

great so i'll be closing this and make pr if needed

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

3 participants