From 67586d2a9232bb09a0c8ce91a1aaf6b310f436c7 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 14 Nov 2022 10:40:30 +0100 Subject: [PATCH 1/3] Use direct method calls over call_user_func_array --- src/Collection.php | 2 +- src/Connection.php | 2 +- src/Eloquent/Model.php | 2 +- src/Query/Builder.php | 4 ++-- src/Relations/EmbedsMany.php | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 8acf6afe5..058498ad8 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -42,7 +42,7 @@ public function __construct(Connection $connection, MongoCollection $collection) public function __call($method, $parameters) { $start = microtime(true); - $result = call_user_func_array([$this->collection, $method], $parameters); + $result = $this->collection->$method(...$parameters); // Once we have run the query we will calculate the time that it took to run and // then log the query, bindings, and execution time so we will report them on diff --git a/src/Connection.php b/src/Connection.php index 57d9d3e37..d5f24100d 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -301,6 +301,6 @@ public function setDatabase(\MongoDB\Database $db) */ public function __call($method, $parameters) { - return call_user_func_array([$this->db, $method], $parameters); + return $this->db->$method(...$parameters); } } diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php index 5836cf83d..e5a2283c2 100644 --- a/src/Eloquent/Model.php +++ b/src/Eloquent/Model.php @@ -510,7 +510,7 @@ public function __call($method, $parameters) { // Unset method if ($method == 'unset') { - return call_user_func_array([$this, 'drop'], $parameters); + return $this->drop(...$parameters); } return parent::__call($method, $parameters); diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 6412ab603..672e8f5e0 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -897,7 +897,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' } } - return call_user_func_array('parent::where', $params); + return parent::where(...$params); } /** @@ -1253,7 +1253,7 @@ public function options(array $options) public function __call($method, $parameters) { if ($method == 'unset') { - return call_user_func_array([$this, 'drop'], $parameters); + return $this->drop(...$parameters); } return parent::__call($method, $parameters); diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index 88a63d0b4..049ab3e44 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -335,7 +335,7 @@ protected function setEmbedded($models) public function __call($method, $parameters) { if (method_exists(Collection::class, $method)) { - return call_user_func_array([$this->getResults(), $method], $parameters); + return $this->getResults()->$method(...$parameters); } return parent::__call($method, $parameters); From f572ae69276258c1c9fccf4729e333433d7d8506 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 10 Nov 2022 14:33:35 +0100 Subject: [PATCH 2/3] Add return types where safely possible --- src/Collection.php | 2 +- src/Connection.php | 21 +++++++++++---------- src/Query/Builder.php | 30 +++++++++++++++--------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 058498ad8..072e55112 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -39,7 +39,7 @@ public function __construct(Connection $connection, MongoCollection $collection) * @param array $parameters * @return mixed */ - public function __call($method, $parameters) + public function __call(string $method, array $parameters) { $start = microtime(true); $result = $this->collection->$method(...$parameters); diff --git a/src/Connection.php b/src/Connection.php index d5f24100d..bb9b89140 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -6,20 +6,21 @@ use Illuminate\Support\Arr; use InvalidArgumentException; use MongoDB\Client; +use MongoDB\Database; class Connection extends BaseConnection { /** * The MongoDB database handler. * - * @var \MongoDB\Database + * @var Database */ protected $db; /** * The MongoDB connection handler. * - * @var \MongoDB\Client + * @var Client */ protected $connection; @@ -101,7 +102,7 @@ public function getSchemaBuilder() /** * Get the MongoDB database object. * - * @return \MongoDB\Database + * @return Database */ public function getMongoDB() { @@ -111,7 +112,7 @@ public function getMongoDB() /** * return MongoDB object. * - * @return \MongoDB\Client + * @return Client */ public function getMongoClient() { @@ -134,7 +135,7 @@ public function getDatabaseName() * @return string * @throws InvalidArgumentException */ - protected function getDefaultDatabaseName($dsn, $config) + protected function getDefaultDatabaseName(string $dsn, array $config): string { if (empty($config['database'])) { if (preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) { @@ -153,9 +154,9 @@ protected function getDefaultDatabaseName($dsn, $config) * @param string $dsn * @param array $config * @param array $options - * @return \MongoDB\Client + * @return Client */ - protected function createConnection($dsn, array $config, array $options) + protected function createConnection($dsn, array $config, array $options): Client { // By default driver options is an empty array. $driverOptions = []; @@ -200,7 +201,7 @@ protected function hasDsnString(array $config) * @param array $config * @return string */ - protected function getDsnString(array $config) + protected function getDsnString(array $config): string { return $config['dsn']; } @@ -211,7 +212,7 @@ protected function getDsnString(array $config) * @param array $config * @return string */ - protected function getHostDsn(array $config) + protected function getHostDsn(array $config): string { // Treat host option as array of hosts $hosts = is_array($config['host']) ? $config['host'] : [$config['host']]; @@ -235,7 +236,7 @@ protected function getHostDsn(array $config) * @param array $config * @return string */ - protected function getDsn(array $config) + protected function getDsn(array $config): string { return $this->hasDsnString($config) ? $this->getDsnString($config) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 672e8f5e0..4447a5cd1 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -905,7 +905,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' * * @return array */ - protected function compileWheres() + protected function compileWheres(): array { // The wheres to compile. $wheres = $this->wheres ?: []; @@ -1002,7 +1002,7 @@ protected function compileWheres() * @param array $where * @return array */ - protected function compileWhereAll(array $where) + protected function compileWhereAll(array $where): array { extract($where); @@ -1013,7 +1013,7 @@ protected function compileWhereAll(array $where) * @param array $where * @return array */ - protected function compileWhereBasic(array $where) + protected function compileWhereBasic(array $where): array { extract($where); @@ -1069,7 +1069,7 @@ protected function compileWhereBasic(array $where) * @param array $where * @return mixed */ - protected function compileWhereNested(array $where) + protected function compileWhereNested(array $where): mixed { extract($where); @@ -1080,7 +1080,7 @@ protected function compileWhereNested(array $where) * @param array $where * @return array */ - protected function compileWhereIn(array $where) + protected function compileWhereIn(array $where): array { extract($where); @@ -1091,7 +1091,7 @@ protected function compileWhereIn(array $where) * @param array $where * @return array */ - protected function compileWhereNotIn(array $where) + protected function compileWhereNotIn(array $where): array { extract($where); @@ -1102,7 +1102,7 @@ protected function compileWhereNotIn(array $where) * @param array $where * @return array */ - protected function compileWhereNull(array $where) + protected function compileWhereNull(array $where): array { $where['operator'] = '='; $where['value'] = null; @@ -1114,7 +1114,7 @@ protected function compileWhereNull(array $where) * @param array $where * @return array */ - protected function compileWhereNotNull(array $where) + protected function compileWhereNotNull(array $where): array { $where['operator'] = '!='; $where['value'] = null; @@ -1126,7 +1126,7 @@ protected function compileWhereNotNull(array $where) * @param array $where * @return array */ - protected function compileWhereBetween(array $where) + protected function compileWhereBetween(array $where): array { extract($where); @@ -1159,7 +1159,7 @@ protected function compileWhereBetween(array $where) * @param array $where * @return array */ - protected function compileWhereDate(array $where) + protected function compileWhereDate(array $where): array { extract($where); @@ -1173,7 +1173,7 @@ protected function compileWhereDate(array $where) * @param array $where * @return array */ - protected function compileWhereMonth(array $where) + protected function compileWhereMonth(array $where): array { extract($where); @@ -1187,7 +1187,7 @@ protected function compileWhereMonth(array $where) * @param array $where * @return array */ - protected function compileWhereDay(array $where) + protected function compileWhereDay(array $where): array { extract($where); @@ -1201,7 +1201,7 @@ protected function compileWhereDay(array $where) * @param array $where * @return array */ - protected function compileWhereYear(array $where) + protected function compileWhereYear(array $where): array { extract($where); @@ -1215,7 +1215,7 @@ protected function compileWhereYear(array $where) * @param array $where * @return array */ - protected function compileWhereTime(array $where) + protected function compileWhereTime(array $where): array { extract($where); @@ -1229,7 +1229,7 @@ protected function compileWhereTime(array $where) * @param array $where * @return mixed */ - protected function compileWhereRaw(array $where) + protected function compileWhereRaw(array $where): mixed { return $where['sql']; } From 1c3f7dcbe643f27b185c3e7b893807eb8e70cc5b Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 14 Nov 2022 12:41:06 +0100 Subject: [PATCH 3/3] Fix styleCI issues --- src/Collection.php | 8 ++-- src/Connection.php | 35 +++++++++--------- src/Eloquent/Model.php | 22 +++++------ src/Query/Builder.php | 72 ++++++++++++++++++------------------ src/Relations/EmbedsMany.php | 32 ++++++++-------- 5 files changed, 86 insertions(+), 83 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 072e55112..3980e8de6 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -23,8 +23,8 @@ class Collection protected $collection; /** - * @param Connection $connection - * @param MongoCollection $collection + * @param Connection $connection + * @param MongoCollection $collection */ public function __construct(Connection $connection, MongoCollection $collection) { @@ -35,8 +35,8 @@ public function __construct(Connection $connection, MongoCollection $collection) /** * Handle dynamic method calls. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call(string $method, array $parameters) diff --git a/src/Connection.php b/src/Connection.php index bb9b89140..b65b40ca3 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -27,7 +27,7 @@ class Connection extends BaseConnection /** * Create a new database connection instance. * - * @param array $config + * @param array $config */ public function __construct(array $config) { @@ -58,7 +58,7 @@ public function __construct(array $config) /** * Begin a fluent query against a database collection. * - * @param string $collection + * @param string $collection * @return Query\Builder */ public function collection($collection) @@ -71,8 +71,8 @@ public function collection($collection) /** * Begin a fluent query against a database collection. * - * @param string $table - * @param string|null $as + * @param string $table + * @param string|null $as * @return Query\Builder */ public function table($table, $as = null) @@ -83,7 +83,7 @@ public function table($table, $as = null) /** * Get a MongoDB collection. * - * @param string $name + * @param string $name * @return Collection */ public function getCollection($name) @@ -130,9 +130,10 @@ public function getDatabaseName() /** * Get the name of the default database based on db config or try to detect it from dsn. * - * @param string $dsn - * @param array $config + * @param string $dsn + * @param array $config * @return string + * * @throws InvalidArgumentException */ protected function getDefaultDatabaseName(string $dsn, array $config): string @@ -151,9 +152,9 @@ protected function getDefaultDatabaseName(string $dsn, array $config): string /** * Create a new MongoDB connection. * - * @param string $dsn - * @param array $config - * @param array $options + * @param string $dsn + * @param array $config + * @param array $options * @return Client */ protected function createConnection($dsn, array $config, array $options): Client @@ -187,7 +188,7 @@ public function disconnect() /** * Determine if the given configuration array has a dsn string. * - * @param array $config + * @param array $config * @return bool */ protected function hasDsnString(array $config) @@ -198,7 +199,7 @@ protected function hasDsnString(array $config) /** * Get the DSN string form configuration. * - * @param array $config + * @param array $config * @return string */ protected function getDsnString(array $config): string @@ -209,7 +210,7 @@ protected function getDsnString(array $config): string /** * Get the DSN string for a host / port configuration. * - * @param array $config + * @param array $config * @return string */ protected function getHostDsn(array $config): string @@ -233,7 +234,7 @@ protected function getHostDsn(array $config): string /** * Create a DSN string from a configuration. * - * @param array $config + * @param array $config * @return string */ protected function getDsn(array $config): string @@ -286,7 +287,7 @@ protected function getDefaultSchemaGrammar() /** * Set database. * - * @param \MongoDB\Database $db + * @param \MongoDB\Database $db */ public function setDatabase(\MongoDB\Database $db) { @@ -296,8 +297,8 @@ public function setDatabase(\MongoDB\Database $db) /** * Dynamically pass methods to the connection. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php index e5a2283c2..576d8b36b 100644 --- a/src/Eloquent/Model.php +++ b/src/Eloquent/Model.php @@ -50,7 +50,7 @@ abstract class Model extends BaseModel /** * Custom accessor for the model's id. * - * @param mixed $value + * @param mixed $value * @return mixed */ public function getIdAttribute($value = null) @@ -279,7 +279,7 @@ public function originalIsEquivalent($key) /** * Remove one or more fields. * - * @param mixed $columns + * @param mixed $columns * @return int */ public function drop($columns) @@ -325,8 +325,8 @@ public function push() /** * Remove one or more values from an array. * - * @param string $column - * @param mixed $values + * @param string $column + * @param mixed $values * @return mixed */ public function pull($column, $values) @@ -344,9 +344,9 @@ public function pull($column, $values) /** * Append one or more values to the underlying attribute value and sync with original. * - * @param string $column - * @param array $values - * @param bool $unique + * @param string $column + * @param array $values + * @param bool $unique */ protected function pushAttributeValues($column, array $values, $unique = false) { @@ -369,8 +369,8 @@ protected function pushAttributeValues($column, array $values, $unique = false) /** * Remove one or more values to the underlying attribute value and sync with original. * - * @param string $column - * @param array $values + * @param string $column + * @param array $values */ protected function pullAttributeValues($column, array $values) { @@ -402,7 +402,7 @@ public function getForeignKey() /** * Set the parent relation. * - * @param \Illuminate\Database\Eloquent\Relations\Relation $relation + * @param \Illuminate\Database\Eloquent\Relations\Relation $relation */ public function setParentRelation(Relation $relation) { @@ -495,7 +495,7 @@ protected function getRelationsWithoutParent() * Checks if column exists on a table. As this is a document model, just return true. This also * prevents calls to non-existent function Grammar::compileColumnListing(). * - * @param string $key + * @param string $key * @return bool */ protected function isGuardableColumn($key) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 4447a5cd1..631e64950 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -140,7 +140,7 @@ public function __construct(Connection $connection, Processor $processor) /** * Set the projections. * - * @param array $columns + * @param array $columns * @return $this */ public function project($columns) @@ -152,7 +152,8 @@ public function project($columns) /** * Set the cursor timeout in seconds. - * @param int $seconds + * + * @param int $seconds * @return $this */ public function timeout($seconds) @@ -165,7 +166,7 @@ public function timeout($seconds) /** * Set the cursor hint. * - * @param mixed $index + * @param mixed $index * @return $this */ public function hint($index) @@ -216,8 +217,8 @@ public function cursor($columns = []) /** * Execute the query as a fresh "select" statement. * - * @param array $columns - * @param bool $returnLazy + * @param array $columns + * @param bool $returnLazy * @return array|static[]|Collection|LazyCollection */ public function getFresh($columns = [], $returnLazy = false) @@ -521,10 +522,10 @@ public function orderBy($column, $direction = 'asc') /** * Add a "where all" clause to the query. * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not + * @param string $column + * @param array $values + * @param string $boolean + * @param bool $not * @return $this */ public function whereAll($column, array $values, $boolean = 'and', $not = false) @@ -728,9 +729,10 @@ public function truncate(): bool /** * Get an array with the values of a given column. * - * @param string $column - * @param string $key + * @param string $column + * @param string $key * @return array + * * @deprecated */ public function lists($column, $key = null) @@ -760,9 +762,9 @@ public function raw($expression = null) /** * Append one or more values to an array. * - * @param mixed $column - * @param mixed $value - * @param bool $unique + * @param mixed $column + * @param mixed $value + * @param bool $unique * @return int */ public function push($column, $value = null, $unique = false) @@ -787,8 +789,8 @@ public function push($column, $value = null, $unique = false) /** * Remove one or more values from an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function pull($column, $value = null) @@ -811,7 +813,7 @@ public function pull($column, $value = null) /** * Remove one or more fields. * - * @param mixed $columns + * @param mixed $columns * @return int */ public function drop($columns) @@ -842,8 +844,8 @@ public function newQuery() /** * Perform an update query. * - * @param array $query - * @param array $options + * @param array $query + * @param array $options * @return int */ protected function performUpdate($query, array $options = []) @@ -865,7 +867,7 @@ protected function performUpdate($query, array $options = []) /** * Convert a key to ObjectID if needed. * - * @param mixed $id + * @param mixed $id * @return mixed */ public function convertKey($id) @@ -999,7 +1001,7 @@ protected function compileWheres(): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereAll(array $where): array @@ -1010,7 +1012,7 @@ protected function compileWhereAll(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereBasic(array $where): array @@ -1066,7 +1068,7 @@ protected function compileWhereBasic(array $where): array } /** - * @param array $where + * @param array $where * @return mixed */ protected function compileWhereNested(array $where): mixed @@ -1077,7 +1079,7 @@ protected function compileWhereNested(array $where): mixed } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereIn(array $where): array @@ -1088,7 +1090,7 @@ protected function compileWhereIn(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereNotIn(array $where): array @@ -1099,7 +1101,7 @@ protected function compileWhereNotIn(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereNull(array $where): array @@ -1111,7 +1113,7 @@ protected function compileWhereNull(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereNotNull(array $where): array @@ -1123,7 +1125,7 @@ protected function compileWhereNotNull(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereBetween(array $where): array @@ -1156,7 +1158,7 @@ protected function compileWhereBetween(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereDate(array $where): array @@ -1170,7 +1172,7 @@ protected function compileWhereDate(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereMonth(array $where): array @@ -1184,7 +1186,7 @@ protected function compileWhereMonth(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereDay(array $where): array @@ -1198,7 +1200,7 @@ protected function compileWhereDay(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereYear(array $where): array @@ -1212,7 +1214,7 @@ protected function compileWhereYear(array $where): array } /** - * @param array $where + * @param array $where * @return array */ protected function compileWhereTime(array $where): array @@ -1226,7 +1228,7 @@ protected function compileWhereTime(array $where): array } /** - * @param array $where + * @param array $where * @return mixed */ protected function compileWhereRaw(array $where): mixed @@ -1237,7 +1239,7 @@ protected function compileWhereRaw(array $where): mixed /** * Set custom options for the query. * - * @param array $options + * @param array $options * @return $this */ public function options(array $options) diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index 049ab3e44..ba1513255 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -34,7 +34,7 @@ public function getResults() /** * Save a new model and attach it to the parent model. * - * @param Model $model + * @param Model $model * @return Model|bool */ public function performInsert(Model $model) @@ -65,7 +65,7 @@ public function performInsert(Model $model) /** * Save an existing model and attach it to the parent model. * - * @param Model $model + * @param Model $model * @return Model|bool */ public function performUpdate(Model $model) @@ -97,7 +97,7 @@ public function performUpdate(Model $model) /** * Delete an existing model and detach it from the parent model. * - * @param Model $model + * @param Model $model * @return int */ public function performDelete(Model $model) @@ -124,7 +124,7 @@ public function performDelete(Model $model) /** * Associate the model instance to the given parent, without saving it to the database. * - * @param Model $model + * @param Model $model * @return Model */ public function associate(Model $model) @@ -139,7 +139,7 @@ public function associate(Model $model) /** * Dissociate the model instance from the given parent, without saving it to the database. * - * @param mixed $ids + * @param mixed $ids * @return int */ public function dissociate($ids = []) @@ -168,7 +168,7 @@ public function dissociate($ids = []) /** * Destroy the embedded models for the given IDs. * - * @param mixed $ids + * @param mixed $ids * @return int */ public function destroy($ids = []) @@ -210,7 +210,7 @@ public function delete() /** * Destroy alias. * - * @param mixed $ids + * @param mixed $ids * @return int */ public function detach($ids = []) @@ -221,7 +221,7 @@ public function detach($ids = []) /** * Save alias. * - * @param Model $model + * @param Model $model * @return Model */ public function attach(Model $model) @@ -232,7 +232,7 @@ public function attach(Model $model) /** * Associate a new model instance to the given parent, without saving it to the database. * - * @param Model $model + * @param Model $model * @return Model */ protected function associateNew($model) @@ -253,7 +253,7 @@ protected function associateNew($model) /** * Associate an existing model instance to the given parent, without saving it to the database. * - * @param Model $model + * @param Model $model * @return Model */ protected function associateExisting($model) @@ -277,10 +277,10 @@ protected function associateExisting($model) } /** - * @param null $perPage - * @param array $columns - * @param string $pageName - * @param null $page + * @param null $perPage + * @param array $columns + * @param string $pageName + * @param null $page * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) @@ -344,8 +344,8 @@ public function __call($method, $parameters) /** * Get the name of the "where in" method for eager loading. * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key)