From a05317c57103aad6e2ed111572ad3911aa85ac4d Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Sat, 12 Mar 2016 23:58:23 +0100 Subject: [PATCH] Drop PHP 5.5 support --- .travis.yml | 1 - composer.json | 2 +- .../ActiveRecordClassNotValid.php | 4 +- lib/ActiveRecord/BelongsToRelation.php | 4 +- .../ConnectionAlreadyEstablished.php | 4 +- lib/ActiveRecord/ConnectionNotDefined.php | 4 +- lib/ActiveRecord/HasManyRelation.php | 4 +- lib/ActiveRecord/Model.php | 25 ++++++-- lib/ActiveRecord/Query.php | 62 ++++++++----------- lib/ActiveRecord/Statement.php | 17 ++--- lib/ActiveRecord/UnableToSetFetchMode.php | 4 +- 11 files changed, 74 insertions(+), 57 deletions(-) diff --git a/.travis.yml b/.travis.yml index 511fc7f..22c9fee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ sudo: false language: php php: - - 5.5 - 5.6 - 7.0 - hhvm diff --git a/composer.json b/composer.json index 8182735..948b1f3 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "minimum-stability": "dev", "require": { - "php": ">=5.5.0", + "php": ">=5.6.0", "ext-pdo": "*", "icanboogie/prototype": "^2.3", "icanboogie/inflector": "^1.4", diff --git a/lib/ActiveRecord/ActiveRecordClassNotValid.php b/lib/ActiveRecord/ActiveRecordClassNotValid.php index 7c4dc34..056dea9 100644 --- a/lib/ActiveRecord/ActiveRecordClassNotValid.php +++ b/lib/ActiveRecord/ActiveRecordClassNotValid.php @@ -13,6 +13,8 @@ use ICanBoogie\Accessor\AccessorTrait; +use function ICanBoogie\format; + /** * Exception thrown when the ActiveRecord class is not valid. * @@ -57,7 +59,7 @@ public function __construct($class, $message = null, $code = 500, \Exception $pr */ protected function format_message($class) { - return \ICanBoogie\format("ActiveRecord class is not valid: %class", [ + return format("ActiveRecord class is not valid: %class", [ 'class' => $class diff --git a/lib/ActiveRecord/BelongsToRelation.php b/lib/ActiveRecord/BelongsToRelation.php index d057728..a32b31e 100644 --- a/lib/ActiveRecord/BelongsToRelation.php +++ b/lib/ActiveRecord/BelongsToRelation.php @@ -14,6 +14,8 @@ use ICanBoogie\ActiveRecord; use ICanBoogie\Prototype; +use function ICanBoogie\singularize; + /** * Representation of the one-to-one relation. */ @@ -80,6 +82,6 @@ protected function alter_prototype(Prototype $prototype, $property) */ protected function resolve_property_name($related) { - return \ICanBoogie\singularize(parent::resolve_property_name($related)); + return singularize(parent::resolve_property_name($related)); } } diff --git a/lib/ActiveRecord/ConnectionAlreadyEstablished.php b/lib/ActiveRecord/ConnectionAlreadyEstablished.php index 523b696..577caa3 100644 --- a/lib/ActiveRecord/ConnectionAlreadyEstablished.php +++ b/lib/ActiveRecord/ConnectionAlreadyEstablished.php @@ -13,6 +13,8 @@ use ICanBoogie\Accessor\AccessorTrait; +use function ICanBoogie\format; + /** * Exception thrown in attempt to set the definition of an already established connection. * @@ -56,7 +58,7 @@ public function __construct($id, $code = 500, \Exception $previous = null) */ protected function format_message($id) { - return \ICanBoogie\format("Connection already established: %id.", [ + return format("Connection already established: %id.", [ 'id' => $id diff --git a/lib/ActiveRecord/ConnectionNotDefined.php b/lib/ActiveRecord/ConnectionNotDefined.php index 3be6da0..408008e 100644 --- a/lib/ActiveRecord/ConnectionNotDefined.php +++ b/lib/ActiveRecord/ConnectionNotDefined.php @@ -13,6 +13,8 @@ use ICanBoogie\Accessor\AccessorTrait; +use function ICanBoogie\format; + /** * Exception thrown in attempt to obtain a connection that is not defined. * @@ -53,7 +55,7 @@ public function __construct($id, $code = 500, \Exception $previous = null) */ protected function format_message($id) { - return \ICanBoogie\format("Connection not defined: %id.", [ + return format("Connection not defined: %id.", [ 'id' => $id diff --git a/lib/ActiveRecord/HasManyRelation.php b/lib/ActiveRecord/HasManyRelation.php index 5599152..9192fff 100644 --- a/lib/ActiveRecord/HasManyRelation.php +++ b/lib/ActiveRecord/HasManyRelation.php @@ -13,6 +13,8 @@ use ICanBoogie\ActiveRecord; +use function ICanBoogie\pluralize; + /** * Representation of the one-to-many relation. */ @@ -34,6 +36,6 @@ public function __invoke(ActiveRecord $record) protected function resolve_property_name($related) { - return \ICanBoogie\pluralize(parent::resolve_property_name($related)); + return pluralize(parent::resolve_property_name($related)); } } diff --git a/lib/ActiveRecord/Model.php b/lib/ActiveRecord/Model.php index 9b383b8..75d0d0d 100644 --- a/lib/ActiveRecord/Model.php +++ b/lib/ActiveRecord/Model.php @@ -35,7 +35,7 @@ * @method ActiveRecord one() one() The method is forwarded to {@link Query::one}. * @method ActiveRecord new() new(array $properties = []) Instantiate a new record. * - * @method Model belongs_to() belongs_to($definition) Add a _belongs_to_ relation. + * @method Model belongs_to() belongs_to(...$args) Adds a _belongs_to_ relation. * @method Model has_many() has_many($related, $options = []) Adds a _has_many_ relation. * * @property-read Model|null $parent Parent model. @@ -163,6 +163,21 @@ public function __construct(ModelCollection $models, array $attributes) $this->resolve_relations(); } + // @codeCoverageIgnoreStart + public function __debugInfo() + { + return [ + + 'id' => $this->id, + 'name' => "$this->name ($this->unprefixed_name)", + 'parent' => $this->parent ? $this->parent->id . " of " . get_class($this->parent) : null, + 'parent_model' => $this->parent_model ? $this->parent_model->id . " of " . get_class($this->parent_model) : null, + 'relations' => $this->relations + + ]; + } + // @codeCoverageIgnoreEnd + /** * Resolves constructor attributes. * @@ -259,7 +274,7 @@ public function __call($method, $arguments) { if ($method == 'new') { - return call_user_func_array([ $this, 'new_record' ], $arguments); + return $this->new_record(...$arguments); } if (is_callable([ Query::class, $method ]) @@ -268,12 +283,12 @@ public function __call($method, $arguments) { $query = new Query($this); - return call_user_func_array([ $query, $method ], $arguments); + return $query->$method(...$arguments); } if (is_callable([ RelationCollection::class, $method ])) { - return call_user_func_array([ $this->relations, $method ], $arguments); + return $this->relations->$method(...$arguments); } return parent::__call($method, $arguments); @@ -542,7 +557,7 @@ public function scope($scope_name, array $scope_args = []) { try { - return call_user_func_array([ $this, 'scope_' . $scope_name ], $scope_args); + return $this->{ 'scope_' . $scope_name}(...$scope_args); } catch (MethodNotDefined $e) { diff --git a/lib/ActiveRecord/Query.php b/lib/ActiveRecord/Query.php index 216e34c..74291bd 100644 --- a/lib/ActiveRecord/Query.php +++ b/lib/ActiveRecord/Query.php @@ -243,7 +243,7 @@ public function __call($method, $arguments) { if ($method === 'and') { - return call_user_func_array([ $this, 'where' ], $arguments); + return $this->where(...$arguments); } if (strpos($method, 'filter_by_') === 0) @@ -687,13 +687,14 @@ private function render_join_on($column, $as, Query $query) * * {@link \DateTime} conditions are converted to strings. * + * @param $conditions_and_args + * * @return array An array made of the condition string and its arguments. */ - private function deferred_parse_conditions() + private function deferred_parse_conditions(...$conditions_and_args) { - $args = debug_backtrace(0, 2)[1]['args']; - - $conditions = array_shift($args); + $conditions = array_shift($conditions_and_args); + $args = $conditions_and_args; if (is_array($conditions)) { @@ -832,15 +833,13 @@ private function dynamic_filter($filter, array $conditions_args = []) * * This will return the orders with the `order_count` different than 2. * - * @param mixed $conditions - * @param mixed $conditions_args - * @param mixed $_ [optional] + * @param mixed ...$conditions_and_args * * @return Query */ - public function where($conditions, $conditions_args = null, $_ = null) + public function where(...$conditions_and_args) { - list($conditions, $conditions_args) = $this->deferred_parse_conditions(); + list($conditions, $conditions_args) = $this->deferred_parse_conditions(...$conditions_and_args); if ($conditions) { @@ -888,19 +887,13 @@ public function group($group) /** * Defines the `HAVING` clause. * - * @param mixed $conditions - * @param mixed $conditions_args + * @param mixed ...$conditions_and_args * * @return Query */ - public function having($conditions, $conditions_args = null) + public function having(...$conditions_and_args) { - if (!$this->group) - { - throw new \LogicException("having() cannot be used without invoking group() first."); - } - - list($having, $having_args) = $this->deferred_parse_conditions(); + list($having, $having_args) = $this->deferred_parse_conditions(...$conditions_and_args); $this->having = $having; $this->having_args = $having_args; @@ -1014,17 +1007,17 @@ public function query() */ /** - * Resolves fetch mode from backtrace. + * Resolves fetch mode. + * + * @param mixed ...$mode * * @return array */ - private function resolve_fetch_mode() + private function resolve_fetch_mode(...$mode) { - $trace = debug_backtrace(0, 2); - - if ($trace[1]['args']) + if ($mode) { - $args = $trace[1]['args']; + $args = $mode; } else if ($this->mode) { @@ -1049,16 +1042,13 @@ private function resolve_fetch_mode() /** * Execute the query and returns an array of records. * - * @param mixed $_ [optional] + * @param mixed ...$mode Fetch mode. * * @return array */ - public function all($_ = null) + public function all(...$mode) { - $statement = $this->query(); - $args = $this->resolve_fetch_mode(); - - return call_user_func_array([ $statement, 'fetchAll' ], $args); + return $this->query()->fetchAll(...$this->resolve_fetch_mode(...$mode)); } /** @@ -1074,30 +1064,30 @@ protected function get_all() /** * Return the first result of the query and close the cursor. * - * @param $_ [optional] Fetch mode. + * @param mixed ...$mode Fetch node. * * @return mixed The return value of this function on success depends on the fetch mode. In * all cases, FALSE is returned on failure. */ - public function one($_ = null) + public function one(...$mode) { $query = clone $this; $query->limit = 1; $statement = $query->query(); - $args = $query->resolve_fetch_mode(); + $args = $query->resolve_fetch_mode(...$mode); if (count($args) > 1 && $args[0] == \PDO::FETCH_CLASS) { array_shift($args); - $rc = call_user_func_array([ $statement, 'fetchObject' ], $args); + $rc = $statement->fetchObject(...$args); $statement->closeCursor(); return $rc; } - return call_user_func_array([ $statement, 'one' ], $args); + return $statement->one(...$args); } /** diff --git a/lib/ActiveRecord/Statement.php b/lib/ActiveRecord/Statement.php index 963a5fb..8722d3c 100644 --- a/lib/ActiveRecord/Statement.php +++ b/lib/ActiveRecord/Statement.php @@ -120,11 +120,9 @@ public function execute($args = []) * * @see http://www.php.net/manual/en/pdostatement.setfetchmode.php */ - public function mode($mode) + public function mode(...$mode) { - $mode = func_get_args(); - - if (!call_user_func_array([ $this, 'setFetchMode' ], $mode)) + if (!$this->setFetchMode(...$mode)) { throw new UnableToSetFetchMode($mode); } @@ -145,8 +143,7 @@ public function mode($mode) */ public function one($fetch_style = \PDO::FETCH_BOTH, $cursor_orientation = \PDO::FETCH_ORI_NEXT, $cursor_offset = 0) { - $args = func_get_args(); - $rc = call_user_func_array([ $this, 'fetch' ], $args); + $rc = $this->fetch(...func_get_args()); $this->closeCursor(); @@ -195,10 +192,14 @@ protected function get_rc() /** * Alias for {@link \PDOStatement::fetchAll()} + * + * @param mixed $mode + * + * @return array */ - public function all($fetch_style = null, $column_index = null, array $ctor_args = null) + public function all(...$mode) { - return call_user_func_array([ $this, 'fetchAll' ], func_get_args()); + return $this->fetchAll(...$mode); } /** diff --git a/lib/ActiveRecord/UnableToSetFetchMode.php b/lib/ActiveRecord/UnableToSetFetchMode.php index d6db388..51ec83a 100644 --- a/lib/ActiveRecord/UnableToSetFetchMode.php +++ b/lib/ActiveRecord/UnableToSetFetchMode.php @@ -13,6 +13,8 @@ use ICanBoogie\Accessor\AccessorTrait; +use function ICanBoogie\format; + /** * Exception thrown when the fetch mode of a statement fails to be set. * @@ -54,6 +56,6 @@ public function __construct($mode, $message = null, $code = 500, \Exception $pre */ protected function format_message($mode) { - return \ICanBoogie\format("Unable to set fetch mode: %mode", [ 'mode' => $mode ]); + return format("Unable to set fetch mode: %mode", [ 'mode' => $mode ]); } }