diff --git a/src/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector.php b/src/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector.php index 58d90f69..47ef3262 100644 --- a/src/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector.php +++ b/src/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Expr\Throw_; use PHPStan\Type\ObjectType; use Rector\Contract\Rector\ConfigurableRectorInterface; use RectorLaravel\AbstractRector; @@ -107,12 +108,14 @@ public function refactor(Node $node): MethodCall|StaticCall|FuncCall|null $this->isObjectType( $objectType, $applyDefaultWith->getObjectType()) && - $this->isName($call->name, $applyDefaultWith->getMethodName()) + $this->isName($call->name, $applyDefaultWith->getMethodName()) && + ! $node->right instanceof Throw_ ) { $valid = true; } elseif ( $applyDefaultWith->getObjectType() === null && - $this->isName($call->name, $applyDefaultWith->getMethodName()) + $this->isName($call->name, $applyDefaultWith->getMethodName()) && + ! $node->right instanceof Throw_ ) { $valid = true; } diff --git a/tests/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector/Fixture/skip_throw_exceptions.php.inc b/tests/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector/Fixture/skip_throw_exceptions.php.inc new file mode 100644 index 00000000..072329c7 --- /dev/null +++ b/tests/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector/Fixture/skip_throw_exceptions.php.inc @@ -0,0 +1,11 @@ +input('value') ?? throw new \Exception('No Google maps base URL configured.'); + +\Illuminate\Support\Env::get('APP_NAME') ?? throw new \Exception('No Google maps base URL configured.'); + +?>