Skip to content

Commit b88546c

Browse files
[9.x] Avoid nested conditions (#40355)
* No need to use multiple conditions * Fix StyleCI * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 3d336c5 commit b88546c

File tree

10 files changed

+46
-68
lines changed

10 files changed

+46
-68
lines changed

src/Illuminate/Console/Parser.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ public static function parse($expression)
2121
{
2222
$name = static::name($expression);
2323

24-
if (preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches)) {
25-
if (count($matches[1])) {
26-
return array_merge([$name], static::parameters($matches[1]));
27-
}
24+
if (preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches) && count($matches[1])) {
25+
return array_merge([$name], static::parameters($matches[1]));
2826
}
2927

3028
return [$name, [], []];

src/Illuminate/Container/BoundMethod.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,14 @@ protected static function addDependencyForCallParameter($container, $parameter,
171171
$dependencies[] = $parameters[$className];
172172

173173
unset($parameters[$className]);
174+
} elseif ($parameter->isVariadic()) {
175+
$variadicDependencies = $container->make($className);
176+
177+
$dependencies = array_merge($dependencies, is_array($variadicDependencies)
178+
? $variadicDependencies
179+
: [$variadicDependencies]);
174180
} else {
175-
if ($parameter->isVariadic()) {
176-
$variadicDependencies = $container->make($className);
177-
178-
$dependencies = array_merge($dependencies, is_array($variadicDependencies)
179-
? $variadicDependencies
180-
: [$variadicDependencies]);
181-
} else {
182-
$dependencies[] = $container->make($className);
183-
}
181+
$dependencies[] = $container->make($className);
184182
}
185183
} elseif ($parameter->isDefaultValueAvailable()) {
186184
$dependencies[] = $parameter->getDefaultValue();

src/Illuminate/Filesystem/Filesystem.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,8 @@ public function copyDirectory($directory, $destination, $options = null)
659659
// If the current items is just a regular file, we will just copy this to the new
660660
// location and keep looping. If for some reason the copy fails we'll bail out
661661
// and return false, so the developer is aware that the copy process failed.
662-
else {
663-
if (! $this->copy($item->getPathname(), $target)) {
664-
return false;
665-
}
662+
elseif (! $this->copy($item->getPathname(), $target)) {
663+
return false;
666664
}
667665
}
668666

src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ public function bootstrap(Application $app)
4040
*/
4141
protected function checkForSpecificEnvironmentFile($app)
4242
{
43-
if ($app->runningInConsole() && ($input = new ArgvInput)->hasParameterOption('--env')) {
44-
if ($this->setEnvironmentFilePath(
45-
$app, $app->environmentFile().'.'.$input->getParameterOption('--env')
46-
)) {
47-
return;
48-
}
43+
if ($app->runningInConsole() &&
44+
($input = new ArgvInput)->hasParameterOption('--env') &&
45+
$this->setEnvironmentFilePath($app, $app->environmentFile().'.'.$input->getParameterOption('--env'))) {
46+
return;
4947
}
5048

5149
$environment = Env::get('APP_ENV');

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,14 @@ public function report(Throwable $e)
224224
return;
225225
}
226226

227-
if (Reflector::isCallable($reportCallable = [$e, 'report'])) {
228-
if ($this->container->call($reportCallable) !== false) {
229-
return;
230-
}
227+
if (Reflector::isCallable($reportCallable = [$e, 'report']) &&
228+
$this->container->call($reportCallable) !== false) {
229+
return;
231230
}
232231

233232
foreach ($this->reportCallbacks as $reportCallback) {
234-
if ($reportCallback->handles($e)) {
235-
if ($reportCallback($e) === false) {
236-
return;
237-
}
233+
if ($reportCallback->handles($e) && $reportCallback($e) === false) {
234+
return;
238235
}
239236
}
240237

src/Illuminate/Redis/Connectors/PhpRedisConnector.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ protected function establishConnection($client, array $config)
132132
$parameters[] = Arr::get($config, 'read_timeout', 0.0);
133133
}
134134

135-
if (version_compare(phpversion('redis'), '5.3.0', '>=')) {
136-
if (! is_null($context = Arr::get($config, 'context'))) {
137-
$parameters[] = $context;
138-
}
135+
if (version_compare(phpversion('redis'), '5.3.0', '>=') && ! is_null($context = Arr::get($config, 'context'))) {
136+
$parameters[] = $context;
139137
}
140138

141139
$client->{($persistent ? 'pconnect' : 'connect')}(...$parameters);
@@ -162,10 +160,8 @@ protected function createRedisClusterInstance(array $servers, array $options)
162160
$parameters[] = $options['password'] ?? null;
163161
}
164162

165-
if (version_compare(phpversion('redis'), '5.3.2', '>=')) {
166-
if (! is_null($context = Arr::get($options, 'context'))) {
167-
$parameters[] = $context;
168-
}
163+
if (version_compare(phpversion('redis'), '5.3.2', '>=') && ! is_null($context = Arr::get($options, 'context'))) {
164+
$parameters[] = $context;
169165
}
170166

171167
return tap(new RedisCluster(...$parameters), function ($client) use ($options) {

src/Illuminate/Routing/Console/ControllerMakeCommand.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ protected function buildParentReplacements()
135135
{
136136
$parentModelClass = $this->parseModel($this->option('parent'));
137137

138-
if (! class_exists($parentModelClass)) {
139-
if ($this->confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", true)) {
140-
$this->call('make:model', ['name' => $parentModelClass]);
141-
}
138+
if (! class_exists($parentModelClass) &&
139+
$this->confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", true)) {
140+
$this->call('make:model', ['name' => $parentModelClass]);
142141
}
143142

144143
return [
@@ -164,10 +163,8 @@ protected function buildModelReplacements(array $replace)
164163
{
165164
$modelClass = $this->parseModel($this->option('model'));
166165

167-
if (! class_exists($modelClass)) {
168-
if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) {
169-
$this->call('make:model', ['name' => $modelClass]);
170-
}
166+
if (! class_exists($modelClass) && $this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) {
167+
$this->call('make:model', ['name' => $modelClass]);
171168
}
172169

173170
$replace = $this->buildFormRequestReplacements($replace, $modelClass);

src/Illuminate/Session/CookieSessionHandler.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ public function read($sessionId)
7777
{
7878
$value = $this->request->cookies->get($sessionId) ?: '';
7979

80-
if (! is_null($decoded = json_decode($value, true)) && is_array($decoded)) {
81-
if (isset($decoded['expires']) && $this->currentTime() <= $decoded['expires']) {
82-
return $decoded['data'];
83-
}
80+
if (! is_null($decoded = json_decode($value, true)) && is_array($decoded) &&
81+
isset($decoded['expires']) && $this->currentTime() <= $decoded['expires']) {
82+
return $decoded['data'];
8483
}
8584

8685
return '';

src/Illuminate/Session/FileSessionHandler.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ public function close()
7575
#[\ReturnTypeWillChange]
7676
public function read($sessionId)
7777
{
78-
if ($this->files->isFile($path = $this->path.'/'.$sessionId)) {
79-
if ($this->files->lastModified($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
80-
return $this->files->sharedGet($path);
81-
}
78+
if ($this->files->isFile($path = $this->path.'/'.$sessionId) &&
79+
$this->files->lastModified($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
80+
return $this->files->sharedGet($path);
8281
}
8382

8483
return '';

src/Illuminate/Testing/Concerns/TestDatabases.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,20 @@ protected function bootTestDatabase()
4545
Testing\RefreshDatabase::class,
4646
];
4747

48-
if (Arr::hasAny($uses, $databaseTraits)) {
49-
if (! ParallelTesting::option('without_databases')) {
50-
$this->whenNotUsingInMemoryDatabase(function ($database) use ($uses) {
51-
[$testDatabase, $created] = $this->ensureTestDatabaseExists($database);
48+
if (Arr::hasAny($uses, $databaseTraits) && ! ParallelTesting::option('without_databases')) {
49+
$this->whenNotUsingInMemoryDatabase(function ($database) use ($uses) {
50+
[$testDatabase, $created] = $this->ensureTestDatabaseExists($database);
5251

53-
$this->switchToDatabase($testDatabase);
52+
$this->switchToDatabase($testDatabase);
5453

55-
if (isset($uses[Testing\DatabaseTransactions::class])) {
56-
$this->ensureSchemaIsUpToDate();
57-
}
54+
if (isset($uses[Testing\DatabaseTransactions::class])) {
55+
$this->ensureSchemaIsUpToDate();
56+
}
5857

59-
if ($created) {
60-
ParallelTesting::callSetUpTestDatabaseCallbacks($testDatabase);
61-
}
62-
});
63-
}
58+
if ($created) {
59+
ParallelTesting::callSetUpTestDatabaseCallbacks($testDatabase);
60+
}
61+
});
6462
}
6563
});
6664
}

0 commit comments

Comments
 (0)