laravel / pint

Laravel Pint is an opinionated PHP code style fixer for minimalists.
https://laravel.com/docs/pint
MIT License
2.79k stars 144 forks source link

Change default for nullable_type_declaration_for_default_null_value #236

Closed otsch closed 11 months ago

otsch commented 11 months ago

Because support for implicit support for implicit nullability exists only for backwards compatibility reasons and will be removed in the future. See https://github.com/laravel/pint/issues/235.

jasonvarga commented 11 months ago

Something to be aware of is that it could be a breaking change, particularly in packages where someone might override one of your classes.

For example:

class Foo
{
- public function test(string $arg = null)
+ public function test(?string $arg = null)
  {
      //
  }
}
class Bar extends Foo
{
  public function test(string $arg = null)
  {
    // Fatal Error:
    // Declaration of Bar::test(string $arg) must be compatible with Foo::test(?string $arg)
  }
}
otsch commented 11 months ago

Tried it and I don't get a fatal: Bildschirmfoto 2023-12-05 um 21 51 26

I guess PHP internally knows that there is no difference if the type is explicitly or implicitly nullable. Or do you really get the fatal error @jasonvarga?

jasonvarga commented 11 months ago

Yeah I really get the error. Here you can see it in a browser and cli. I don't think there's anything special about my setup that would show an error where others wouldn't see one. I could be wrong though!

CleanShot 2023-12-05 at 16 45 01

Jubeki commented 11 months ago

@jasonvarga in your image you are missing the '= null'.

otsch commented 11 months ago

@jasonvarga In this screenshot the $name argument has no default value. That makes the difference. ?string $name and string $name definitely is not the same. But ?string $name = null and string $name = null, basically is the same, as the null default value makes the string type implicitly nullable.

jasonvarga commented 11 months ago

Ah crap. You're absolutely right. Sorry for the bother! ☺️ 🤐 🤦‍♂️