Skip to content

Commit b52ced2

Browse files
authored
Use ?-> instead of optional (#40354)
1 parent be3e20c commit b52ced2

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

src/Illuminate/Database/Concerns/ManagesTransactions.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function transaction(Closure $callback, $attempts = 1)
4848
$this->transactions = max(0, $this->transactions - 1);
4949

5050
if ($this->transactions == 0) {
51-
optional($this->transactionsManager)->commit($this->getName());
51+
$this->transactionsManager?->commit($this->getName());
5252
}
5353
} catch (Throwable $e) {
5454
$this->handleCommitTransactionException(
@@ -83,7 +83,7 @@ protected function handleTransactionException(Throwable $e, $currentAttempt, $ma
8383
$this->transactions > 1) {
8484
$this->transactions--;
8585

86-
optional($this->transactionsManager)->rollback(
86+
$this->transactionsManager?->rollback(
8787
$this->getName(), $this->transactions
8888
);
8989

@@ -116,7 +116,7 @@ public function beginTransaction()
116116

117117
$this->transactions++;
118118

119-
optional($this->transactionsManager)->begin(
119+
$this->transactionsManager?->begin(
120120
$this->getName(), $this->transactions
121121
);
122122

@@ -194,7 +194,7 @@ public function commit()
194194
$this->transactions = max(0, $this->transactions - 1);
195195

196196
if ($this->transactions == 0) {
197-
optional($this->transactionsManager)->commit($this->getName());
197+
$this->transactionsManager?->commit($this->getName());
198198
}
199199

200200
$this->fireConnectionEvent('committed');
@@ -258,7 +258,7 @@ public function rollBack($toLevel = null)
258258

259259
$this->transactions = $toLevel;
260260

261-
optional($this->transactionsManager)->rollback(
261+
$this->transactionsManager?->rollback(
262262
$this->getName(), $this->transactions
263263
);
264264

@@ -297,7 +297,7 @@ protected function handleRollBackException(Throwable $e)
297297
if ($this->causedByLostConnection($e)) {
298298
$this->transactions = 0;
299299

300-
optional($this->transactionsManager)->rollback(
300+
$this->transactionsManager?->rollback(
301301
$this->getName(), $this->transactions
302302
);
303303
}

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ public function buildRecorderHandler()
919919
$promise = $handler($request, $options);
920920

921921
return $promise->then(function ($response) use ($request, $options) {
922-
optional($this->factory)->recordRequestResponsePair(
922+
$this->factory?->recordRequestResponsePair(
923923
(new Request($request))->withData($options['laravel_data']),
924924
new Response($response)
925925
);
@@ -1056,7 +1056,7 @@ public function getPromise()
10561056
*/
10571057
protected function dispatchRequestSendingEvent()
10581058
{
1059-
if ($dispatcher = optional($this->factory)->getDispatcher()) {
1059+
if ($dispatcher = $this->factory?->getDispatcher()) {
10601060
$dispatcher->dispatch(new RequestSending($this->request));
10611061
}
10621062
}
@@ -1069,7 +1069,7 @@ protected function dispatchRequestSendingEvent()
10691069
*/
10701070
protected function dispatchResponseReceivedEvent(Response $response)
10711071
{
1072-
if (! ($dispatcher = optional($this->factory)->getDispatcher()) ||
1072+
if (! ($dispatcher = $this->factory?->getDispatcher()) ||
10731073
! $this->request) {
10741074
return;
10751075
}
@@ -1084,7 +1084,7 @@ protected function dispatchResponseReceivedEvent(Response $response)
10841084
*/
10851085
protected function dispatchConnectionFailedEvent()
10861086
{
1087-
if ($dispatcher = optional($this->factory)->getDispatcher()) {
1087+
if ($dispatcher = $this->factory?->getDispatcher()) {
10881088
$dispatcher->dispatch(new ConnectionFailed($this->request));
10891089
}
10901090
}

src/Illuminate/Http/Client/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function reason()
137137
*/
138138
public function effectiveUri()
139139
{
140-
return optional($this->transferStats)->getEffectiveUri();
140+
return $this->transferStats?->getEffectiveUri();
141141
}
142142

143143
/**
@@ -252,7 +252,7 @@ public function cookies()
252252
*/
253253
public function handlerStats()
254254
{
255-
return optional($this->transferStats)->getHandlerStats() ?? [];
255+
return $this->transferStats?->getHandlerStats() ?? [];
256256
}
257257

258258
/**

src/Illuminate/Session/Middleware/StartSession.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function handleRequestWhileBlocking(Request $request, $session, Closur
9595

9696
return $this->handleStatefulRequest($request, $session, $next);
9797
} finally {
98-
optional($lock)->release();
98+
$lock?->release();
9999
}
100100
}
101101

src/Illuminate/View/AnonymousComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function data()
5151
$this->attributes = $this->attributes ?: $this->newAttributeBag();
5252

5353
return array_merge(
54-
optional($this->data['attributes'] ?? null)->getAttributes() ?: [],
54+
($this->data['attributes'] ?? null)?->getAttributes() ?: [],
5555
$this->attributes->getAttributes(),
5656
$this->data,
5757
['attributes' => $this->attributes]

tests/Integration/Http/Fixtures/AuthorResourceWithOptionalRelationship.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function toArray($request)
1414
return 'not loaded';
1515
}),
1616
'latest_post_title' => $this->whenLoaded('posts', function () {
17-
return optional($this->posts->first())->title ?: 'no posts yet';
17+
return $this->posts->first()?->title ?: 'no posts yet';
1818
}, 'not loaded'),
1919
];
2020
}

0 commit comments

Comments
 (0)