Skip to content

Commit 0a1ea90

Browse files
Skip setting an exception as a default value in ApplyDefaultInsteadOfNullCoalesceRector (#294)
* Update ApplyDefaultInsteadOfNullCoalesceRector.php * Create skip_throw_exceptions.php.inc * Fix style; * Fix lint error; --------- Co-authored-by: Geni Jaho <jahogeni@gmail.com>
1 parent f80e95e commit 0a1ea90

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Expr\FuncCall;
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\StaticCall;
11+
use PhpParser\Node\Expr\Throw_;
1112
use PHPStan\Type\ObjectType;
1213
use Rector\Contract\Rector\ConfigurableRectorInterface;
1314
use RectorLaravel\AbstractRector;
@@ -107,12 +108,14 @@ public function refactor(Node $node): MethodCall|StaticCall|FuncCall|null
107108
$this->isObjectType(
108109
$objectType,
109110
$applyDefaultWith->getObjectType()) &&
110-
$this->isName($call->name, $applyDefaultWith->getMethodName())
111+
$this->isName($call->name, $applyDefaultWith->getMethodName()) &&
112+
! $node->right instanceof Throw_
111113
) {
112114
$valid = true;
113115
} elseif (
114116
$applyDefaultWith->getObjectType() === null &&
115-
$this->isName($call->name, $applyDefaultWith->getMethodName())
117+
$this->isName($call->name, $applyDefaultWith->getMethodName()) &&
118+
! $node->right instanceof Throw_
116119
) {
117120
$valid = true;
118121
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\Fixture;
4+
5+
config('app.name') ?? throw new \Exception('No Google maps base URL configured.');
6+
7+
(new \Illuminate\Http\Request())->input('value') ?? throw new \Exception('No Google maps base URL configured.');
8+
9+
\Illuminate\Support\Env::get('APP_NAME') ?? throw new \Exception('No Google maps base URL configured.');
10+
11+
?>

0 commit comments

Comments
 (0)