Skip to content

Commit a793089

Browse files
browner12tegos
authored andcommitted
[12.x] with() helper call simplification (laravel#57041)
* remove unecessary `with()` helper calls when we have a well defined closure, there is no reason to use the `with()` helper, or even the closure at all. in most cases, but using a temporary variable we achieve the same effect with arguably cleaner easier to read code. * style fixes * revert refactor I can't figure out how to fix the tests right now, so reverting these
1 parent 91f4dd6 commit a793089

File tree

9 files changed

+94
-103
lines changed

9 files changed

+94
-103
lines changed

src/Illuminate/Bus/Batch.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ public function add($jobs)
168168
if (is_array($job)) {
169169
$count += count($job);
170170

171-
return with($this->prepareBatchedChain($job), function ($chain) {
172-
return $chain->first()
173-
->allOnQueue($this->options['queue'] ?? null)
174-
->allOnConnection($this->options['connection'] ?? null)
175-
->chain($chain->slice(1)->values()->all());
176-
});
171+
$chain = $this->prepareBatchedChain($job);
172+
173+
return $chain->first()
174+
->allOnQueue($this->options['queue'] ?? null)
175+
->allOnConnection($this->options['connection'] ?? null)
176+
->chain($chain->slice(1)->values()->all());
177177
} else {
178178
$job->withBatchId($this->id);
179179

src/Illuminate/Console/Signals.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,21 @@ public function register($signal, $callback)
5151
{
5252
$this->previousHandlers[$signal] ??= $this->initializeSignal($signal);
5353

54-
with($this->getHandlers(), function ($handlers) use ($signal) {
55-
$handlers[$signal] ??= $this->initializeSignal($signal);
54+
$handlers = $this->getHandlers();
5655

57-
$this->setHandlers($handlers);
58-
});
56+
$handlers[$signal] ??= $this->initializeSignal($signal);
57+
58+
$this->setHandlers($handlers);
5959

6060
$this->registry->register($signal, $callback);
6161

62-
with($this->getHandlers(), function ($handlers) use ($signal) {
63-
$lastHandlerInserted = array_pop($handlers[$signal]);
62+
$handlers = $this->getHandlers();
63+
64+
$lastHandlerInserted = array_pop($handlers[$signal]);
6465

65-
array_unshift($handlers[$signal], $lastHandlerInserted);
66+
array_unshift($handlers[$signal], $lastHandlerInserted);
6667

67-
$this->setHandlers($handlers);
68-
});
68+
$this->setHandlers($handlers);
6969
}
7070

7171
/**

src/Illuminate/Foundation/Bootstrap/HandleExceptions.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,21 @@ protected function shouldIgnoreDeprecationErrors()
132132
*/
133133
protected function ensureDeprecationLoggerIsConfigured()
134134
{
135-
with(static::$app['config'], function ($config) {
136-
if ($config->get('logging.channels.deprecations')) {
137-
return;
138-
}
135+
$config = static::$app['config'];
139136

140-
$this->ensureNullLogDriverIsConfigured();
137+
if ($config->get('logging.channels.deprecations')) {
138+
return;
139+
}
141140

142-
if (is_array($options = $config->get('logging.deprecations'))) {
143-
$driver = $options['channel'] ?? 'null';
144-
} else {
145-
$driver = $options ?? 'null';
146-
}
141+
$this->ensureNullLogDriverIsConfigured();
147142

148-
$config->set('logging.channels.deprecations', $config->get("logging.channels.{$driver}"));
149-
});
143+
if (is_array($options = $config->get('logging.deprecations'))) {
144+
$driver = $options['channel'] ?? 'null';
145+
} else {
146+
$driver = $options ?? 'null';
147+
}
148+
149+
$config->set('logging.channels.deprecations', $config->get("logging.channels.{$driver}"));
150150
}
151151

152152
/**
@@ -156,16 +156,16 @@ protected function ensureDeprecationLoggerIsConfigured()
156156
*/
157157
protected function ensureNullLogDriverIsConfigured()
158158
{
159-
with(static::$app['config'], function ($config) {
160-
if ($config->get('logging.channels.null')) {
161-
return;
162-
}
159+
$config = static::$app['config'];
163160

164-
$config->set('logging.channels.null', [
165-
'driver' => 'monolog',
166-
'handler' => NullHandler::class,
167-
]);
168-
});
161+
if ($config->get('logging.channels.null')) {
162+
return;
163+
}
164+
165+
$config->set('logging.channels.null', [
166+
'driver' => 'monolog',
167+
'handler' => NullHandler::class,
168+
]);
169169
}
170170

171171
/**

src/Illuminate/Foundation/Console/DocsCommand.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ public function handle(Http $http, Cache $cache)
125125
*/
126126
protected function openUrl()
127127
{
128-
with($this->url(), function ($url) {
129-
$this->components->info("Opening the docs to: <fg=yellow>{$url}</>");
128+
$url = $this->url();
130129

131-
$this->open($url);
132-
});
130+
$this->components->info("Opening the docs to: <fg=yellow>{$url}</>");
131+
132+
$this->open($url);
133133
}
134134

135135
/**
@@ -145,9 +145,9 @@ protected function url()
145145
]);
146146
}
147147

148-
return with($this->page(), function ($page) {
149-
return trim("https://laravel.com/docs/{$this->version()}/{$page}#{$this->section($page)}", '#/');
150-
});
148+
$page = $this->page();
149+
150+
return trim("https://laravel.com/docs/{$this->version()}/{$page}#{$this->section($page)}", '#/');
151151
}
152152

153153
/**
@@ -157,15 +157,15 @@ protected function url()
157157
*/
158158
protected function page()
159159
{
160-
return with($this->resolvePage(), function ($page) {
161-
if ($page === null) {
162-
$this->components->warn('Unable to determine the page you are trying to visit.');
160+
$page = $this->resolvePage();
163161

164-
return '/';
165-
}
162+
if ($page === null) {
163+
$this->components->warn('Unable to determine the page you are trying to visit.');
166164

167-
return $page;
168-
});
165+
return '/';
166+
}
167+
168+
return $page;
169169
}
170170

171171
/**
@@ -441,11 +441,11 @@ public function docs()
441441
*/
442442
protected function refreshDocs()
443443
{
444-
with($this->fetchDocs(), function ($response) {
445-
if ($response->successful()) {
446-
$this->cache->put("artisan.docs.{{$this->version()}}.index", $response->collect(), CarbonInterval::months(2));
447-
}
448-
});
444+
$response = $this->fetchDocs();
445+
446+
if ($response->successful()) {
447+
$this->cache->put("artisan.docs.{{$this->version()}}.index", $response->collect(), CarbonInterval::months(2));
448+
}
449449
}
450450

451451
/**

src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,22 @@ protected function assertModelMissing($model)
225225
*/
226226
public function expectsDatabaseQueryCount($expected, $connection = null)
227227
{
228-
with($this->getConnection($connection), function ($connectionInstance) use ($expected, $connection) {
229-
$actual = 0;
230-
231-
$connectionInstance->listen(function (QueryExecuted $event) use (&$actual, $connectionInstance, $connection) {
232-
if (is_null($connection) || $connectionInstance === $event->connection) {
233-
$actual++;
234-
}
235-
});
236-
237-
$this->beforeApplicationDestroyed(function () use (&$actual, $expected, $connectionInstance) {
238-
$this->assertSame(
239-
$expected,
240-
$actual,
241-
"Expected {$expected} database queries on the [{$connectionInstance->getName()}] connection. {$actual} occurred."
242-
);
243-
});
228+
$connectionInstance = $this->getConnection($connection);
229+
230+
$actual = 0;
231+
232+
$connectionInstance->listen(function (QueryExecuted $event) use (&$actual, $connectionInstance, $connection) {
233+
if (is_null($connection) || $connectionInstance === $event->connection) {
234+
$actual++;
235+
}
236+
});
237+
238+
$this->beforeApplicationDestroyed(function () use (&$actual, $expected, $connectionInstance) {
239+
$this->assertSame(
240+
$expected,
241+
$actual,
242+
"Expected {$expected} database queries on the [{$connectionInstance->getName()}] connection. {$actual} occurred."
243+
);
244244
});
245245

246246
return $this;

src/Illuminate/Mail/Attachment.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,17 @@ function ($data) use ($mail, $options) {
206206
*/
207207
public function isEquivalent(Attachment $attachment, $options = [])
208208
{
209-
return with([
209+
$newOptions = [
210210
'as' => $options['as'] ?? $attachment->as,
211211
'mime' => $options['mime'] ?? $attachment->mime,
212-
], fn ($options) => $this->attachWith(
212+
];
213+
214+
return $this->attachWith(
213215
fn ($path) => [$path, ['as' => $this->as, 'mime' => $this->mime]],
214216
fn ($data) => [$data(), ['as' => $this->as, 'mime' => $this->mime]],
215217
) === $attachment->attachWith(
216-
fn ($path) => [$path, $options],
217-
fn ($data) => [$data(), $options],
218-
));
218+
fn ($path) => [$path, $newOptions],
219+
fn ($data) => [$data(), $newOptions],
220+
);
219221
}
220222
}

src/Illuminate/Routing/RoutingServiceProvider.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,16 @@ protected function registerPsrRequest()
134134
{
135135
$this->app->bind(ServerRequestInterface::class, function ($app) {
136136
if (class_exists(PsrHttpFactory::class)) {
137-
return with((new PsrHttpFactory)
138-
->createRequest($illuminateRequest = $app->make('request')), function (ServerRequestInterface $request) use ($illuminateRequest) {
139-
if ($illuminateRequest->getContentTypeFormat() !== 'json' && $illuminateRequest->request->count() === 0) {
140-
return $request;
141-
}
142-
143-
return $request->withParsedBody(
144-
array_merge($request->getParsedBody() ?? [], $illuminateRequest->getPayload()->all())
145-
);
146-
});
137+
$illuminateRequest = $app->make('request');
138+
$request = (new PsrHttpFactory)->createRequest($illuminateRequest);
139+
140+
if ($illuminateRequest->getContentTypeFormat() !== 'json' && $illuminateRequest->request->count() === 0) {
141+
return $request;
142+
}
143+
144+
return $request->withParsedBody(
145+
array_merge($request->getParsedBody() ?? [], $illuminateRequest->getPayload()->all())
146+
);
147147
}
148148

149149
throw new BindingResolutionException('Unable to resolve PSR request. Please install the "symfony/psr-http-message-bridge" package.');

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,9 @@ public function validateBetween($attribute, $value, $parameters)
472472
$this->requireParameterCount(2, $parameters, 'between');
473473

474474
try {
475-
return with(
476-
BigNumber::of($this->getSize($attribute, $value)),
477-
fn ($size) => $size->isGreaterThanOrEqualTo($this->trim($parameters[0])) && $size->isLessThanOrEqualTo($this->trim($parameters[1]))
478-
);
475+
$size = BigNumber::of($this->getSize($attribute, $value));
476+
477+
return $size->isGreaterThanOrEqualTo($this->trim($parameters[0])) && $size->isLessThanOrEqualTo($this->trim($parameters[1]));
479478
} catch (MathException) {
480479
return false;
481480
}

tests/Foundation/Bootstrap/HandleExceptionsTest.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ protected function setUp(): void
2929
protected function handleExceptions()
3030
{
3131
return tap(new HandleExceptions(), function ($instance) {
32-
with(new ReflectionClass($instance), function ($reflection) use ($instance) {
33-
$reflection->getProperty('app')->setValue($instance, $this->app);
34-
});
32+
(new ReflectionClass($instance))->getProperty('app')->setValue($instance, $this->app);
3533
});
3634
}
3735

@@ -381,11 +379,7 @@ public function testForgetApp()
381379
{
382380
$instance = $this->handleExceptions();
383381

384-
$appResolver = fn () => with(new ReflectionClass($instance), function ($reflection) use ($instance) {
385-
$property = $reflection->getProperty('app');
386-
387-
return $property->getValue($instance);
388-
});
382+
$appResolver = fn () => (new ReflectionClass($instance))->getProperty('app')->getValue($instance);
389383

390384
$this->assertNotNull($appResolver());
391385

@@ -398,11 +392,7 @@ public function testHandlerForgetsPreviousApp()
398392
{
399393
$instance = $this->handleExceptions();
400394

401-
$appResolver = fn () => with(new ReflectionClass($instance), function ($reflection) use ($instance) {
402-
$property = $reflection->getProperty('app');
403-
404-
return $property->getValue($instance);
405-
});
395+
$appResolver = fn () => (new ReflectionClass($instance))->getProperty('app')->getValue($instance);
406396

407397
$this->assertSame($this->app, $appResolver());
408398

0 commit comments

Comments
 (0)