Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #1814

Merged
merged 1 commit into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function tokenExpired($createdAt)
$date = $createdAt->toDateTime();
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$createdAt = $date->format('Y-m-d H:i:s');
} elseif (is_array($createdAt) and isset($createdAt['date'])) {
} elseif (is_array($createdAt) && isset($createdAt['date'])) {
$date = new DateTime($createdAt['date'], new DateTimeZone(isset($createdAt['timezone']) ? $createdAt['timezone'] : 'UTC'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$createdAt = $date->format('Y-m-d H:i:s');
Expand Down
3 changes: 1 addition & 2 deletions src/Jenssegers/Mongodb/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ public function raw($expression = null)
*/
protected function addUpdatedAtColumn(array $values)
{
if (! $this->model->usesTimestamps() ||
is_null($this->model->getUpdatedAtColumn())) {
if (! $this->model->usesTimestamps() || $this->model->getUpdatedAtColumn() === null) {
return $values;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if (is_null($relation)) {
if ($relation === null) {
list(, $caller) = debug_backtrace(false);

$relation = $caller['function'];
}

if (is_null($localKey)) {
if ($localKey === null) {
$localKey = $relation;
}

if (is_null($foreignKey)) {
if ($foreignKey === null) {
$foreignKey = Str::snake(class_basename($this));
}

Expand All @@ -57,17 +57,17 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if (is_null($relation)) {
if ($relation === null) {
list(, $caller) = debug_backtrace(false);

$relation = $caller['function'];
}

if (is_null($localKey)) {
if ($localKey === null) {
$localKey = $relation;
}

if (is_null($foreignKey)) {
if ($foreignKey === null) {
$foreignKey = Str::snake(class_basename($this));
}

Expand Down
28 changes: 13 additions & 15 deletions src/Jenssegers/Mongodb/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if (is_null($relation)) {
if ($relation === null) {
list($current, $caller) = debug_backtrace(false, 2);

$relation = $caller['function'];
Expand All @@ -147,7 +147,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
// If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
if (is_null($foreignKey)) {
if ($foreignKey === null) {
$foreignKey = Str::snake($relation) . '_id';
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// If no name is provided, we will use the backtrace to get the function name
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if (is_null($name)) {
if ($name === null) {
list($current, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$name = Str::snake($caller['function']);
Expand All @@ -188,7 +188,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
// there are multiple types in the morph and we can't use single queries.
if (is_null($class = $this->$type)) {
if (($class = $this->$type) === null) {
return new MorphTo(
$this->newQuery(), $this, $id, null, $type, $name
);
Expand All @@ -197,15 +197,13 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// If we are not eager loading the relationship we will essentially treat this
// as a belongs-to style relationship since morph-to extends that class and
// we will pass in the appropriate values so that it behaves as expected.
else {
$class = $this->getActualClassNameForMorph($class);
$class = $this->getActualClassNameForMorph($class);

$instance = new $class;
$instance = new $class;

return new MorphTo(
$instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
);
}
return new MorphTo(
$instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
);
}

/**
Expand All @@ -232,7 +230,7 @@ public function belongsToMany(
// If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
// title of this relation since that is a great convention to apply.
if (is_null($relation)) {
if ($relation === null) {
$relation = $this->guessBelongsToManyRelation();
}

Expand Down Expand Up @@ -261,7 +259,7 @@ public function belongsToMany(
// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
// are transformed to snake case from their default CamelCase also.
if (is_null($collection)) {
if ($collection === null) {
$collection = $instance->getTable();
}

Expand Down Expand Up @@ -303,8 +301,8 @@ public function newEloquentBuilder($query)
{
if (is_subclass_of($this, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return new Builder($query);
} else {
return new EloquentBuilder($query);
}

return new EloquentBuilder($query);
}
}
36 changes: 20 additions & 16 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public function getFresh($columns = [])
// If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving
// all of the columns on the table using the "wildcard" column character.
if (is_null($this->columns)) {
if ($this->columns === null) {
$this->columns = $columns;
}

Expand Down Expand Up @@ -469,7 +469,7 @@ public function aggregate($function, $columns = [])
*/
public function exists()
{
return !is_null($this->first());
return $this->first() !== null;
}

/**
Expand Down Expand Up @@ -580,7 +580,7 @@ public function insertGetId(array $values, $sequence = null)
$result = $this->collection->insertOne($values);

if (1 == (int) $result->isAcknowledged()) {
if (is_null($sequence)) {
if ($sequence === null) {
$sequence = '_id';
}

Expand Down Expand Up @@ -652,7 +652,7 @@ public function forPageAfterId($perPage = 15, $lastId = 0, $column = '_id')
*/
public function pluck($column, $key = null)
{
$results = $this->get(is_null($key) ? [$column] : [$column, $key]);
$results = $this->get($key === null ? [$column] : [$column, $key]);

// Convert ObjectID's to strings
if ($key == '_id') {
Expand All @@ -674,7 +674,7 @@ public function delete($id = null)
// If an ID is passed to the method, we will set the where clause to check
// the ID to allow developers to simply and quickly remove a single row
// from their database without manually specifying the where clauses.
if (!is_null($id)) {
if ($id !== null) {
$this->where('_id', '=', $id);
}

Expand Down Expand Up @@ -730,8 +730,10 @@ public function raw($expression = null)
// Execute the closure on the mongodb collection
if ($expression instanceof Closure) {
return call_user_func($expression, $this->collection);
} // Create an expression for the given value
elseif (!is_null($expression)) {
}

// Create an expression for the given value
if ($expression !== null) {
return new Expression($expression);
}

Expand Down Expand Up @@ -854,7 +856,9 @@ public function convertKey($id)
{
if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) {
return new ObjectID($id);
} elseif (is_string($id) && strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) {
}

if (is_string($id) && strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) {
return new Binary($id, Binary::TYPE_UUID);
}

Expand Down Expand Up @@ -1009,7 +1013,7 @@ protected function compileWhereBasic(array $where)
$regex = '^' . $regex;
}
if (!Str::endsWith($value, '%')) {
$regex = $regex . '$';
$regex .= '$';
}

$value = new Regex($regex, 'i');
Expand Down Expand Up @@ -1121,14 +1125,14 @@ protected function compileWhereBetween(array $where)
],
],
];
} else {
return [
$column => [
'$gte' => $values[0],
'$lte' => $values[1],
],
];
}

return [
$column => [
'$gte' => $values[0],
'$lte' => $values[1],
],
];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Queue/MongoQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function pop($queue = null)
{
$queue = $this->getQueue($queue);

if (!is_null($this->retryAfter)) {
if ($this->retryAfter !== null) {
$this->releaseJobsThatHaveBeenReservedTooLong($queue);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Jenssegers/Mongodb/Relations/EmbedsMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public function associate(Model $model)
{
if (!$this->contains($model)) {
return $this->associateNew($model);
} else {
return $this->associateExisting($model);
}

return $this->associateExisting($model);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ protected function toCollection(array $records = [])
*/
protected function toModel($attributes = [])
{
if (is_null($attributes)) {
if ($attributes === null) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function sparse_and_unique($columns = null, $options = [])
*/
protected function fluent($columns = null)
{
if (is_null($columns)) {
if ($columns === null) {
return $this->columns;
} elseif (is_string($columns)) {
return $this->columns = [$columns];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getCount($collection, $column, $value, $excludeId = null, $idCol
{
$query = $this->table($collection)->where($column, 'regex', "/$value/i");

if (!is_null($excludeId) && $excludeId != 'NULL') {
if ($excludeId !== null && $excludeId != 'NULL') {
$query->where($idColumn ?: 'id', '<>', $excludeId);
}

Expand Down
1 change: 1 addition & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Collection;
Expand Down
17 changes: 10 additions & 7 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php
declare(strict_types=1);

use Illuminate\Support\Facades\DB;

class ConnectionTest extends TestCase
{
public function testConnection()
{
$connection = DB::connection('mongodb');
$this->assertInstanceOf('Jenssegers\Mongodb\Connection', $connection);
$this->assertInstanceOf(\Jenssegers\Mongodb\Connection::class, $connection);
}

public function testReconnect()
Expand All @@ -23,22 +26,22 @@ public function testReconnect()
public function testDb()
{
$connection = DB::connection('mongodb');
$this->assertInstanceOf('MongoDB\Database', $connection->getMongoDB());
$this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB());

$connection = DB::connection('mongodb');
$this->assertInstanceOf('MongoDB\Client', $connection->getMongoClient());
$this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient());
}

public function testCollection()
{
$collection = DB::connection('mongodb')->getCollection('unittest');
$this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection);
$this->assertInstanceOf(Jenssegers\Mongodb\Collection::class, $collection);

$collection = DB::connection('mongodb')->collection('unittests');
$this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', $collection);
$this->assertInstanceOf(Jenssegers\Mongodb\Query\Builder::class, $collection);

$collection = DB::connection('mongodb')->table('unittests');
$this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', $collection);
$this->assertInstanceOf(Jenssegers\Mongodb\Query\Builder::class, $collection);
}

// public function testDynamic()
Expand Down Expand Up @@ -87,7 +90,7 @@ public function testQueryLog()
public function testSchemaBuilder()
{
$schema = DB::connection('mongodb')->getSchemaBuilder();
$this->assertInstanceOf('Jenssegers\Mongodb\Schema\Builder', $schema);
$this->assertInstanceOf(\Jenssegers\Mongodb\Schema\Builder::class, $schema);
}

public function testDriverName()
Expand Down
1 change: 1 addition & 0 deletions tests/DsnTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

class DsnTest extends TestCase
{
Expand Down
Loading