Skip to content

Commit

Permalink
Skip setting an exception as a default value in ApplyDefaultInsteadOf…
Browse files Browse the repository at this point in the history
…NullCoalesceRector (#294)

* Update ApplyDefaultInsteadOfNullCoalesceRector.php

* Create skip_throw_exceptions.php.inc

* Fix style;

* Fix lint error;

---------

Co-authored-by: Geni Jaho <jahogeni@gmail.com>
  • Loading branch information
hans-thomas and GeniJaho authored Jan 15, 2025
1 parent f80e95e commit 0a1ea90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace RectorLaravel\Tests\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\Fixture;

config('app.name') ?? throw new \Exception('No Google maps base URL configured.');

(new \Illuminate\Http\Request())->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.');

?>

0 comments on commit 0a1ea90

Please sign in to comment.