Skip to content

Commit

Permalink
Chore: add types where safe (#2464)
Browse files Browse the repository at this point in the history
* Use direct method calls over call_user_func_array

* Add return types where safely possible

* Fix styleCI issues
  • Loading branch information
alcaeus authored Nov 14, 2022
1 parent dbde512 commit 560e05e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 115 deletions.
12 changes: 6 additions & 6 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
Expand Down
58 changes: 30 additions & 28 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -101,7 +102,7 @@ public function getSchemaBuilder()
/**
* Get the MongoDB database object.
*
* @return \MongoDB\Database
* @return Database
*/
public function getMongoDB()
{
Expand All @@ -111,7 +112,7 @@ public function getMongoDB()
/**
* return MongoDB object.
*
* @return \MongoDB\Client
* @return Client
*/
public function getMongoClient()
{
Expand All @@ -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)) {
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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)
Expand All @@ -197,21 +199,21 @@ 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'];
}

/**
* 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']];
Expand All @@ -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)
Expand Down Expand Up @@ -285,7 +287,7 @@ protected function getDefaultSchemaGrammar()
/**
* Set database.
*
* @param \MongoDB\Database $db
* @param \MongoDB\Database $db
*/
public function setDatabase(\MongoDB\Database $db)
{
Expand All @@ -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);
}
}
24 changes: 12 additions & 12 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 560e05e

Please sign in to comment.