From dfece0870967cbbc250e23645f21454670ff6d50 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Sun, 13 Mar 2016 23:12:13 +0100 Subject: [PATCH] Drop PHP 5.6 support --- .travis.yml | 5 +- README.md | 2 +- composer.json | 2 +- lib/ActiveRecord.php | 10 +- lib/ActiveRecord/ActiveRecordCache.php | 4 +- .../ActiveRecordClassNotValid.php | 12 +- lib/ActiveRecord/BelongsToRelation.php | 6 +- lib/ActiveRecord/Connection.php | 51 ++++--- .../ConnectionAlreadyEstablished.php | 6 +- lib/ActiveRecord/ConnectionCollection.php | 12 +- lib/ActiveRecord/ConnectionNotDefined.php | 9 +- lib/ActiveRecord/ConnectionNotEstablished.php | 7 +- lib/ActiveRecord/ConnectionOptions.php | 2 +- lib/ActiveRecord/CreatedAtProperty.php | 2 +- lib/ActiveRecord/DateProperty.php | 2 +- lib/ActiveRecord/DateTimeProperty.php | 2 +- lib/ActiveRecord/DateTimePropertySupport.php | 2 +- lib/ActiveRecord/Driver.php | 12 +- lib/ActiveRecord/Driver/BasicDriver.php | 8 +- lib/ActiveRecord/Driver/MySQLDriver.php | 14 +- lib/ActiveRecord/Driver/SQLiteDriver.php | 8 +- lib/ActiveRecord/DriverNotDefined.php | 6 +- lib/ActiveRecord/FinishAtProperty.php | 2 +- lib/ActiveRecord/FinishedAtProperty.php | 2 +- lib/ActiveRecord/HasManyRelation.php | 7 +- lib/ActiveRecord/Model.php | 141 +++++++++--------- lib/ActiveRecord/ModelAlreadyInstantiated.php | 13 +- lib/ActiveRecord/ModelCollection.php | 27 ++-- lib/ActiveRecord/ModelNotDefined.php | 9 +- lib/ActiveRecord/Query.php | 128 ++++++++-------- lib/ActiveRecord/RecordNotFound.php | 9 +- lib/ActiveRecord/Relation.php | 6 +- lib/ActiveRecord/RelationCollection.php | 11 +- lib/ActiveRecord/RelationNotDefined.php | 12 +- lib/ActiveRecord/RuntimeActiveRecordCache.php | 4 +- lib/ActiveRecord/Schema.php | 10 +- lib/ActiveRecord/SchemaColumn.php | 30 ++-- lib/ActiveRecord/ScopeNotDefined.php | 14 +- lib/ActiveRecord/StartAtProperty.php | 2 +- lib/ActiveRecord/StartedAtProperty.php | 2 +- lib/ActiveRecord/Statement.php | 44 ++---- .../StatementInvocationFailed.php | 16 +- lib/ActiveRecord/StatementNotValid.php | 17 ++- lib/ActiveRecord/Table.php | 52 +++---- lib/ActiveRecord/UnableToSetFetchMode.php | 15 +- lib/ActiveRecord/UpdatedAtProperty.php | 2 +- tests/ActiveRecord/ExceptionTest.php | 27 +++- tests/ActiveRecord/ModelTest.php | 7 +- tests/ActiveRecordTest/Sample.php | 2 +- 49 files changed, 436 insertions(+), 359 deletions(-) diff --git a/.travis.yml b/.travis.yml index 22c9fee..fb9b508 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ sudo: false language: php php: - - 5.6 - 7.0 - hhvm @@ -19,5 +18,5 @@ script: - mkdir -p build/logs - phpunit --coverage-clover build/logs/clover.xml -after_script: - - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "5.5" ]; then php vendor/bin/coveralls -v; fi;' +after_success: + - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then php vendor/bin/coveralls -v; fi;' diff --git a/README.md b/README.md index f66c812..0b19b7a 100644 --- a/README.md +++ b/README.md @@ -2138,7 +2138,7 @@ $nodes = ActiveRecord\get_model('nodes'); ## Requirements -The package requires PHP 5.5 or later and the [PDO extension](http://php.net/manual/en/intro.pdo.php). +The package requires PHP 7.0 or later and the [PDO extension](http://php.net/manual/en/intro.pdo.php). diff --git a/composer.json b/composer.json index 948b1f3..fed54b9 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "minimum-stability": "dev", "require": { - "php": ">=5.6.0", + "php": "^7.0", "ext-pdo": "*", "icanboogie/prototype": "^2.3", "icanboogie/inflector": "^1.4", diff --git a/lib/ActiveRecord.php b/lib/ActiveRecord.php index 1c6e5b2..01749a9 100644 --- a/lib/ActiveRecord.php +++ b/lib/ActiveRecord.php @@ -85,7 +85,7 @@ public function __construct($model = null) * Properties whose value are instances of the {@link ActiveRecord} class are removed from the * exported properties. */ - public function __sleep() + public function __sleep(): array { $properties = parent::__sleep(); @@ -108,7 +108,7 @@ public function __sleep() * * @return array */ - public function __debugInfo() + public function __debugInfo(): array { $array = (array) $this; @@ -122,7 +122,7 @@ public function __debugInfo() * * @return Model */ - protected function get_model() + protected function get_model(): Model { return $this->model ?: $this->model = ActiveRecord\get_model($this->model_id); @@ -200,7 +200,7 @@ public function save() * * @return array The altered persistent properties */ - protected function alter_persistent_properties(array $properties, Model $model) + protected function alter_persistent_properties(array $properties, Model $model): array { $schema = $model->extended_schema; @@ -242,7 +242,7 @@ protected function update_primary_key($primary_key) * * @throws \Exception in attempt to delete a record from a model which primary key is empty. */ - public function delete() + public function delete(): bool { $model = $this->model; $primary = $model->primary; diff --git a/lib/ActiveRecord/ActiveRecordCache.php b/lib/ActiveRecord/ActiveRecordCache.php index 50caef4..4798709 100644 --- a/lib/ActiveRecord/ActiveRecordCache.php +++ b/lib/ActiveRecord/ActiveRecordCache.php @@ -32,14 +32,14 @@ public function store(ActiveRecord $record); * * @return ActiveRecord|null */ - public function retrieve($key); + public function retrieve(int $key); /** * Eliminates an {@link ActiveRecord} instance from the cache. * * @param int $key */ - public function eliminate($key); + public function eliminate(int $key); /** * Clears the cache. diff --git a/lib/ActiveRecord/ActiveRecordClassNotValid.php b/lib/ActiveRecord/ActiveRecordClassNotValid.php index 056dea9..125130d 100644 --- a/lib/ActiveRecord/ActiveRecordClassNotValid.php +++ b/lib/ActiveRecord/ActiveRecordClassNotValid.php @@ -25,12 +25,12 @@ class ActiveRecordClassNotValid extends \LogicException implements Exception use AccessorTrait; /** - * @var string + * @var mixed */ private $class; /** - * @return string + * @return mixed */ protected function get_class() { @@ -38,12 +38,12 @@ protected function get_class() } /** - * @param string $class + * @param mixed $class * @param string|null $message * @param int $code * @param \Exception|null $previous */ - public function __construct($class, $message = null, $code = 500, \Exception $previous = null) + public function __construct($class, string $message = null, int $code = 500, \Exception $previous = null) { $this->class = $class; @@ -53,11 +53,11 @@ public function __construct($class, $message = null, $code = 500, \Exception $pr /** * Formats exception message. * - * @param string $class + * @param mixed $class * * @return string */ - protected function format_message($class) + protected function format_message($class): string { return format("ActiveRecord class is not valid: %class", [ diff --git a/lib/ActiveRecord/BelongsToRelation.php b/lib/ActiveRecord/BelongsToRelation.php index a32b31e..41639b6 100644 --- a/lib/ActiveRecord/BelongsToRelation.php +++ b/lib/ActiveRecord/BelongsToRelation.php @@ -48,6 +48,8 @@ public function __construct(Model $parent, $related, array $options = []) /** * @inheritdoc + * + * @return ActiveRecord|null */ public function __invoke(ActiveRecord $record) { @@ -66,7 +68,7 @@ public function __invoke(ActiveRecord $record) * * @inheritdoc */ - protected function alter_prototype(Prototype $prototype, $property) + protected function alter_prototype(Prototype $prototype, string $property) { parent::alter_prototype($prototype, $property); @@ -80,7 +82,7 @@ protected function alter_prototype(Prototype $prototype, $property) /** * @inheritdoc */ - protected function resolve_property_name($related) + protected function resolve_property_name($related): string { return singularize(parent::resolve_property_name($related)); } diff --git a/lib/ActiveRecord/Connection.php b/lib/ActiveRecord/Connection.php index cbd4214..af2e245 100644 --- a/lib/ActiveRecord/Connection.php +++ b/lib/ActiveRecord/Connection.php @@ -45,7 +45,7 @@ class Connection extends \PDO implements Driver /** * @return string */ - protected function get_id() + protected function get_id(): string { return $this->id; } @@ -64,7 +64,7 @@ protected function get_id() /** * @return string */ - protected function get_table_name_prefix() + protected function get_table_name_prefix(): string { return $this->table_name_prefix; } @@ -79,7 +79,7 @@ protected function get_table_name_prefix() /** * @return string */ - protected function get_charset() + protected function get_charset(): string { return $this->charset; } @@ -94,7 +94,7 @@ protected function get_charset() /** * @return string */ - protected function get_collate() + protected function get_collate(): string { return $this->collate; } @@ -109,7 +109,7 @@ protected function get_collate() /** * @return string */ - protected function get_timezone() + protected function get_timezone(): string { return $this->timezone; } @@ -124,7 +124,7 @@ protected function get_timezone() /** * @return string */ - protected function get_driver_name() + protected function get_driver_name(): string { return $this->driver_name; } @@ -137,7 +137,7 @@ protected function get_driver_name() /** * @return Driver */ - protected function lazy_get_driver() + protected function lazy_get_driver(): Driver { return $this->resolve_driver($this->driver_name); } @@ -170,7 +170,7 @@ protected function lazy_get_driver() * @param string $password * @param array $options */ - public function __construct($dsn, $username = null, $password = null, $options = []) + public function __construct(string $dsn, string $username = null, string $password = null, array $options = []) { unset($this->driver); @@ -186,11 +186,13 @@ public function __construct($dsn, $username = null, $password = null, $options = /** * Alias to {@link query}. * + * @param array ...$args + * * @return Statement */ - public function __invoke() + public function __invoke(...$args): Statement { - return call_user_func_array([ $this, 'query' ], func_get_args()); + return $this->query(...$args); } /** @@ -200,7 +202,7 @@ public function __invoke() * * @return string */ - protected function resolve_driver_name($dsn) + protected function resolve_driver_name(string $dsn): string { return explode(':', $dsn, 2)[0]; } @@ -214,7 +216,7 @@ protected function resolve_driver_name($dsn) * * @throws DriverNotDefined */ - protected function resolve_driver_class($driver_name) + protected function resolve_driver_class(string $driver_name): string { if (empty(self::$drivers_mapping[$driver_name])) { @@ -231,7 +233,7 @@ protected function resolve_driver_class($driver_name) * * @return Driver */ - protected function resolve_driver($driver_name) + protected function resolve_driver(string $driver_name): Driver { $driver_class = $this->resolve_driver_class($driver_name); @@ -298,10 +300,7 @@ protected function after_connection() * Overrides the method to resolve the statement before it is prepared, then set its fetch * mode and connection. * - * @param string $statement Query statement. - * @param array $options - * - * @return Statement The prepared statement. + * @inheritdoc * * @throws StatementNotValid if the statement cannot be prepared. */ @@ -325,7 +324,7 @@ public function prepare($statement, $options = []) { $mode = (array) $options['mode']; - call_user_func_array([ $statement, 'setFetchMode' ], $mode); + $statement->mode(...$mode); } return $statement; @@ -339,7 +338,7 @@ public function prepare($statement, $options = []) * * @return Statement */ - public function query($statement, array $args = [], array $options = []) + public function query(string $statement, array $args = [], array $options = []): Statement { $statement = $this->prepare($statement, $options); $statement->execute($args); @@ -392,7 +391,7 @@ public function exec($statement) * * @return string The resolved statement. */ - public function resolve_statement($statement) + public function resolve_statement(string $statement): string { return strtr($statement, [ @@ -438,7 +437,7 @@ public function quote_identifier($identifier) * * @codeCoverageIgnore */ - public function cast_value($value, $type = null) + public function cast_value($value, string $type = null) { return $this->driver->cast_value($value, $type); } @@ -448,7 +447,7 @@ public function cast_value($value, $type = null) * * @codeCoverageIgnore */ - public function render_column(SchemaColumn $column) + public function render_column(SchemaColumn $column): string { return $this->driver->render_column($column); } @@ -458,7 +457,7 @@ public function render_column(SchemaColumn $column) * * @codeCoverageIgnore */ - public function create_table($unprefixed_name, Schema $schema) + public function create_table(string $unprefixed_name, Schema $schema) { $this->driver->create_table($unprefixed_name, $schema); } @@ -468,7 +467,7 @@ public function create_table($unprefixed_name, Schema $schema) * * @codeCoverageIgnore */ - public function create_indexes($unprefixed_table_name, Schema $schema) + public function create_indexes(string $unprefixed_table_name, Schema $schema) { $this->driver->create_indexes($unprefixed_table_name, $schema); } @@ -478,7 +477,7 @@ public function create_indexes($unprefixed_table_name, Schema $schema) * * @codeCoverageIgnore */ - public function create_unique_indexes($unprefixed_table_name, Schema $schema) + public function create_unique_indexes(string $unprefixed_table_name, Schema $schema) { $this->driver->create_unique_indexes($unprefixed_table_name, $schema); } @@ -488,7 +487,7 @@ public function create_unique_indexes($unprefixed_table_name, Schema $schema) * * @codeCoverageIgnore */ - public function table_exists($unprefixed_name) + public function table_exists(string $unprefixed_name): bool { return $this->driver->table_exists($unprefixed_name); } diff --git a/lib/ActiveRecord/ConnectionAlreadyEstablished.php b/lib/ActiveRecord/ConnectionAlreadyEstablished.php index 577caa3..2e8ec24 100644 --- a/lib/ActiveRecord/ConnectionAlreadyEstablished.php +++ b/lib/ActiveRecord/ConnectionAlreadyEstablished.php @@ -32,7 +32,7 @@ class ConnectionAlreadyEstablished extends \LogicException implements Exception /** * @return string */ - protected function get_id() + protected function get_id(): string { return $this->id; } @@ -42,7 +42,7 @@ protected function get_id() * @param int $code * @param \Exception|null $previous */ - public function __construct($id, $code = 500, \Exception $previous = null) + public function __construct(string $id, int $code = 500, \Exception $previous = null) { $this->id = $id; @@ -56,7 +56,7 @@ public function __construct($id, $code = 500, \Exception $previous = null) * * @return string */ - protected function format_message($id) + protected function format_message(string $id): string { return format("Connection already established: %id.", [ diff --git a/lib/ActiveRecord/ConnectionCollection.php b/lib/ActiveRecord/ConnectionCollection.php index 1694cb7..cb6df0d 100644 --- a/lib/ActiveRecord/ConnectionCollection.php +++ b/lib/ActiveRecord/ConnectionCollection.php @@ -28,9 +28,12 @@ class ConnectionCollection implements \ArrayAccess, \IteratorAggregate * * @var array */ - private $definitions; + private $definitions = []; - protected function get_definitions() + /** + * @return array + */ + protected function get_definitions(): array { return $this->definitions; } @@ -42,7 +45,10 @@ protected function get_definitions() */ private $established = []; - protected function get_established() + /** + * @return Connection[] + */ + protected function get_established(): array { return $this->established; } diff --git a/lib/ActiveRecord/ConnectionNotDefined.php b/lib/ActiveRecord/ConnectionNotDefined.php index 408008e..2b1c9ee 100644 --- a/lib/ActiveRecord/ConnectionNotDefined.php +++ b/lib/ActiveRecord/ConnectionNotDefined.php @@ -29,7 +29,10 @@ class ConnectionNotDefined extends \LogicException implements Exception */ private $id; - protected function get_id() + /** + * @return string + */ + protected function get_id(): string { return $this->id; } @@ -39,7 +42,7 @@ protected function get_id() * @param int $code * @param \Exception|null $previous */ - public function __construct($id, $code = 500, \Exception $previous = null) + public function __construct(string $id, int $code = 500, \Exception $previous = null) { $this->id = $id; @@ -53,7 +56,7 @@ public function __construct($id, $code = 500, \Exception $previous = null) * * @return string */ - protected function format_message($id) + protected function format_message(string $id): string { return format("Connection not defined: %id.", [ diff --git a/lib/ActiveRecord/ConnectionNotEstablished.php b/lib/ActiveRecord/ConnectionNotEstablished.php index 182ce7c..f92fd10 100644 --- a/lib/ActiveRecord/ConnectionNotEstablished.php +++ b/lib/ActiveRecord/ConnectionNotEstablished.php @@ -27,7 +27,10 @@ class ConnectionNotEstablished extends \RuntimeException implements Exception */ private $id; - protected function get_id() + /** + * @return string + */ + protected function get_id(): string { return $this->id; } @@ -38,7 +41,7 @@ protected function get_id() * @param int $code * @param \Exception|null $previous */ - public function __construct($id, $message, $code = 500, \Exception $previous = null) + public function __construct(string $id, string $message, int $code = 500, \Exception $previous = null) { $this->id = $id; diff --git a/lib/ActiveRecord/ConnectionOptions.php b/lib/ActiveRecord/ConnectionOptions.php index 60ed8b2..2928b55 100644 --- a/lib/ActiveRecord/ConnectionOptions.php +++ b/lib/ActiveRecord/ConnectionOptions.php @@ -65,7 +65,7 @@ final class ConnectionOptions * * @return array */ - static public function normalize(array $options) + static public function normalize(array $options): array { return $options + [ diff --git a/lib/ActiveRecord/CreatedAtProperty.php b/lib/ActiveRecord/CreatedAtProperty.php index c5a786b..6d578f1 100644 --- a/lib/ActiveRecord/CreatedAtProperty.php +++ b/lib/ActiveRecord/CreatedAtProperty.php @@ -34,7 +34,7 @@ trait CreatedAtProperty * * @return DateTime */ - protected function get_created_at() + protected function get_created_at(): DateTime { return DateTimePropertySupport::get($this->created_at); } diff --git a/lib/ActiveRecord/DateProperty.php b/lib/ActiveRecord/DateProperty.php index ec0d6ec..e7f2256 100644 --- a/lib/ActiveRecord/DateProperty.php +++ b/lib/ActiveRecord/DateProperty.php @@ -34,7 +34,7 @@ trait DateProperty * * @return DateTime */ - protected function get_date() + protected function get_date(): DateTime { return DateTimePropertySupport::get($this->date); } diff --git a/lib/ActiveRecord/DateTimeProperty.php b/lib/ActiveRecord/DateTimeProperty.php index c4ef7c7..a15674b 100644 --- a/lib/ActiveRecord/DateTimeProperty.php +++ b/lib/ActiveRecord/DateTimeProperty.php @@ -32,7 +32,7 @@ trait DateTimeProperty * * @return DateTime */ - protected function get_datetime() + protected function get_datetime(): DateTime { return DateTimePropertySupport::get($this->datetime); } diff --git a/lib/ActiveRecord/DateTimePropertySupport.php b/lib/ActiveRecord/DateTimePropertySupport.php index 0742b8a..728737a 100644 --- a/lib/ActiveRecord/DateTimePropertySupport.php +++ b/lib/ActiveRecord/DateTimePropertySupport.php @@ -36,7 +36,7 @@ static public function set(&$property, $datetime) * * @return DateTime The function always return a {@link DateTime} instance. */ - static public function get(&$property) + static public function get(&$property): DateTime { if ($property instanceof DateTime) { diff --git a/lib/ActiveRecord/Driver.php b/lib/ActiveRecord/Driver.php index f7bd994..b7ed48f 100644 --- a/lib/ActiveRecord/Driver.php +++ b/lib/ActiveRecord/Driver.php @@ -42,7 +42,7 @@ public function quote_identifier($identifier); * * @return mixed */ - public function cast_value($value, $type = null); + public function cast_value($value, string $type = null); /** * Renders a column definition. @@ -51,7 +51,7 @@ public function cast_value($value, $type = null); * * @return string */ - public function render_column(SchemaColumn $column); + public function render_column(SchemaColumn $column): string; /** * Creates a table given a schema. @@ -63,7 +63,7 @@ public function render_column(SchemaColumn $column); * * @throws \Exception */ - public function create_table($unprefixed_table_name, Schema $schema); + public function create_table(string $unprefixed_table_name, Schema $schema); /** * Creates indexes given a schema. @@ -75,7 +75,7 @@ public function create_table($unprefixed_table_name, Schema $schema); * * @throws \Exception */ - public function create_indexes($unprefixed_table_name, Schema $schema); + public function create_indexes(string $unprefixed_table_name, Schema $schema); /** * Creates unique indexes given a schema. @@ -87,7 +87,7 @@ public function create_indexes($unprefixed_table_name, Schema $schema); * * @throws \Exception */ - public function create_unique_indexes($unprefixed_table_name, Schema $schema); + public function create_unique_indexes(string $unprefixed_table_name, Schema $schema); /** * Checks if a specified table exists in the database. @@ -96,7 +96,7 @@ public function create_unique_indexes($unprefixed_table_name, Schema $schema); * * @return bool `true` if the table exists, `false` otherwise. */ - public function table_exists($unprefixed_name); + public function table_exists(string $unprefixed_name): bool; /** * Optimizes the tables of the database. diff --git a/lib/ActiveRecord/Driver/BasicDriver.php b/lib/ActiveRecord/Driver/BasicDriver.php index 594c2fd..412ef36 100644 --- a/lib/ActiveRecord/Driver/BasicDriver.php +++ b/lib/ActiveRecord/Driver/BasicDriver.php @@ -89,7 +89,7 @@ public function quote_identifier($identifier) /** * @inheritdoc */ - public function cast_value($value, $type = null) + public function cast_value($value, string $type = null) { if ($value instanceof \DateTime) { @@ -118,7 +118,7 @@ public function cast_value($value, $type = null) * * @return string */ - protected function resolve_table_name($unprefixed_table_name) + protected function resolve_table_name(string $unprefixed_table_name): string { return $this->connection->table_name_prefix . $unprefixed_table_name; } @@ -130,7 +130,7 @@ protected function resolve_table_name($unprefixed_table_name) * * @return string */ - protected function resolve_quoted_table_name($unprefixed_table_name) + protected function resolve_quoted_table_name(string $unprefixed_table_name): string { return $this->quote_identifier($this->connection->table_name_prefix . $unprefixed_table_name); } @@ -143,7 +143,7 @@ protected function resolve_quoted_table_name($unprefixed_table_name) * * @return string */ - protected function resolve_index_name($unprefixed_table_name, $index_id) + protected function resolve_index_name(string $unprefixed_table_name, string $index_id): string { return $index_id; } diff --git a/lib/ActiveRecord/Driver/MySQLDriver.php b/lib/ActiveRecord/Driver/MySQLDriver.php index d882ca6..aeb4d80 100644 --- a/lib/ActiveRecord/Driver/MySQLDriver.php +++ b/lib/ActiveRecord/Driver/MySQLDriver.php @@ -23,7 +23,7 @@ class MySQLDriver extends BasicDriver /** * @inheritdoc */ - public function render_column(SchemaColumn $column) + public function render_column(SchemaColumn $column): string { return (string) $column; } @@ -31,7 +31,7 @@ public function render_column(SchemaColumn $column) /** * @inheritdoc */ - public function create_table($unprefixed_table_name, Schema $schema) + public function create_table(string $unprefixed_table_name, Schema $schema) { $statement = $this->render_create_table($unprefixed_table_name, $schema); $this->connection->exec($statement); @@ -43,7 +43,7 @@ public function create_table($unprefixed_table_name, Schema $schema) /** * @inheritdoc */ - public function create_indexes($unprefixed_table_name, Schema $schema) + public function create_indexes(string $unprefixed_table_name, Schema $schema) { $this->create_indexes_of('', $unprefixed_table_name, $schema->indexes); } @@ -51,7 +51,7 @@ public function create_indexes($unprefixed_table_name, Schema $schema) /** * @inheritdoc */ - public function create_unique_indexes($unprefixed_table_name, Schema $schema) + public function create_unique_indexes(string $unprefixed_table_name, Schema $schema) { $this->create_indexes_of('UNIQUE', $unprefixed_table_name, $schema->unique_indexes); } @@ -59,7 +59,7 @@ public function create_unique_indexes($unprefixed_table_name, Schema $schema) /** * @inheritdoc */ - public function table_exists($unprefixed_name) + public function table_exists(string $unprefixed_name): bool { $tables = $this->connection->query('SHOW TABLES')->all(\PDO::FETCH_COLUMN); @@ -84,7 +84,7 @@ public function optimize() * * @return string */ - protected function render_create_table($unprefixed_table_name, Schema $schema) + protected function render_create_table(string $unprefixed_table_name, Schema $schema) { $connection = $this->connection; $quoted_table_name = $this->resolve_quoted_table_name($unprefixed_table_name); @@ -161,7 +161,7 @@ protected function render_create_table_primary_key(Schema $schema) * @param string $unprefixed_table_name * @param array $indexes */ - protected function create_indexes_of($type, $unprefixed_table_name, array $indexes) + protected function create_indexes_of(string $type, string $unprefixed_table_name, array $indexes) { if (!$indexes) { diff --git a/lib/ActiveRecord/Driver/SQLiteDriver.php b/lib/ActiveRecord/Driver/SQLiteDriver.php index e320c41..c21149f 100644 --- a/lib/ActiveRecord/Driver/SQLiteDriver.php +++ b/lib/ActiveRecord/Driver/SQLiteDriver.php @@ -22,7 +22,7 @@ class SQLiteDriver extends MySQLDriver /** * @inheritdoc */ - public function render_column(SchemaColumn $column) + public function render_column(SchemaColumn $column): string { if ($column->primary && $column->type == SchemaColumn::TYPE_INTEGER) { @@ -44,7 +44,7 @@ public function render_column(SchemaColumn $column) /** * @inheritdoc */ - protected function render_create_table($unprefixed_table_name, Schema $schema) + protected function render_create_table(string $unprefixed_table_name, Schema $schema): string { $quoted_table_name = $this->resolve_quoted_table_name($unprefixed_table_name); $lines = $this->render_create_table_lines($schema); @@ -56,7 +56,7 @@ protected function render_create_table($unprefixed_table_name, Schema $schema) /** * @inheritdoc */ - public function table_exists($unprefixed_name) + public function table_exists(string $unprefixed_name): bool { $name = $this->resolve_table_name($unprefixed_name); @@ -78,7 +78,7 @@ public function optimize() /** * @inheritdoc */ - protected function resolve_index_name($unprefixed_table_name, $index_id) + protected function resolve_index_name(string $unprefixed_table_name, string $index_id): string { return $index_id . '_' . substr(sha1($unprefixed_table_name), 0 , 8); } diff --git a/lib/ActiveRecord/DriverNotDefined.php b/lib/ActiveRecord/DriverNotDefined.php index 8a885d8..8d61e1c 100644 --- a/lib/ActiveRecord/DriverNotDefined.php +++ b/lib/ActiveRecord/DriverNotDefined.php @@ -30,7 +30,7 @@ class DriverNotDefined extends \LogicException implements Exception /** * @return string */ - protected function get_driver_name() + protected function get_driver_name(): string { return $this->driver_name; } @@ -41,7 +41,7 @@ protected function get_driver_name() * @param int $code * @param \Exception|null $previous */ - public function __construct($driver_name, $message = null, $code = 500, \Exception $previous = null) + public function __construct(string $driver_name, string $message = null, int $code = 500, \Exception $previous = null) { $this->driver_name = $driver_name; @@ -55,7 +55,7 @@ public function __construct($driver_name, $message = null, $code = 500, \Excepti * * @return string */ - protected function format_message($driver_name) + protected function format_message(string $driver_name): string { return "Driver not defined for: $driver_name."; } diff --git a/lib/ActiveRecord/FinishAtProperty.php b/lib/ActiveRecord/FinishAtProperty.php index 5d28cc1..d2da5f2 100644 --- a/lib/ActiveRecord/FinishAtProperty.php +++ b/lib/ActiveRecord/FinishAtProperty.php @@ -36,7 +36,7 @@ trait FinishAtProperty * * @return DateTime */ - protected function get_finish_at() + protected function get_finish_at(): DateTime { return DateTimePropertySupport::get($this->finish_at); } diff --git a/lib/ActiveRecord/FinishedAtProperty.php b/lib/ActiveRecord/FinishedAtProperty.php index 0845a10..6aa1f57 100644 --- a/lib/ActiveRecord/FinishedAtProperty.php +++ b/lib/ActiveRecord/FinishedAtProperty.php @@ -36,7 +36,7 @@ trait FinishedAtProperty * * @return DateTime */ - protected function get_finished_at() + protected function get_finished_at(): DateTime { return DateTimePropertySupport::get($this->finished_at); } diff --git a/lib/ActiveRecord/HasManyRelation.php b/lib/ActiveRecord/HasManyRelation.php index 9192fff..eba1d27 100644 --- a/lib/ActiveRecord/HasManyRelation.php +++ b/lib/ActiveRecord/HasManyRelation.php @@ -27,14 +27,17 @@ class HasManyRelation extends Relation * * @return Query */ - public function __invoke(ActiveRecord $record) + public function __invoke(ActiveRecord $record): Query { return $this ->resolve_related() ->where([ $this->foreign_key => $record->{ $this->local_key }]); } - protected function resolve_property_name($related) + /** + * @inheritdoc + */ + protected function resolve_property_name($related): string { return pluralize(parent::resolve_property_name($related)); } diff --git a/lib/ActiveRecord/Model.php b/lib/ActiveRecord/Model.php index 75d0d0d..5573e9c 100644 --- a/lib/ActiveRecord/Model.php +++ b/lib/ActiveRecord/Model.php @@ -18,22 +18,21 @@ /** * Base class for activerecord models. * - * @method Query select() select($expression) The method is forwarded to {@link Query::select}. - * @method Query join() join($expression) The method is forwarded to {@link Query::join}. - * @method Query where() where($conditions, $conditions_args = null, $_ = null) The method is forwarded to {@link Query::where}. - * @method Query group() group($group) The method is forwarded to {@link Query::group}. - * @method Query order() order($order) The method is forwarded to {@link Query::order}. - * @method Query limit() limit($limit, $offset = null) The method is forwarded to {@link Query::limit}. - * @method Query offset() offset($offset) The method is forwarded to {@link Query::offset}. - * @method bool exists() exists($key = null) The method is forwarded to {@link Query::exists}. - * @method mixed count() count($column = null) The method is forwarded to {@link Query::count}. - * @method string average() average($column) The method is forwarded to {@link Query::average}. - * @method string maximum() maximum($column) The method is forwarded to {@link Query::maximum}. - * @method string minimum() minimum($column) The method is forwarded to {@link Query::minimum}. - * @method int sum() sum($column) The method is forwarded to {@link Query::sum}. - * @method array all() all() The method is forwarded to {@link Query::all}. - * @method ActiveRecord one() one() The method is forwarded to {@link Query::one}. - * @method ActiveRecord new() new(array $properties = []) Instantiate a new record. + * @method Query select(string $expression) The method is forwarded to {@link Query::select}. + * @method Query join($expression, array $options = []) The method is forwarded to {@link Query::join}. + * @method Query where(...$conditions_and_args) The method is forwarded to {@link Query::where}. + * @method Query group(string $group) The method is forwarded to {@link Query::group}. + * @method Query order($order) The method is forwarded to {@link Query::order}. + * @method Query limit($limit, int $offset = null) The method is forwarded to {@link Query::limit}. + * @method Query offset(int $offset) The method is forwarded to {@link Query::offset}. + * @method bool exists($key = null) The method is forwarded to {@link Query::exists}. + * @method int|array count($column = null) The method is forwarded to {@link Query::count}. + * @method number average($column) The method is forwarded to {@link Query::average}. + * @method mixed maximum($column) The method is forwarded to {@link Query::maximum}. + * @method mixed minimum($column) The method is forwarded to {@link Query::minimum}. + * @method number sum($column) The method is forwarded to {@link Query::sum}. + * @method array all() The method is forwarded to {@link Query::all}. + * @method ActiveRecord|null one() The method is forwarded to {@link Query::one}. * * @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. @@ -44,10 +43,10 @@ * @property-read string $activerecord_class Class of the active records of the model. * @property-read int $count The number of records of the model. * @property-read bool $exists Whether the SQL table associated with the model exists. - * @property-read string $id The identifier of the model. - * @property-read ActiveRecord $one Retrieve the first record from the mode. + * @property-read string|null $id The identifier of the model. + * @property-read ActiveRecord|null $one Retrieve the first record from the mode. * @property ActiveRecordCache $activerecord_cache The cache use to store activerecords. - * @property-read Model $parent_model The parent model. + * @property-read Model|null $parent_model The parent model. * @property-read Relation[] $relations The relations of this model to other models. */ class Model extends Table implements \ArrayAccess @@ -63,7 +62,10 @@ class Model extends Table implements \ArrayAccess */ private $models; - protected function get_models() + /** + * @return ModelCollection + */ + protected function get_models(): ModelCollection { return $this->models; } @@ -73,29 +75,49 @@ protected function get_models() * * @var string */ - protected $activerecord_class; + protected $activerecord_class = ActiveRecord::class; + + /** + * Returns the class of the active records of the model. + * + * @return string + */ + protected function get_activerecord_class(): string + { + return $this->activerecord_class; + } /** * Attributes of the model. * - * @var array[string]mixed + * @var array */ protected $attributes; + /** + * Returns the identifier of the model. + * + * @return string|null + */ + protected function get_id() + { + return $this->attributes[self::ID]; + } + /** * The parent model of the model. * * The parent model and the {@link parent} may be different if the model does not have a * schema but inherits it from its parent. * - * @var Model + * @var Model|null */ protected $parent_model; /** * Return the parent mode. * - * @return Model + * @return Model|null */ protected function get_parent_model() { @@ -114,7 +136,7 @@ protected function get_parent_model() * * @return RelationCollection */ - protected function get_relations() + protected function get_relations(): RelationCollection { return $this->relations; } @@ -126,7 +148,7 @@ protected function get_relations() * * @return ActiveRecordCache */ - protected function lazy_get_activerecord_cache() + protected function lazy_get_activerecord_cache(): ActiveRecordCache { return parent::lazy_get_activerecord_cache(); } @@ -164,7 +186,10 @@ public function __construct(ModelCollection $models, array $attributes) } // @codeCoverageIgnoreStart - public function __debugInfo() + /** + * @inheritdoc + */ + public function __debugInfo(): array { return [ @@ -187,7 +212,7 @@ public function __debugInfo() * * @return array */ - private function resolve_attributes(array $attributes) + private function resolve_attributes(array $attributes): array { $attributes += [ @@ -227,7 +252,7 @@ private function resolve_attributes(array $attributes) * * @return string */ - private function resolve_activerecord_class() + private function resolve_activerecord_class(): string { $activerecord_class = $this->attributes[self::ACTIVERECORD_CLASS]; @@ -236,7 +261,7 @@ private function resolve_activerecord_class() $activerecord_class = $this->parent->activerecord_class; } - return $activerecord_class; + return $activerecord_class ?: ActiveRecord::class; } /** @@ -272,11 +297,6 @@ private function resolve_relations() */ public function __call($method, $arguments) { - if ($method == 'new') - { - return $this->new_record(...$arguments); - } - if (is_callable([ Query::class, $method ]) || strpos($method, 'filter_by_') === 0 || method_exists($this, 'scope_' . $method)) @@ -311,39 +331,18 @@ public function __get($property) return parent::__get($property); } - /** - * Returns the identifier of the model. - * - * @return string - */ - protected function get_id() - { - return $this->attributes[self::ID]; - } - - /** - * Returns the class of the active records of the model. - * - * @return string - */ - protected function get_activerecord_class() - { - return $this->activerecord_class; - } - /** * Finds a record or a collection of records. * - * @param mixed $key A key, multiple keys, or an array of keys. + * @param array $args A key, multiple keys, or an array of keys. * * @throws RecordNotFound when the record, or one or more records of the records * set, could not be found. * * @return ActiveRecord|ActiveRecord[] A record or a set of records. */ - public function find($key) + public function find(...$args) { - $args = func_get_args(); $n = count($args); if (!$n) @@ -351,7 +350,7 @@ public function find($key) throw new \BadMethodCallException("Expected at least one argument."); } - if (count($args) == 1) + if ($n === 1) { $key = $args[0]; @@ -373,7 +372,7 @@ public function find($key) * * @return ActiveRecord */ - private function find_one($key) + private function find_one($key): ActiveRecord { $record = $this->activerecord_cache->retrieve($key); @@ -404,7 +403,7 @@ private function find_one($key) * * @return ActiveRecord[] */ - private function find_many(array $keys) + private function find_many(array $keys): array { $records = array_combine($keys, array_fill(0, count($keys), null)); $missing = $records; @@ -480,7 +479,7 @@ public function save(array $properties, $key = null, array $options = []) * * @inheritdoc */ - public function delete($key) + public function delete($key): bool { $this->activerecord_cache->eliminate($key); @@ -492,7 +491,7 @@ public function delete($key) * * @return bool */ - protected function get_exists() + protected function get_exists(): bool { return $this->exists(); } @@ -502,7 +501,7 @@ protected function get_exists() * * @return int */ - protected function get_count() + protected function get_count(): int { return $this->count(); } @@ -512,7 +511,7 @@ protected function get_count() * * @return ActiveRecord[] */ - protected function get_all() + protected function get_all(): array { return $this->all(); } @@ -520,7 +519,7 @@ protected function get_all() /** * Returns the first record of the model. * - * @return ActiveRecord + * @return ActiveRecord|null */ protected function get_one() { @@ -535,9 +534,9 @@ protected function get_one() * * @param string $name Scope name. * - * @return boolean + * @return bool */ - public function has_scope($name) + public function has_scope(string $name):bool { return method_exists($this, 'scope_' . $name); } @@ -553,7 +552,7 @@ public function has_scope($name) * * @return Query */ - public function scope($scope_name, array $scope_args = []) + public function scope(string $scope_name, array $scope_args = []): Query { try { @@ -604,7 +603,7 @@ public function offsetUnset($key) * * @return ActiveRecord */ - public function offsetGet($key) + public function offsetGet($key): ActiveRecord { return $this->find($key); } @@ -618,7 +617,7 @@ public function offsetGet($key) * * @return ActiveRecord */ - protected function new_record(array $properties = []) + public function new(array $properties = []): ActiveRecord { $class = $this->activerecord_class; diff --git a/lib/ActiveRecord/ModelAlreadyInstantiated.php b/lib/ActiveRecord/ModelAlreadyInstantiated.php index 87314c5..0b83a66 100644 --- a/lib/ActiveRecord/ModelAlreadyInstantiated.php +++ b/lib/ActiveRecord/ModelAlreadyInstantiated.php @@ -16,7 +16,7 @@ /** * Exception thrown in attempt to set/unset the definition of an already instantiated model. * - * @property-read string $id The identifier of the model. + * @property-read string $id Model identifier. */ class ModelAlreadyInstantiated extends \LogicException implements Exception { @@ -27,17 +27,20 @@ class ModelAlreadyInstantiated extends \LogicException implements Exception */ private $id; - protected function get_id() + /** + * @return string + */ + protected function get_id(): string { return $this->id; } /** - * @param string $id + * @param string $id Model identifier. * @param int $code * @param \Exception|null $previous */ - public function __construct($id, $code = 500, \Exception $previous = null) + public function __construct(string $id, int $code = 500, \Exception $previous = null) { $this->id = $id; @@ -51,7 +54,7 @@ public function __construct($id, $code = 500, \Exception $previous = null) * * @return string */ - protected function format_message($id) + protected function format_message(string $id): string { return "Model already instantiated: $id."; } diff --git a/lib/ActiveRecord/ModelCollection.php b/lib/ActiveRecord/ModelCollection.php index 75f53e2..1ce72a7 100644 --- a/lib/ActiveRecord/ModelCollection.php +++ b/lib/ActiveRecord/ModelCollection.php @@ -31,7 +31,10 @@ class ModelCollection implements \ArrayAccess */ protected $instances = []; - protected function get_instances() + /** + * @return Model[] + */ + protected function get_instances(): array { return $this->instances; } @@ -43,7 +46,10 @@ protected function get_instances() */ protected $definitions = []; - protected function get_definitions() + /** + * @return array + */ + protected function get_definitions(): array { return $this->definitions; } @@ -53,7 +59,10 @@ protected function get_definitions() */ protected $connections; - protected function get_connections() + /** + * @return ConnectionCollection + */ + protected function get_connections(): ConnectionCollection { return $this->connections; } @@ -121,7 +130,7 @@ public function offsetSet($id, $definition) * * @throws ModelNotDefined when the model is not defined. */ - public function offsetGet($id) + public function offsetGet($id): Model { if (isset($this->instances[$id])) { @@ -166,7 +175,7 @@ public function offsetUnset($id) * * @return array */ - protected function resolve_model_attributes(array $attributes) + protected function resolve_model_attributes(array $attributes): array { $attributes += [ @@ -200,7 +209,7 @@ protected function resolve_model_attributes(array $attributes) * * @return Model */ - protected function instantiate_model(array $attributes) + protected function instantiate_model(array $attributes): Model { $class = $attributes[Model::CLASSNAME]; @@ -210,7 +219,7 @@ protected function instantiate_model(array $attributes) /** * Install all the models. * - * @return ModelCollection + * @return $this */ public function install() { @@ -232,7 +241,7 @@ public function install() /** * Uninstall all the models. * - * @return ModelCollection + * @return $this */ public function uninstall() { @@ -258,7 +267,7 @@ public function uninstall() * @return array An array of key/value pair where _key_ is a model identifier and * _value_ `true` if the model is installed, `false` otherwise. */ - public function is_installed() + public function is_installed(): array { $rc = []; diff --git a/lib/ActiveRecord/ModelNotDefined.php b/lib/ActiveRecord/ModelNotDefined.php index cf01eb2..e1bac0a 100644 --- a/lib/ActiveRecord/ModelNotDefined.php +++ b/lib/ActiveRecord/ModelNotDefined.php @@ -27,7 +27,10 @@ class ModelNotDefined extends \LogicException implements Exception */ private $id; - protected function get_id() + /** + * @return string + */ + protected function get_id(): string { return $this->id; } @@ -37,7 +40,7 @@ protected function get_id() * @param int $code * @param \Exception|null $previous */ - public function __construct($id, $code = 500, \Exception $previous = null) + public function __construct(string $id, int $code = 500, \Exception $previous = null) { $this->id = $id; @@ -51,7 +54,7 @@ public function __construct($id, $code = 500, \Exception $previous = null) * * @return string */ - protected function format_message($id) + protected function format_message(string $id): string { return "Model not defined: $id."; } diff --git a/lib/ActiveRecord/Query.php b/lib/ActiveRecord/Query.php index 74291bd..0c86690 100644 --- a/lib/ActiveRecord/Query.php +++ b/lib/ActiveRecord/Query.php @@ -67,7 +67,7 @@ class Query implements \IteratorAggregate /** * @return array */ - protected function get_joints() + protected function get_joints(): array { return $this->joints; } @@ -82,7 +82,7 @@ protected function get_joints() /** * @return array */ - protected function get_joints_args() + protected function get_joints_args(): array { return $this->joints_args; } @@ -100,7 +100,7 @@ protected function get_joints_args() * * @return array */ - protected function get_conditions() + protected function get_conditions(): array { return $this->conditions; } @@ -117,7 +117,7 @@ protected function get_conditions() * * @return array */ - protected function get_conditions_args() + protected function get_conditions_args(): array { return $this->conditions_args; } @@ -155,7 +155,7 @@ protected function get_conditions_args() * * @return array */ - protected function get_having_args() + protected function get_having_args(): array { return $this->having_args; } @@ -191,7 +191,7 @@ protected function get_having_args() /** * @return Model */ - protected function get_model() + protected function get_model(): Model { return $this->model; } @@ -202,7 +202,7 @@ protected function get_model() * * @return array */ - protected function get_args() + protected function get_args(): array { return array_merge($this->joints_args, $this->conditions_args, $this->having_args); } @@ -222,7 +222,7 @@ public function __construct(Model $model) * * @inheritdoc */ - public function __get($property) + public function __get(string $property) { $scopes = $this->get_model_scope(); @@ -239,7 +239,7 @@ public function __get($property) * * @inheritdoc */ - public function __call($method, $arguments) + public function __call(string $method, array $arguments) { if ($method === 'and') { @@ -279,7 +279,7 @@ public function __call($method, $arguments) * * @return string */ - public function __toString() + public function __toString(): string { return $this->resolve_statement ( @@ -294,7 +294,7 @@ public function __toString() * * @return string */ - protected function render_select() + protected function render_select(): string { return 'SELECT ' . ($this->select ? $this->select : '*'); } @@ -306,7 +306,7 @@ protected function render_select() * * @return string */ - protected function render_from() + protected function render_from(): string { return 'FROM {self_and_related}'; } @@ -316,7 +316,7 @@ protected function render_from() * * @return string */ - protected function render_joints() + protected function render_joints(): string { return implode(' ', $this->joints); } @@ -326,7 +326,7 @@ protected function render_joints() * * @return string */ - protected function render_main() + protected function render_main(): string { $query = ''; @@ -381,7 +381,7 @@ protected function render_main() * * @return string */ - protected function render_order($order) + protected function render_order($order): string { if (count($order) == 1) { @@ -404,12 +404,12 @@ protected function render_order($order) /** * Render the `LIMIT` and `OFFSET` clauses. * - * @param int $offset - * @param int $limit + * @param int|null $offset + * @param int|null $limit * * @return string */ - protected function render_offset_and_limit($offset, $limit) + protected function render_offset_and_limit($offset, $limit): string { if ($offset && $limit) { @@ -434,14 +434,14 @@ protected function render_offset_and_limit($offset, $limit) /** * Resolve the placeholders of a statement. * - * Note: Currently, the method simply forwards the statement to the model's + * **Note:** Currently, the method simply forwards the statement to the model's * resolve_statement() method. * * @param string $statement * * @return string */ - protected function resolve_statement($statement) + protected function resolve_statement(string $statement): string { return $this->model->resolve_statement($statement); } @@ -460,7 +460,7 @@ protected function resolve_statement($statement) * * @return array */ - protected function get_model_scope() + protected function get_model_scope(): array { $class = get_class($this->model); @@ -494,9 +494,9 @@ protected function get_model_scope() * * @param string $expression The expression of the `SELECT` clause. e.g. 'nid, title'. * - * @return Query + * @return $this */ - public function select($expression) + public function select(string $expression) { $this->select = $expression; @@ -545,15 +545,10 @@ public function select($expression) * $query->join("INNER JOIN `articles` USING(`nid`)"); * * - * @return Query + * @return $this */ - public function join($expression, $options = []) + public function join($expression, array $options = []) { - if (is_string($expression) && $expression{0} == ':') - { - $expression = $this->model->models[substr($expression, 1)]; - } - if ($expression instanceof self) { $this->join_with_query($expression, $options); @@ -568,6 +563,11 @@ public function join($expression, $options = []) return $this; } + if (is_string($expression) && $expression{0} == ':') + { + $expression = $this->model->models[substr($expression, 1)]; + } + $this->joints[] = $expression; return $this; @@ -672,7 +672,7 @@ private function join_with_model(Model $model, array $options = []) * * @return string */ - private function render_join_on($column, $as, Query $query) + private function render_join_on(string $column, string $as, Query $query): string { if (isset($query->model->schema[$column]) && isset($this->model->schema[$column])) { @@ -691,7 +691,7 @@ private function render_join_on($column, $as, Query $query) * * @return array An array made of the condition string and its arguments. */ - private function deferred_parse_conditions(...$conditions_and_args) + private function deferred_parse_conditions(...$conditions_and_args): array { $conditions = array_shift($conditions_and_args); $args = $conditions_and_args; @@ -775,13 +775,13 @@ private function deferred_parse_conditions(...$conditions_and_args) * @param string $filter * @param array $conditions_args * - * @return Query + * @return $this */ - private function dynamic_filter($filter, array $conditions_args = []) + private function dynamic_filter(string $filter, array $conditions_args = []) { $conditions = explode('_and_', $filter); - return $this->where(array_combine($conditions, $conditions_args)); + return $this->and(array_combine($conditions, $conditions_args)); } /** @@ -835,7 +835,7 @@ private function dynamic_filter($filter, array $conditions_args = []) * * @param mixed ...$conditions_and_args * - * @return Query + * @return $this */ public function where(...$conditions_and_args) { @@ -861,7 +861,7 @@ public function where(...$conditions_and_args) * 'weight, date DESC', or field to order with, in which case `$field_values` is required. * @param array $field_values Values of the field specified by `$order_or_field_name`. * - * @return Query + * @return $this */ public function order($order_or_field_name, $field_values = null) { @@ -875,9 +875,9 @@ public function order($order_or_field_name, $field_values = null) * * @param string $group * - * @return Query + * @return $this */ - public function group($group) + public function group(string $group) { $this->group = $group; @@ -889,7 +889,7 @@ public function group($group) * * @param mixed ...$conditions_and_args * - * @return Query + * @return $this */ public function having(...$conditions_and_args) { @@ -904,13 +904,13 @@ public function having(...$conditions_and_args) /** * Define the offset of the `LIMIT` clause. * - * @param $offset + * @param int $offset * - * @return Query + * @return $this */ - public function offset($offset) + public function offset(int $offset) { - $this->offset = (int) $offset; + $this->offset = $offset; return $this; } @@ -959,9 +959,9 @@ public function limit($limit) * * @see http://www.php.net/manual/en/pdostatement.setfetchmode.php */ - public function mode($mode) + public function mode(...$mode) { - $this->mode = func_get_args(); + $this->mode = $mode; return $this; } @@ -974,7 +974,7 @@ public function mode($mode) * * @return Statement */ - protected function prepare() + protected function prepare(): Statement { return $this->model->connection->prepare((string) $this); } @@ -984,7 +984,7 @@ protected function prepare() * * @return Statement */ - protected function get_prepared() + protected function get_prepared(): Statement { return $this->prepare(); } @@ -994,7 +994,7 @@ protected function get_prepared() * * @return Statement */ - public function query() + public function query(): Statement { $statement = $this->prepare(); $statement->execute($this->args); @@ -1013,7 +1013,7 @@ public function query() * * @return array */ - private function resolve_fetch_mode(...$mode) + private function resolve_fetch_mode(...$mode): array { if ($mode) { @@ -1056,7 +1056,7 @@ public function all(...$mode) * * @return array */ - protected function get_all() + protected function get_all(): array { return $this->all(); } @@ -1108,7 +1108,7 @@ protected function get_one() * * @return array */ - protected function get_pairs() + protected function get_pairs(): array { return $this->all(\PDO::FETCH_KEY_PAIR); } @@ -1116,7 +1116,7 @@ protected function get_pairs() /** * Return the value of the first column of the first row. * - * @return string + * @return mixed */ protected function get_rc() { @@ -1216,11 +1216,11 @@ protected function get_exists() * Handle all the computations. * * @param string $method - * @param string $column + * @param string|null $column * * @return int|array */ - private function compute($method, $column) + private function compute(string $method, string $column = null) { $query = 'SELECT '; @@ -1270,7 +1270,7 @@ public function count($column = null) * * @return int */ - protected function get_count() + protected function get_count(): int { return $this->count(); } @@ -1280,9 +1280,9 @@ protected function get_count() * * @param string $column * - * @return int + * @return number */ - public function average($column) + public function average(string $column) { return $this->compute('AVG', $column); } @@ -1292,9 +1292,9 @@ public function average($column) * * @param string $column * - * @return int + * @return mixed */ - public function minimum($column) + public function minimum(string $column) { return $this->compute('MIN', $column); } @@ -1304,9 +1304,9 @@ public function minimum($column) * * @param string $column * - * @return int + * @return mixed */ - public function maximum($column) + public function maximum(string $column) { return $this->compute('MAX', $column); } @@ -1316,9 +1316,9 @@ public function maximum($column) * * @param string $column * - * @return int + * @return number */ - public function sum($column) + public function sum(string $column) { return $this->compute('SUM', $column); } diff --git a/lib/ActiveRecord/RecordNotFound.php b/lib/ActiveRecord/RecordNotFound.php index 9abda0a..c1f4e7e 100644 --- a/lib/ActiveRecord/RecordNotFound.php +++ b/lib/ActiveRecord/RecordNotFound.php @@ -30,9 +30,12 @@ class RecordNotFound extends \LogicException implements Exception * * @var ActiveRecord[] */ - private $records; + private $records = []; - protected function get_records() + /** + * @return ActiveRecord[] + */ + protected function get_records(): array { return $this->records; } @@ -45,7 +48,7 @@ protected function get_records() * @param int $code Defaults to 404. * @param \Exception $previous Previous exception. */ - public function __construct($message, array $records = [], $code = 404, \Exception $previous = null) + public function __construct(string $message, array $records = [], int $code = 404, \Exception $previous = null) { $this->records = $records; diff --git a/lib/ActiveRecord/Relation.php b/lib/ActiveRecord/Relation.php index c99c0da..30b6259 100644 --- a/lib/ActiveRecord/Relation.php +++ b/lib/ActiveRecord/Relation.php @@ -148,7 +148,7 @@ abstract public function __invoke(ActiveRecord $record); * @param Prototype $prototype The active record prototype. * @param string $property The name of the property. */ - protected function alter_prototype(Prototype $prototype, $property) + protected function alter_prototype(Prototype $prototype, string $property) { $prototype["get_$property"] = $this; } @@ -181,7 +181,7 @@ protected function resolve_activerecord_class(Model $model) * * @return string */ - protected function resolve_property_name($related) + protected function resolve_property_name($related): string { $related_id = $related instanceof Model ? $related->id : $related; $parts = explode('.', $related_id); @@ -194,7 +194,7 @@ protected function resolve_property_name($related) * * @return Model */ - protected function resolve_related() + protected function resolve_related(): Model { $related = $this->related; diff --git a/lib/ActiveRecord/RelationCollection.php b/lib/ActiveRecord/RelationCollection.php index 2d5343d..5a960f6 100644 --- a/lib/ActiveRecord/RelationCollection.php +++ b/lib/ActiveRecord/RelationCollection.php @@ -30,7 +30,10 @@ class RelationCollection implements \ArrayAccess */ protected $model; - protected function get_model() + /** + * @return Model + */ + protected function get_model(): Model { return $this->model; } @@ -73,7 +76,7 @@ public function offsetExists($relation_name) * * @throws RelationNotDefined if the relation is not defined. */ - public function offsetGet($relation_name) + public function offsetGet($relation_name): Relation { if (!$this->offsetExists($relation_name)) { @@ -123,7 +126,7 @@ public function offsetUnset($relation_name) * * @return Model */ - public function belongs_to($belongs_to) + public function belongs_to($belongs_to): Model { if (func_num_args() > 1) { @@ -169,7 +172,7 @@ public function belongs_to($belongs_to) * * @see HasManyRelation */ - public function has_many($related, array $options = []) + public function has_many($related, array $options = []): Model { if (is_array($related)) { diff --git a/lib/ActiveRecord/RelationNotDefined.php b/lib/ActiveRecord/RelationNotDefined.php index d4503ee..fde9c65 100644 --- a/lib/ActiveRecord/RelationNotDefined.php +++ b/lib/ActiveRecord/RelationNotDefined.php @@ -31,7 +31,10 @@ class RelationNotDefined extends OffsetNotDefined implements Exception */ private $relation_name; - protected function get_relation_name() + /** + * @return string + */ + protected function get_relation_name(): string { return $this->relation_name; } @@ -43,7 +46,10 @@ protected function get_relation_name() */ private $collection; - protected function get_collection() + /** + * @return RelationCollection + */ + protected function get_collection(): RelationCollection { return $this->collection; } @@ -54,7 +60,7 @@ protected function get_collection() * @param int $code * @param \Exception|null $previous */ - public function __construct($relation_name, RelationCollection $collection, $code = 500, \Exception $previous = null) + public function __construct(string $relation_name, RelationCollection $collection, int $code = 500, \Exception $previous = null) { $this->relation_name = $relation_name; $this->collection = $collection; diff --git a/lib/ActiveRecord/RuntimeActiveRecordCache.php b/lib/ActiveRecord/RuntimeActiveRecordCache.php index b9a22d0..b81102c 100644 --- a/lib/ActiveRecord/RuntimeActiveRecordCache.php +++ b/lib/ActiveRecord/RuntimeActiveRecordCache.php @@ -43,7 +43,7 @@ public function store(ActiveRecord $record) /** * @inheritdoc */ - public function retrieve($key) + public function retrieve(int $key) { if (empty($this->records[$key])) { @@ -56,7 +56,7 @@ public function retrieve($key) /** * @inheritdoc */ - public function eliminate($key) + public function eliminate(int $key) { unset($this->records[$key]); } diff --git a/lib/ActiveRecord/Schema.php b/lib/ActiveRecord/Schema.php index cea307e..6d7c851 100644 --- a/lib/ActiveRecord/Schema.php +++ b/lib/ActiveRecord/Schema.php @@ -35,7 +35,7 @@ class Schema implements \ArrayAccess, \IteratorAggregate /** * @return SchemaColumn[] */ - protected function get_columns() + protected function get_columns(): array { return $this->columns; } @@ -72,7 +72,7 @@ protected function get_primary() * * @return array */ - protected function get_indexes() + protected function get_indexes(): array { return $this->collect_indexes_by_type('indexed'); } @@ -82,7 +82,7 @@ protected function get_indexes() * * @return array */ - protected function get_unique_indexes() + protected function get_unique_indexes(): array { return $this->collect_indexes_by_type('unique'); } @@ -177,7 +177,7 @@ public function getIterator() * * @return array */ - private function collect_indexes_by_type($type) + private function collect_indexes_by_type(string $type): array { $indexes = []; @@ -203,7 +203,7 @@ private function collect_indexes_by_type($type) * * @return array */ - public function filter(array $values) + public function filter(array $values): array { return array_intersect_key($values, $this->columns); } diff --git a/lib/ActiveRecord/SchemaColumn.php b/lib/ActiveRecord/SchemaColumn.php index 2dedfd2..60e0498 100644 --- a/lib/ActiveRecord/SchemaColumn.php +++ b/lib/ActiveRecord/SchemaColumn.php @@ -72,7 +72,10 @@ class SchemaColumn */ protected $primary = false; - protected function get_primary() + /** + * @return bool + */ + protected function get_primary(): bool { return $this->primary; } @@ -155,7 +158,7 @@ public function __construct(array $options) * * @return string */ - protected function get_formatted_type() + protected function get_formatted_type(): string { $rc = ''; @@ -212,7 +215,7 @@ protected function get_formatted_type() * * @return string */ - protected function get_formatted_default() + protected function get_formatted_default(): string { $default = $this->default; @@ -238,7 +241,7 @@ protected function get_formatted_default() * * @return string */ - protected function get_formatted_attributes() + protected function get_formatted_attributes(): string { return $this->unsigned ? 'UNSIGNED' : ''; } @@ -248,7 +251,7 @@ protected function get_formatted_attributes() * * @return string */ - protected function get_formatted_null() + protected function get_formatted_null(): string { return $this->null ? 'NULL' : 'NOT NULL'; } @@ -258,7 +261,7 @@ protected function get_formatted_null() * * @return string */ - protected function get_formatted_index() + protected function get_formatted_index(): string { return implode(' ', array_filter([ @@ -273,7 +276,7 @@ protected function get_formatted_index() * * @return string */ - protected function get_formatted_comment() + protected function get_formatted_comment(): string { return $this->comment ? "`$this->comment`" : ''; } @@ -283,7 +286,7 @@ protected function get_formatted_comment() * * @return string */ - protected function get_formatted_charset() + protected function get_formatted_charset(): string { $charset = $this->charset; @@ -302,7 +305,7 @@ protected function get_formatted_charset() * * @return string */ - protected function get_formatted_auto_increment() + protected function get_formatted_auto_increment(): string { return $this->auto_increment ? 'AUTO_INCREMENT' : ''; } @@ -312,7 +315,7 @@ protected function get_formatted_auto_increment() * * @return bool */ - protected function get_is_serial() + protected function get_is_serial(): bool { return $this->type == self::TYPE_INTEGER && !$this->null && $this->auto_increment && $this->primary; } @@ -322,7 +325,7 @@ protected function get_is_serial() * * @return string */ - public function render() + public function render(): string { return implode(' ', array_filter([ @@ -337,7 +340,10 @@ public function render() ])); } - public function __toString() + /** + * @return string + */ + public function __toString(): string { return (string) $this->render(); } diff --git a/lib/ActiveRecord/ScopeNotDefined.php b/lib/ActiveRecord/ScopeNotDefined.php index 1d25e56..d9df930 100644 --- a/lib/ActiveRecord/ScopeNotDefined.php +++ b/lib/ActiveRecord/ScopeNotDefined.php @@ -30,7 +30,10 @@ class ScopeNotDefined extends \LogicException implements Exception */ private $scope_name; - protected function get_scope_name() + /** + * @return string + */ + protected function get_scope_name(): string { return $this->scope_name; } @@ -42,7 +45,10 @@ protected function get_scope_name() */ private $model; - protected function get_model() + /** + * @return Model + */ + protected function get_model(): Model { return $this->model; } @@ -55,7 +61,7 @@ protected function get_model() * @param int $code Default to 404. * @param \Exception $previous Previous exception. */ - public function __construct($scope_name, Model $model, $code = 500, \Exception $previous = null) + public function __construct(string $scope_name, Model $model, int $code = 500, \Exception $previous = null) { $this->scope_name = $scope_name; $this->model = $model; @@ -71,7 +77,7 @@ public function __construct($scope_name, Model $model, $code = 500, \Exception $ * * @return string */ - protected function format_message($scope_name, Model $model) + protected function format_message(string $scope_name, Model $model): string { return "Unknown scope `{$scope_name}` for model `{$model->unprefixed_name}`."; } diff --git a/lib/ActiveRecord/StartAtProperty.php b/lib/ActiveRecord/StartAtProperty.php index 89af1ae..e97564c 100644 --- a/lib/ActiveRecord/StartAtProperty.php +++ b/lib/ActiveRecord/StartAtProperty.php @@ -36,7 +36,7 @@ trait StartAtProperty * * @return DateTime */ - protected function get_start_at() + protected function get_start_at(): DateTime { return DateTimePropertySupport::get($this->start_at); } diff --git a/lib/ActiveRecord/StartedAtProperty.php b/lib/ActiveRecord/StartedAtProperty.php index 76c59f8..b75642a 100644 --- a/lib/ActiveRecord/StartedAtProperty.php +++ b/lib/ActiveRecord/StartedAtProperty.php @@ -36,7 +36,7 @@ trait StartedAtProperty * * @return DateTime */ - protected function get_started_at() + protected function get_started_at(): DateTime { return DateTimePropertySupport::get($this->started_at); } diff --git a/lib/ActiveRecord/Statement.php b/lib/ActiveRecord/Statement.php index 8722d3c..172c9b9 100644 --- a/lib/ActiveRecord/Statement.php +++ b/lib/ActiveRecord/Statement.php @@ -34,11 +34,17 @@ class Statement extends \PDOStatement */ private $connection; - protected function get_connection() + /** + * @return Connection + */ + protected function get_connection(): Connection { return $this->connection; } + /** + * @param Connection $connection + */ protected function set_connection(Connection $connection) { $this->connection = $connection; @@ -52,12 +58,12 @@ protected function set_connection(Connection $connection) * $statement(1, 2, 3); * $statement([ 1, 2, 3 ]); * + * @param array $args + * * @return $this */ - public function __invoke() + public function __invoke(...$args) { - $args = func_get_args(); - if ($args && is_array($args[0])) { $args = $args[0]; @@ -74,7 +80,7 @@ public function __invoke() /** * Return the {@link queryString} property of the statement. */ - public function __toString() + public function __toString(): string { return $this->queryString; } @@ -159,27 +165,9 @@ protected function get_one() } /** - * Fetches a column of the first row of the result set and closes the cursor. - * - * @param int $column_number - * - * @return string + * Fetches the first column of the first row of the result set and closes the cursor. * - * @see PDOStatement::fetchColumn() - * - * @deprecated - */ - public function fetchColumnAndClose($column_number = 0) - { - $rc = $this->fetchColumn($column_number); - - $this->closeCursor(); - - return $rc; - } - - /** - * Alias for `fetchColumnAndClose()`. + * @return mixed */ protected function get_rc() { @@ -197,7 +185,7 @@ protected function get_rc() * * @return array */ - public function all(...$mode) + public function all(...$mode): array { return $this->fetchAll(...$mode); } @@ -205,7 +193,7 @@ public function all(...$mode) /** * Alias for `all()`. */ - protected function get_all() + protected function get_all(): array { return $this->fetchAll(); } @@ -216,7 +204,7 @@ protected function get_all() * @return array An array of key/value pairs, where _key_ is the value of the first * column and _value_ the value of the second column. */ - protected function get_pairs() + protected function get_pairs(): array { return $this->fetchAll(\PDO::FETCH_KEY_PAIR); } diff --git a/lib/ActiveRecord/StatementInvocationFailed.php b/lib/ActiveRecord/StatementInvocationFailed.php index 9b21861..81bf9c1 100644 --- a/lib/ActiveRecord/StatementInvocationFailed.php +++ b/lib/ActiveRecord/StatementInvocationFailed.php @@ -28,7 +28,10 @@ class StatementInvocationFailed extends \LogicException implements Exception */ private $statement; - protected function get_statement() + /** + * @return Statement + */ + protected function get_statement(): Statement { return $this->statement; } @@ -36,9 +39,12 @@ protected function get_statement() /** * @var array */ - private $args; + private $args = []; - protected function get_args() + /** + * @return array + */ + protected function get_args(): array { return $this->args; } @@ -50,7 +56,7 @@ protected function get_args() * @param int $code * @param \Exception|null $previous */ - public function __construct(Statement $statement, array $args, $message = null, $code = 500, \Exception $previous = null) + public function __construct(Statement $statement, array $args, string $message = null, int $code = 500, \Exception $previous = null) { $this->statement = $statement; $this->args = $args; @@ -66,7 +72,7 @@ public function __construct(Statement $statement, array $args, $message = null, * * @return string */ - private function format_message(Statement $statement, array $args) + private function format_message(Statement $statement, array $args): string { return "Statement execution failed: {$statement->queryString}, with: " . json_encode($args); } diff --git a/lib/ActiveRecord/StatementNotValid.php b/lib/ActiveRecord/StatementNotValid.php index 32cc9ba..5ad9074 100644 --- a/lib/ActiveRecord/StatementNotValid.php +++ b/lib/ActiveRecord/StatementNotValid.php @@ -25,10 +25,13 @@ class StatementNotValid extends \RuntimeException implements Exception use AccessorTrait; /** - * @var string + * @var Statement|string */ private $statement; + /** + * @return Statement|string + */ protected function get_statement() { return $this->statement; @@ -37,25 +40,31 @@ protected function get_statement() /** * @var array */ - private $args; + private $args = []; + /** + * @return array + */ protected function get_args() { return $this->args; } /** - * @var \PDOException + * @var \PDOException|null */ private $original; + /** + * @return \PDOException|null + */ protected function get_original() { return $this->original; } /** - * @param array|string $statement + * @param Statement|array|string $statement * @param int $code * @param \PDOException|null $original */ diff --git a/lib/ActiveRecord/Table.php b/lib/ActiveRecord/Table.php index 01dbeb4..b3d550e 100644 --- a/lib/ActiveRecord/Table.php +++ b/lib/ActiveRecord/Table.php @@ -66,7 +66,7 @@ class Table extends Prototyped * * @return Connection */ - protected function get_connection() + protected function get_connection(): Connection { return $this->connection; } @@ -81,7 +81,7 @@ protected function get_connection() /** * @return string */ - protected function get_name() + protected function get_name(): string { return $this->name; } @@ -96,7 +96,7 @@ protected function get_name() /** * @return string */ - protected function get_unprefixed_name() + protected function get_unprefixed_name(): string { return $this->unprefixed_name; } @@ -129,7 +129,7 @@ protected function get_primary() /** * @return string */ - protected function get_alias() + protected function get_alias(): string { return $this->alias; } @@ -144,7 +144,7 @@ protected function get_alias() /** * @return Schema */ - protected function get_schema() + protected function get_schema(): Schema { return $this->schema; } @@ -159,7 +159,7 @@ protected function get_schema() /** * @return array */ - protected function get_schema_options() + protected function get_schema_options(): array { return $this->schema_options; } @@ -168,12 +168,12 @@ protected function get_schema_options() * The parent is used when the table is in a hierarchy, which is the case if the table * extends another table. * - * @var Table + * @var Table|null */ protected $parent; /** - * @return Table + * @return Table|null */ protected function get_parent() { @@ -193,7 +193,7 @@ protected function get_parent() /** * @return string */ - protected function lazy_get_update_join() + protected function lazy_get_update_join(): string { $join = ''; $parent = $this->parent; @@ -220,7 +220,7 @@ protected function lazy_get_update_join() /** * @return string */ - protected function lazy_get_select_join() + protected function lazy_get_select_join(): string { $join = "`{$this->alias}`" . $this->update_join; @@ -245,7 +245,7 @@ protected function lazy_get_select_join() * * @return Schema */ - protected function lazy_get_extended_schema() + protected function lazy_get_extended_schema(): Schema { $table = $this; $options = []; @@ -304,15 +304,15 @@ public function __construct(array $attributes) /** * Alias to {@link query()}. * - * @param string $query + * @param string $statement * @param array $args * @param array $options * * @return Statement */ - public function __invoke($query, array $args = [], array $options = []) + public function __invoke(string $statement, array $args = [], array $options = []): Statement { - return $this->query($query, $args, $options); + return $this->query($statement, $args, $options); } /** @@ -502,7 +502,7 @@ public function uninstall() * * @return bool `true` if the table exists, `false` otherwise. */ - public function is_installed() + public function is_installed(): bool { return $this->connection->table_exists($this->unprefixed_name); } @@ -528,7 +528,7 @@ public function is_installed() * * @return string */ - public function resolve_statement($statement) + public function resolve_statement($statement): string { $primary = $this->primary; $primary = is_array($primary) ? '__multicolumn_primary__' . implode('_', $primary) : $primary; @@ -554,7 +554,7 @@ public function resolve_statement($statement) * * @return Statement */ - public function prepare($query, $options = []) + public function prepare($query, $options = []): Statement { $query = $this->resolve_statement($query); @@ -569,7 +569,7 @@ public function prepare($query, $options = []) * * @return string */ - public function quote($string, $parameter_type = \PDO::PARAM_STR) + public function quote(string $string, int $parameter_type = \PDO::PARAM_STR): string { return $this->connection->quote($string, $parameter_type); } @@ -579,15 +579,15 @@ public function quote($string, $parameter_type = \PDO::PARAM_STR) * * The statement is prepared by the {@link prepare()} method before it is executed. * - * @param string $query + * @param string $statement * @param array $args * @param array $options * * @return mixed */ - public function execute($query, array $args = [], array $options = []) + public function execute(string $statement, array $args = [], array $options = []) { - $statement = $this->prepare($query, $options); + $statement = $this->prepare($statement, $options); return $statement($args); } @@ -603,7 +603,7 @@ public function execute($query, array $args = [], array $options = []) * * @return Statement */ - public function query($query, array $args = [], array $options = []) + public function query(string $query, array $args = [], array $options = []) { $statement = $this->prepare($this->resolve_statement($query), $options); $statement($args); @@ -615,11 +615,11 @@ public function query($query, array $args = [], array $options = []) * Filters mass assignment values. * * @param array $values - * @param bool|false $extended + * @param bool $extended * * @return array */ - protected function filter_values(array $values, $extended = false) + protected function filter_values(array $values, bool $extended = false): array { $filtered = []; $holders = []; @@ -914,7 +914,7 @@ public function update(array $values, $key) * * @return bool */ - public function delete($key) + public function delete($key): bool { if ($this->parent) { @@ -973,7 +973,7 @@ public function truncate() * * @return mixed */ - public function drop(array $options=[]) + public function drop(array $options = []) { $query = 'DROP TABLE '; diff --git a/lib/ActiveRecord/UnableToSetFetchMode.php b/lib/ActiveRecord/UnableToSetFetchMode.php index 51ec83a..946cd87 100644 --- a/lib/ActiveRecord/UnableToSetFetchMode.php +++ b/lib/ActiveRecord/UnableToSetFetchMode.php @@ -25,22 +25,25 @@ class UnableToSetFetchMode extends \RuntimeException implements Exception use AccessorTrait; /** - * @var int + * @var mixed */ private $mode; + /** + * @return mixed + */ protected function get_mode() { return $this->mode; } /** - * @param int $mode - * @param null $message + * @param mixed $mode + * @param string|null $message * @param int $code * @param \Exception|null $previous */ - public function __construct($mode, $message = null, $code = 500, \Exception $previous = null) + public function __construct($mode, string $message = null, int $code = 500, \Exception $previous = null) { $this->mode = $mode; @@ -50,11 +53,11 @@ public function __construct($mode, $message = null, $code = 500, \Exception $pre /** * Formats exception message. * - * @param int $mode + * @param mixed $mode * * @return string */ - protected function format_message($mode) + protected function format_message($mode): string { return format("Unable to set fetch mode: %mode", [ 'mode' => $mode ]); } diff --git a/lib/ActiveRecord/UpdatedAtProperty.php b/lib/ActiveRecord/UpdatedAtProperty.php index cd91e9c..9c807dd 100644 --- a/lib/ActiveRecord/UpdatedAtProperty.php +++ b/lib/ActiveRecord/UpdatedAtProperty.php @@ -34,7 +34,7 @@ trait UpdatedAtProperty * * @return DateTime */ - protected function get_updated_at() + protected function get_updated_at(): DateTime { return DateTimePropertySupport::get($this->updated_at); } diff --git a/tests/ActiveRecord/ExceptionTest.php b/tests/ActiveRecord/ExceptionTest.php index 1183ee2..ee19c04 100644 --- a/tests/ActiveRecord/ExceptionTest.php +++ b/tests/ActiveRecord/ExceptionTest.php @@ -25,10 +25,29 @@ public function test_implementing($classname, $ctor_args) public function provide_test_implementing() { - $fake_model = $this - ->getMockBuilder(Model::class) + $connection = $this + ->getMockBuilder(Connection::class) ->disableOriginalConstructor() - ->getMockForAbstractClass(); + ->getMock(); + + $models = $this + ->getMockBuilder(ModelCollection::class) + ->disableOriginalConstructor() + ->getMock(); + + /* @var $models ModelCollection */ + + $model = new Model($models, [ + + Model::CONNECTION => $connection, + Model::NAME => 'testing' . uniqid(), + Model::SCHEMA => [ + + 'id' => 'serial' + + ] + + ]); return [ @@ -37,7 +56,7 @@ public function provide_test_implementing() [ 'ConnectionAlreadyEstablished', [ 'connection-name' ] ], [ 'RecordNotFound', [ "message", [] ] ], - [ 'ScopeNotDefined', [ 'scope-name', $fake_model ] ], + [ 'ScopeNotDefined', [ 'scope-name', $model ] ], [ 'ModelNotDefined' , [ 'model-name' ] ], [ 'ModelAlreadyInstantiated' , [ 'model-name' ] ], diff --git a/tests/ActiveRecord/ModelTest.php b/tests/ActiveRecord/ModelTest.php index 9f144f5..c3014a9 100644 --- a/tests/ActiveRecord/ModelTest.php +++ b/tests/ActiveRecord/ModelTest.php @@ -17,7 +17,6 @@ use ICanBoogie\ActiveRecord\ModelTest\Brand; use ICanBoogie\ActiveRecord\ModelTest\Car; use ICanBoogie\ActiveRecord\ModelTest\Comment; -use ICanBoogie\ActiveRecord\ModelTest\Driver; use ICanBoogie\DateTime; use ICanBoogie\OffsetNotWritable; @@ -561,7 +560,7 @@ public function test_belongs_to() 'drivers' => [ - Model::ACTIVERECORD_CLASS => Driver::class, + Model::ACTIVERECORD_CLASS => ModelTest\Driver::class, Model::SCHEMA => [ 'driver_id' => 'serial', @@ -612,7 +611,7 @@ public function test_belongs_to() # driver $driver = $drivers->new([ 'name' => 'Madonna' ]); - $this->assertInstanceOf(Driver::class, $driver); + $this->assertInstanceOf(ModelTest\Driver::class, $driver); $driver_id = $driver->save(); # brand @@ -625,7 +624,7 @@ public function test_belongs_to() $car->brand_id = $brand_id; $car->save(); - $this->assertInstanceOf(Driver::class, $car->driver); + $this->assertInstanceOf(ModelTest\Driver::class, $car->driver); $this->assertInstanceOf(Brand::class, $car->brand); $car->driver_id = null; diff --git a/tests/ActiveRecordTest/Sample.php b/tests/ActiveRecordTest/Sample.php index b404038..621f038 100644 --- a/tests/ActiveRecordTest/Sample.php +++ b/tests/ActiveRecordTest/Sample.php @@ -33,7 +33,7 @@ protected function get_id() * * @inheritdoc */ - protected function alter_persistent_properties(array $properties, ActiveRecord\Model $model) + protected function alter_persistent_properties(array $properties, ActiveRecord\Model $model): array { return array_merge(parent::alter_persistent_properties($properties, $model), [