From 560e05e7d80a5765f2b85ae02a490a17fb3ee6db Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 14 Nov 2022 12:46:55 +0100 Subject: [PATCH] Chore: add types where safe (#2464) * Use direct method calls over call_user_func_array * Add return types where safely possible * Fix styleCI issues --- src/Collection.php | 12 ++-- src/Connection.php | 58 ++++++++++--------- src/Eloquent/Model.php | 24 ++++---- src/Query/Builder.php | 106 ++++++++++++++++++----------------- src/Relations/EmbedsMany.php | 34 +++++------ 5 files changed, 119 insertions(+), 115 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 8acf6afe5..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,14 +35,14 @@ 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($method, $parameters) + public function __call(string $method, array $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..b65b40ca3 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -6,27 +6,28 @@ 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; /** * Create a new database connection instance. * - * @param array $config + * @param array $config */ public function __construct(array $config) { @@ -57,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) @@ -70,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) @@ -82,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) @@ -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() { @@ -129,12 +130,13 @@ 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($dsn, $config) + protected function getDefaultDatabaseName(string $dsn, array $config): string { if (empty($config['database'])) { if (preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) { @@ -150,12 +152,12 @@ protected function getDefaultDatabaseName($dsn, $config) /** * Create a new MongoDB connection. * - * @param string $dsn - * @param array $config - * @param array $options - * @return \MongoDB\Client + * @param string $dsn + * @param array $config + * @param array $options + * @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 = []; @@ -186,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) @@ -197,10 +199,10 @@ protected function hasDsnString(array $config) /** * Get the DSN string form configuration. * - * @param array $config + * @param array $config * @return string */ - protected function getDsnString(array $config) + protected function getDsnString(array $config): string { return $config['dsn']; } @@ -208,10 +210,10 @@ protected function getDsnString(array $config) /** * Get the DSN string for a host / port configuration. * - * @param 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']]; @@ -232,10 +234,10 @@ protected function getHostDsn(array $config) /** * Create a DSN string from a configuration. * - * @param 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) @@ -285,7 +287,7 @@ protected function getDefaultSchemaGrammar() /** * Set database. * - * @param \MongoDB\Database $db + * @param \MongoDB\Database $db */ public function setDatabase(\MongoDB\Database $db) { @@ -295,12 +297,12 @@ 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) { - 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..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) @@ -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..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) @@ -897,7 +899,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' } } - return call_user_func_array('parent::where', $params); + return parent::where(...$params); } /** @@ -905,7 +907,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 ?: []; @@ -999,10 +1001,10 @@ protected function compileWheres() } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereAll(array $where) + protected function compileWhereAll(array $where): array { extract($where); @@ -1010,10 +1012,10 @@ protected function compileWhereAll(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereBasic(array $where) + protected function compileWhereBasic(array $where): array { extract($where); @@ -1066,10 +1068,10 @@ protected function compileWhereBasic(array $where) } /** - * @param array $where + * @param array $where * @return mixed */ - protected function compileWhereNested(array $where) + protected function compileWhereNested(array $where): mixed { extract($where); @@ -1077,10 +1079,10 @@ protected function compileWhereNested(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereIn(array $where) + protected function compileWhereIn(array $where): array { extract($where); @@ -1088,10 +1090,10 @@ protected function compileWhereIn(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereNotIn(array $where) + protected function compileWhereNotIn(array $where): array { extract($where); @@ -1099,10 +1101,10 @@ protected function compileWhereNotIn(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereNull(array $where) + protected function compileWhereNull(array $where): array { $where['operator'] = '='; $where['value'] = null; @@ -1111,10 +1113,10 @@ protected function compileWhereNull(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereNotNull(array $where) + protected function compileWhereNotNull(array $where): array { $where['operator'] = '!='; $where['value'] = null; @@ -1123,10 +1125,10 @@ protected function compileWhereNotNull(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereBetween(array $where) + protected function compileWhereBetween(array $where): array { extract($where); @@ -1156,10 +1158,10 @@ protected function compileWhereBetween(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereDate(array $where) + protected function compileWhereDate(array $where): array { extract($where); @@ -1170,10 +1172,10 @@ protected function compileWhereDate(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereMonth(array $where) + protected function compileWhereMonth(array $where): array { extract($where); @@ -1184,10 +1186,10 @@ protected function compileWhereMonth(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereDay(array $where) + protected function compileWhereDay(array $where): array { extract($where); @@ -1198,10 +1200,10 @@ protected function compileWhereDay(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereYear(array $where) + protected function compileWhereYear(array $where): array { extract($where); @@ -1212,10 +1214,10 @@ protected function compileWhereYear(array $where) } /** - * @param array $where + * @param array $where * @return array */ - protected function compileWhereTime(array $where) + protected function compileWhereTime(array $where): array { extract($where); @@ -1226,10 +1228,10 @@ protected function compileWhereTime(array $where) } /** - * @param array $where + * @param array $where * @return mixed */ - protected function compileWhereRaw(array $where) + protected function compileWhereRaw(array $where): mixed { return $where['sql']; } @@ -1237,7 +1239,7 @@ protected function compileWhereRaw(array $where) /** * Set custom options for the query. * - * @param array $options + * @param array $options * @return $this */ public function options(array $options) @@ -1253,7 +1255,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..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) @@ -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); @@ -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)