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 support for enums as parameters or return types #16

Closed
nickyr opened this issue Dec 14, 2016 · 14 comments
Closed

Add support for enums as parameters or return types #16

nickyr opened this issue Dec 14, 2016 · 14 comments

Comments

@nickyr
Copy link
Contributor

nickyr commented Dec 14, 2016

You always have to fake enums in PHP since they're not supported. Psalm could help this!

One option would be to do a syntax like this:

/**
 * @return 'A'|'B'|'C'
 */

The quotes would differentiate the strings from classes, and this would fit pretty naturally with the way Psalm currently allows notation with false like

/**
 * @return string|false
 */

Or, you could do something like

/**
 * @return enum{'A', 'B', 'C'}
 */

which would kind of match the current fancy array syntax.

@nickyr
Copy link
Contributor Author

nickyr commented Dec 14, 2016

#17 could make this even awesomer

@muglug
Copy link
Collaborator

muglug commented Dec 15, 2016

What do you think of https://flowtype.org/docs/builtins.html#literal-types? Pretty much identical to your first proposition. And it could be used together with #18 to avoid repetition

@erunion
Copy link
Contributor

erunion commented Dec 15, 2016

Seems like this might break from the standard PHPDoc @return annotation.

@nickyr
Copy link
Contributor Author

nickyr commented Dec 15, 2016

Looks good!

@erunion there's a @psalm-return annotation for things that don't match PHPDoc

@muglug
Copy link
Collaborator

muglug commented Apr 12, 2018

How about

/*
 * @psalm-param enum-string('some-option'|Class::CONSTANT) $foo
 */
function takesConstant(string $foo) {}

@erunion
Copy link
Contributor

erunion commented Apr 12, 2018

Oh I like that.

@nickyr
Copy link
Contributor Author

nickyr commented Apr 13, 2018

that looks good! but it should support ints as well as strings

@muglug
Copy link
Collaborator

muglug commented Apr 20, 2018

@JanTvrdik did you have any plan to support a particular format in PHPStan, given you already have value types?

@muglug
Copy link
Collaborator

muglug commented May 20, 2018

@psalm-param enum-string("hello"|"goodbye")|enum-int(1 |2 | C::A)

and also

@psalm-param keys-of(C::A)|keys-of(C::B)|values-of(C::D)

@weirdan
Copy link
Collaborator

weirdan commented May 20, 2018

enum-string("hello"|"goodbye") should probably use comma as a separator, to match that of enum-int.

I reckon it's just a syntax proposal at this stage, and it's not implemented yet, right?

@muglug
Copy link
Collaborator

muglug commented May 20, 2018

Yeah, have updated - was unintentional

@muglug
Copy link
Collaborator

muglug commented May 20, 2018

Actually it can be simpler like
@psalm-param ('hello' | 'goodbye' | 1 | 2 | C::A)
or (necessarily without spaces)
@psalm-param 'hello'|'goodbye'|1|2|C::A

@nickyr
Copy link
Contributor Author

nickyr commented May 20, 2018

@psalm-param 'hello'|'goodbye'|1|2|C::A

That's perfect

@muglug
Copy link
Collaborator

muglug commented May 20, 2018

Merged basic support into master, will follow up with values-of/keys-of and class constant support at a later date.

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

No branches or pull requests

4 participants