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
Support class constants properties #119
Comments
I tried to implement a simple ConstantType, but seems to need a lot of changes, more than I am comfortable 'just doing'. Example const FOO = 'bar';
const FOO_BAR = 'bar2';
class Bar {
/**
* @var FOO|FOO_BAR
*/
public $type = FOO;
} To implement |
Hi @eigan, I'd be glad to help you when you're ready to submit a PR. Concerning your issue, I think you should take a look at Don't hesitate if you need more help! |
@romm I did try Consider the case: class Bar {
/**
* @var string
*/
public $type = "fiz";
} Type of See wip here https://github.com/eigan/Valinor/pull/1/files |
I see, this might not be as easy as it first seemed to be. At first I thought the But now I wonder if there should just not be any class Foo
{
public const SOME_CONST = 'foo';
/** @var Foo::SOME_CONST */
public string $foo;
} —> the resolved type would be class Foo
{
public const SOME_CONST = 42;
/** @var Foo::SOME_CONST */
public int $foo;
} —> the resolved type would be class Foo
{
public const SOME_CONST = 1337.42;
/** @var Foo::SOME_CONST */
public float $foo;
} —> class Foo
{
public const SOME_CONST = true;
/** @var Foo::SOME_CONST */
public bool $foo;
} —> Well this one is coming in #117 😄 class Foo
{
public const SOME_CONST = [
'foo' => 'foo',
'bar' => 'bar',
];
/** @var Foo::SOME_CONST */
public array $foo;
} —> the resolved type would be class Foo
{
public const SOME_CONST_A = 'foo';
public const SOME_CONST_B = 42;
/** @var Foo::SOME_CONST_* */
public string|int $foo;
} —> the resolved type would be Probably a lot of work ahead 😬 |
class Bar {}
const SOME_CONST = new Bar;
class Foo
{
/** @var SOME_CONST|string */
public $foo;
} What would the resolved type be in this case? |
Damn I just learned something! This looks like a twisted case, but this would be possible so that should be handled as well. I'll have to think a bit about it. If you're ok with it, I will help you out with the lexing part which could be a bit hard (unless you want to try to do it by yourself), then you should be able to finish it. I'll keep you posted. 🙂 |
I am not a "lexer" guy (no experience here), but want to help. Writing tests or whatever :) My initial thought after your last comment was to try return the correct token after resolving the value of the constant. Doesn't sound that difficult, but might say more about my experience than anything... :) Please let me know when you think about starting with this, it's something we need so I am especially eager to help, or start by my self if you have other priorities :) |
@romm Thanks! 😃 |
As defined at phpstan here: https://phpstan.org/writing-php-code/phpdoc-types#literals-and-constants
Actual use-case
Optional properties in payload.
Today, the error message is:
Notice it simply ignores the rest of the types, and is not even an UnionType as expected.
In PHP 8.1 is this a lot easier because we could use a global const which value is an object (
const None = new None
) and useNone
as a valid type. Though we are still on 7.4 and must rely on some weird strings - if we decide that this is the best approuch for optional properties.The text was updated successfully, but these errors were encountered: