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

Remove null coalesce for methods with default argument #268

Merged
merged 15 commits into from
Nov 5, 2024

Conversation

peterfox
Copy link
Collaborator

@peterfox peterfox commented Nov 2, 2024

Changes

  • Adds a configurable rule (with framework defaults)
  • Adds a value object to use with the rule
  • adds tests
  • updates docs
  • Adds rule to code quality set

Why

The rule should be useful to reduce the use of null coalescing with methods and functions where the method can take a default argument.

Not that in some cases a null coalescing might still be needed, for instance, if a config key is set to null, as far as I'm aware, it would still return null over the default. The default is only used when a key is missing.

The rule also covers the scenario where null is set and removes it.

-config('app.name') ?? false;
+config('app.name', false);
-config('app.name') ?? null;
+config('app.name');

-(new \Illuminate\Http\Request())->input('value') ?? '';
+(new \Illuminate\Http\Request())->input('value', '');

-\Illuminate\Support\Env::get('APP_NAME') ?? '';
+\Illuminate\Support\Env::get('APP_NAME', '');

@peterfox peterfox added the enhancement New feature or request label Nov 2, 2024
@peterfox peterfox self-assigned this Nov 2, 2024
@peterfox peterfox merged commit f8d624d into main Nov 5, 2024
5 checks passed
@peterfox peterfox deleted the feature/remove-null-coalesce-for-default branch November 5, 2024 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants