hammer-tools / laravel-hammer

Apache License 2.0
10 stars 1 forks source link

feat request add optionaltonullsafeoperatorrector #1

Closed chrillep closed 1 year ago

chrillep commented 1 year ago

https://github.com/driftingly/rector-laravel/blob/main/docs/rector_rules_overview.md#optionaltonullsafeoperatorrector

OptionalToNullsafeOperatorRector Convert simple calls to optional helper to use the nullsafe operator

🔧 configure it!

class: Rector\Laravel\Rector\PropertyFetch\OptionalToNullsafeOperatorRector

use Rector\Config\RectorConfig;
use Rector\Laravel\Rector\PropertyFetch\OptionalToNullsafeOperatorRector;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(OptionalToNullsafeOperatorRector::class, [
        OptionalToNullsafeOperatorRector::EXCLUDE_METHODS => ['present'],
    ]);
};

↓

-optional($user)->getKey();
-optional($user)->id;
+$user?->getKey();
+$user?->id;
 // macro methods
 optional($user)->present()->getKey();
rentalhost commented 1 year ago

@chrillep Thanks for your request!

In this case, is your request to suggest replacing optional() with null safe operator? Maybe it's a nice addition to the php-hammer brother. What do you think?

chrillep commented 1 year ago

In this case, is your request to suggest replacing optional() with null safe operator?

hello. yes exactly, i mean optional() is a Laravel function but it works either way for me :)

https://github.com/laravel/framework/blob/9.x/src/Illuminate/Support/Optional.php

https://github.com/laravel/framework/blob/4d126e876d42d8549c420f451badc61a45b6809a/src/Illuminate/Support/helpers.php#L181-L197

rentalhost commented 1 year ago

@chrillep Nice! I'll see if I can make this possible in php-hammer, so I can cover other frameworks if they have similar functionality.

chrillep commented 1 year ago

@chrillep Nice! I'll see if I can make this possible in php-hammer, so I can cover other frameworks if they have similar functionality.

added an issue in php-hammer aswell :) https://github.com/hammer-tools/php-hammer/issues/1

rentalhost commented 1 year ago

Moved to https://github.com/hammer-tools/php-hammer/issues/1

rentalhost commented 1 year ago

Fixed at https://github.com/hammer-tools/php-hammer/issues/1