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

Add parameter-dependent psalm return type hint to temporal accessors #1700

Merged

Conversation

nederdirk
Copy link
Contributor

Please refer to the Psalm documentation for conditional types1 for
more information. Although the docs specify that template parameters are
required, the reference-a-variable shorthand for templates also work,
see the [example].

Usually this project seems to prefer @phpstan--prefixes, but this
construct is currently not available to PHPstan2.

Footnotes

  1. https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/

  2. https://github.com/phpstan/phpstan/issues/3853
    [example]: https://psalm.dev/r/11cdedd7b3

Please refer to the Psalm documentation for conditional types[^0] for
more information. Although the docs specify that template parameters are
required, the reference-a-variable shorthand for templates also work,
see the [example].

Usually this project seems to prefer `@phpstan-`-prefixes, but this
construct is currently not available to PHPstan[^1].

[^0]: https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/
[^1]: phpstan/phpstan#3853
[example]: https://psalm.dev/r/11cdedd7b3
@@ -894,6 +894,8 @@ public function addTemporalAccessorComment(&$script, Column $column)
* @return string|{$dateTimeClass}{$orNull} Formatted date/time value as string or $dateTimeClass object (if format is NULL), NULL if column is NULL" . ($handleMysqlDate ? ', and 0 if column value is ' . $mysqlInvalidDateString : '') . "
*
* @throws PropelException - if unable to parse/validate the date/time value.
*
* @psalm-return (\$format is null ? {$dateTimeClass}{$orNull} : string{$orNull})
Copy link
Contributor

@mringler mringler Mar 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linked doc says conditional types only work with template parameters, which would need more setup, but it says here that this is a short hand syntax which works too, so I guess it is ok.

Since this only affects generated classes, I don't think it can cause problems in Propel anyway. Not sure what happens, if someone uses an outdated version of Psalm, where the feature is not available. But if they manage to update their Propel , they can update their Psalm, too.
Propel itself requires Psalm 4.2.1, where conditional types are available (which doesn't even matter according to above). I wouldn't agree to using third-party extensions in user code, but since Propel uses Psalm itself, I guess this is a mild recommendation from the team (which I cannot speak for at all).
But unless I am misunderstanding something (am I?), this looks fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mringler, somewhere else you wrote:

When updating my project, I realized that in your other psalm PR, where you added @psalm-return ($format is null ? DateTime : string) to DateTime getters, I did not realize that these function also return null. You might want to fix that.
Correct type is probably
@psalm-return null|($format is null ? DateTime : string)

As you see, the returned type is {$dateTimeClass}{$orNull} or string{$orNull}, so if the field you're referencing is nullable, the null typehint should already be there!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you are right. The columns I saw were all required. Should have checked again. Sorry!

@dereuromark dereuromark merged commit 6b46d61 into propelorm:master Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants