Skip to content

Commit

Permalink
Always prefer typesafe string comparisons. (#36657)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Michot authored Mar 18, 2021
1 parent 9981c77 commit 371e7c4
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function path()
{
$pattern = trim($this->getPathInfo(), '/');

return $pattern == '' ? '/' : $pattern;
return $pattern === '' ? '/' : $pattern;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ protected function getLockForPopping()
$databaseEngine = $this->database->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
$databaseVersion = $this->database->getConfig('version') ?? $this->database->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);

if ($databaseEngine == 'mysql' && ! strpos($databaseVersion, 'MariaDB') && version_compare($databaseVersion, '8.0.1', '>=') ||
$databaseEngine == 'pgsql' && version_compare($databaseVersion, '9.5', '>=')) {
if ($databaseEngine === 'mysql' && ! strpos($databaseVersion, 'MariaDB') && version_compare($databaseVersion, '8.0.1', '>=') ||
$databaseEngine === 'pgsql' && version_compare($databaseVersion, '9.5', '>=')) {
return 'FOR UPDATE SKIP LOCKED';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/CompiledRouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ protected function newRoute(array $attributes)
), '/');
}

return $this->router->newRoute($attributes['methods'], $baseUri == '' ? '/' : $baseUri, $attributes['action'])
return $this->router->newRoute($attributes['methods'], $baseUri === '' ? '/' : $baseUri, $attributes['action'])
->setFallback($attributes['fallback'])
->setDefaults($attributes['defaults'])
->setWheres($attributes['wheres'])
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public static function replaceArray($search, array $replace, $subject)
*/
public static function replaceFirst($search, $replace, $subject)
{
if ($search == '') {
if ($search === '') {
return $subject;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function filled($value)
*/
function object_get($object, $key, $default = null)
{
if (is_null($key) || trim($key) == '') {
if (is_null($key) || trim($key) === '') {
return $object;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Testing/Concerns/TestDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected function whenNotUsingInMemoryDatabase($callback)
{
$database = DB::getConfig('database');

if ($database != ':memory:') {
if ($database !== ':memory:') {
$callback($database);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ protected function validateAttribute($attribute, $rule)

[$rule, $parameters] = ValidationRuleParser::parse($rule);

if ($rule == '') {
if ($rule === '') {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Auth/AuthAccessGateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ public function testAfterCallbacksAreCalledWithResult()
});

$gate->after(function ($user, $ability, $result) {
if ($ability == 'foo') {
if ($ability === 'foo') {
$this->assertTrue($result, 'After callback on `foo` should receive true as result');
} elseif ($ability == 'bar') {
} elseif ($ability === 'bar') {
$this->assertFalse($result, 'After callback on `bar` should receive false as result');
} else {
$this->assertNull($result, 'After callback on `missing` should receive null as result');
Expand Down Expand Up @@ -312,7 +312,7 @@ public function testAfterCallbacksDoNotOverrideEachOther()
$gate = $this->getBasicGate();

$gate->after(function ($user, $ability, $result) {
return $ability == 'allow';
return $ability === 'allow';
});

$gate->after(function ($user, $ability, $result) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Console/Scheduling/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testBuildCommandInBackgroundUsingWindows()

public function testBuildCommandSendOutputTo()
{
$quote = (DIRECTORY_SEPARATOR == '\\') ? '"' : "'";
$quote = (DIRECTORY_SEPARATOR === '\\') ? '"' : "'";

$event = new Event(m::mock(EventMutex::class), 'php -i');

Expand All @@ -81,7 +81,7 @@ public function testBuildCommandSendOutputTo()

public function testBuildCommandAppendOutput()
{
$quote = (DIRECTORY_SEPARATOR == '\\') ? '"' : "'";
$quote = (DIRECTORY_SEPARATOR === '\\') ? '"' : "'";

$event = new Event(m::mock(EventMutex::class), 'php -i');

Expand Down
6 changes: 3 additions & 3 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function test_belongs_to_relationship()
->create();

$this->assertCount(3, $posts->filter(function ($post) {
return $post->user->name == 'Taylor Otwell';
return $post->user->name === 'Taylor Otwell';
}));

$this->assertCount(1, FactoryTestUser::all());
Expand Down Expand Up @@ -401,11 +401,11 @@ public function test_sequences()
$this->assertCount(4, $user->roles);

$this->assertCount(2, $user->roles->filter(function ($role) {
return $role->pivot->admin == 'Y';
return $role->pivot->admin === 'Y';
}));

$this->assertCount(2, $user->roles->filter(function ($role) {
return $role->pivot->admin == 'N';
return $role->pivot->admin === 'N';
}));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseMigrationRefreshCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testRefreshCommandCallsCommandsWithProperArguments()
$console->shouldReceive('find')->with('migrate')->andReturn($migrateCommand);
$dispatcher->shouldReceive('dispatch')->once()->with(m::type(DatabaseRefreshed::class));

$quote = DIRECTORY_SEPARATOR == '\\' ? '"' : "'";
$quote = DIRECTORY_SEPARATOR === '\\' ? '"' : "'";
$resetCommand->shouldReceive('run')->with(new InputMatcher("--force=1 {$quote}migrate:reset{$quote}"), m::any());
$migrateCommand->shouldReceive('run')->with(new InputMatcher('--force=1 migrate'), m::any());

Expand All @@ -65,7 +65,7 @@ public function testRefreshCommandCallsCommandsWithStep()
$console->shouldReceive('find')->with('migrate')->andReturn($migrateCommand);
$dispatcher->shouldReceive('dispatch')->once()->with(m::type(DatabaseRefreshed::class));

$quote = DIRECTORY_SEPARATOR == '\\' ? '"' : "'";
$quote = DIRECTORY_SEPARATOR === '\\' ? '"' : "'";
$rollbackCommand->shouldReceive('run')->with(new InputMatcher("--step=2 --force=1 {$quote}migrate:rollback{$quote}"), m::any());
$migrateCommand->shouldReceive('run')->with(new InputMatcher('--force=1 migrate'), m::any());

Expand Down
6 changes: 3 additions & 3 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function testSetChmod()
$files = new Filesystem;
$files->chmod(self::$tempDir.'/file.txt', 0755);
$filePermission = substr(sprintf('%o', fileperms(self::$tempDir.'/file.txt')), -4);
$expectedPermissions = DIRECTORY_SEPARATOR == '\\' ? '0666' : '0755';
$expectedPermissions = DIRECTORY_SEPARATOR === '\\' ? '0666' : '0755';
$this->assertEquals($expectedPermissions, $filePermission);
}

Expand All @@ -148,7 +148,7 @@ public function testGetChmod()

$files = new Filesystem;
$filePermission = $files->chmod(self::$tempDir.'/file.txt');
$expectedPermissions = DIRECTORY_SEPARATOR == '\\' ? '0666' : '0755';
$expectedPermissions = DIRECTORY_SEPARATOR === '\\' ? '0666' : '0755';
$this->assertEquals($expectedPermissions, $filePermission);
}

Expand Down Expand Up @@ -488,7 +488,7 @@ public function testMakeDirectory()
*/
public function testSharedGet()
{
if (PHP_OS == 'Darwin') {
if (PHP_OS === 'Darwin') {
$this->markTestSkipped('The operating system is MacOS.');
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,13 +688,13 @@ public function testCanAssertAgainstOrderOfHttpRequestsWithCallables()

$exampleUrls = [
function ($request) {
return $request->url() == 'http://example.com/1';
return $request->url() === 'http://example.com/1';
},
function ($request) {
return $request->url() == 'http://example.com/2';
return $request->url() === 'http://example.com/2';
},
function ($request) {
return $request->url() == 'http://example.com/3';
return $request->url() === 'http://example.com/3';
},
];

Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/Database/MigratorEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public function testMigrationEventsContainTheMigrationAndMethod()
$this->artisan('migrate:rollback', $this->migrateOptions());

Event::assertDispatched(MigrationStarted::class, function ($event) {
return $event->method == 'up' && $event->migration instanceof Migration;
return $event->method === 'up' && $event->migration instanceof Migration;
});
Event::assertDispatched(MigrationStarted::class, function ($event) {
return $event->method == 'down' && $event->migration instanceof Migration;
return $event->method === 'down' && $event->migration instanceof Migration;
});
Event::assertDispatched(MigrationEnded::class, function ($event) {
return $event->method == 'up' && $event->migration instanceof Migration;
return $event->method === 'up' && $event->migration instanceof Migration;
});
Event::assertDispatched(MigrationEnded::class, function ($event) {
return $event->method == 'down' && $event->migration instanceof Migration;
return $event->method === 'down' && $event->migration instanceof Migration;
});
}

Expand All @@ -62,10 +62,10 @@ public function testTheNoMigrationEventIsFiredWhenNothingToMigrate()
$this->artisan('migrate:rollback');

Event::assertDispatched(NoPendingMigrations::class, function ($event) {
return $event->method == 'up';
return $event->method === 'up';
});
Event::assertDispatched(NoPendingMigrations::class, function ($event) {
return $event->method == 'down';
return $event->method === 'down';
});
}
}
2 changes: 1 addition & 1 deletion tests/Notifications/NotificationBroadcastChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testNotificationIsBroadcastedNow()

$events = m::mock(Dispatcher::class);
$events->shouldReceive('dispatch')->once()->with(m::on(function ($event) {
return $event->connection == 'sync';
return $event->connection === 'sync';
}));
$channel = new BroadcastChannel($events);
$channel->send($notifiable, $notification);
Expand Down
10 changes: 5 additions & 5 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public function testFilter($collection)

$c = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
$this->assertEquals(['first' => 'Hello', 'second' => 'World'], $c->filter(function ($item, $key) {
return $key != 'id';
return $key !== 'id';
})->all());
}

Expand Down Expand Up @@ -3126,7 +3126,7 @@ public function testRejectRemovesElementsPassingTruthTest($collection)

$c = new $collection(['foo', 'bar']);
$this->assertEquals(['foo'], $c->reject(function ($v) {
return $v == 'bar';
return $v === 'bar';
})->values()->all());

$c = new $collection(['foo', null]);
Expand All @@ -3137,12 +3137,12 @@ public function testRejectRemovesElementsPassingTruthTest($collection)

$c = new $collection(['foo', 'bar']);
$this->assertEquals(['foo', 'bar'], $c->reject(function ($v) {
return $v == 'baz';
return $v === 'baz';
})->values()->all());

$c = new $collection(['id' => 1, 'primary' => 'foo', 'secondary' => 'bar']);
$this->assertEquals(['primary' => 'foo', 'secondary' => 'bar'], $c->reject(function ($item, $key) {
return $key == 'id';
return $key === 'id';
})->all());
}

Expand Down Expand Up @@ -3215,7 +3215,7 @@ public function testSearchReturnsFalseWhenItemIsNotFound($collection)
return $value < 1 && is_numeric($value);
}));
$this->assertFalse($c->search(function ($value) {
return $value == 'nope';
return $value === 'nope';
}));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Support/SupportTestingQueueFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,15 @@ public function testAssertPushedWithChainUsingCallback()
$this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
JobStub::class,
], function ($job) {
return $job->parameter == 'second';
return $job->parameter === 'second';
});

try {
$this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
JobStub::class,
JobStub::class,
], function ($job) {
return $job->parameter == 'second';
return $job->parameter === 'second';
});
$this->fail();
} catch (ExpectationFailedException $e) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Testing/ParallelTestingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testOptions()
$this->assertFalse($parallelTesting->option('recreate_databases'));

$parallelTesting->resolveOptionsUsing(function ($option) {
return $option == 'recreate_databases';
return $option === 'recreate_databases';
});

$this->assertFalse($parallelTesting->option('recreate_caches'));
Expand Down
10 changes: 5 additions & 5 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3830,7 +3830,7 @@ public function testSometimesAddingRules()
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
$v->sometimes('x', 'Confirmed', function ($i) {
return $i->x == 'foo';
return $i->x === 'foo';
});
$this->assertEquals(['x' => ['Required', 'Confirmed']], $v->getRules());

Expand All @@ -3844,21 +3844,21 @@ public function testSometimesAddingRules()
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
$v->sometimes('x', 'Confirmed', function ($i) {
return $i->x == 'bar';
return $i->x === 'bar';
});
$this->assertEquals(['x' => ['Required']], $v->getRules());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
$v->sometimes('x', 'Foo|Bar', function ($i) {
return $i->x == 'foo';
return $i->x === 'foo';
});
$this->assertEquals(['x' => ['Required', 'Foo', 'Bar']], $v->getRules());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
$v->sometimes('x', ['Foo', 'Bar:Baz'], function ($i) {
return $i->x == 'foo';
return $i->x === 'foo';
});
$this->assertEquals(['x' => ['Required', 'Foo', 'Bar:Baz']], $v->getRules());

Expand Down Expand Up @@ -3963,7 +3963,7 @@ public function testCustomDependentValidators()
['*.name' => 'dependent_rule:*.age']
);
$v->addDependentExtension('dependent_rule', function ($name) use ($v) {
return Arr::get($v->getData(), $name) == 'Jamie';
return Arr::get($v->getData(), $name) === 'Jamie';
});
$this->assertTrue($v->passes());
}
Expand Down

0 comments on commit 371e7c4

Please sign in to comment.