Given
class a {
public ?int $int;
}
Would it be considerable to have a fixer that changes it to public ?int $int = null; (regardless of visibility and type). This potentially avoids a redundant constructor.
Ultimately the "uninitialized" state can be caught by static analysis tools. See e.g. https://github.com/vimeo/psalm/issues/1787
Context for this at https://3v4l.org/C8OBt:
<?php
class A
{
public int $b;
public ?int $c;
public ?int $d = null;
}
var_dump((array) new A);
array(1) { ["d"]=> NULL }
@Taluu personally i like to opt-out from "uninitialized" state by default. I see no sanity in having to account for:
class a {
public ?int $int;
}
var_dump((new a())->int);
Fatal error: Uncaught Error: Typed property a::$int must not be accessed before initialization
IMHO the language got it wrong here. But we accept and move on, so here we are.
to be honest i like the idea.
totally possible to create fixer like that.
can you try to do it, @ro0NL ?
As I said on slack, to me it's not something that should be done. There is a different meaning between "my property is nullable", e.g "it can have a null value", but this doesn't mean "my property is nullable, so it should default to null". This has twi meaning, which is fixed with an intentional ?int $foo = null.
int $foo = null (which was the old way of declaring a nullable argument) is a heresy that should never has seen light IMO.
I understand the technical difference, but still i haven't found a reasonable sane answer on "why would i want it" or "why did we do this".
I'm considering the practical impact. One is free to use this fixer y/n of course :)
This is not about int $foo = null
This is about ?int $foo = null vs ?int $foo
we would need to handle this new rule vs no_null_property_initialization
Most helpful comment
@Taluu personally i like to opt-out from "uninitialized" state by default. I see no sanity in having to account for:
IMHO the language got it wrong here. But we accept and move on, so here we are.