From d19cf70c2b5e0cb8e2257ec2846860be3dc4648b Mon Sep 17 00:00:00 2001 From: Dave Redfern Date: Wed, 27 Oct 2021 14:38:17 -0400 Subject: [PATCH] Minor cleanup and bug fixes Deprecated passing array as arg to with Add more PHP8 code features --- CHANGELOG.md | 11 ++++ composer.json | 4 +- src/ConnectionManager.php | 1 - .../IdentityMapClearerMessengerSubscriber.php | 6 +- .../IdentityMapClearerSubscriber.php | 6 +- src/Exceptions/ConnectionManagerException.php | 1 - src/Exceptions/EntityNotFoundException.php | 3 +- src/Exceptions/JsonEncodingException.php | 1 - src/Exceptions/NoResultsException.php | 1 - src/Manager.php | 1 - src/Model.php | 1 - src/ModelBuilder.php | 9 ++- src/ModelExporter.php | 1 - src/ModelIdentityMap.php | 1 - src/ModelMetadata.php | 1 - src/PHPUnit/PHPUnitListener.php | 1 - src/PaginatorAdapter.php | 1 - src/Relationships/AbstractRelationship.php | 1 - src/Relationships/BelongsTo.php | 1 - src/Relationships/BelongsToMany.php | 1 - src/Relationships/HasOne.php | 1 - src/Relationships/HasOneOrMany.php | 1 - src/Relationships/HasOneToMany.php | 1 - src/TypeCasters/DoctrineTypeCaster.php | 6 +- src/Utils/ClassHelpers.php | 4 +- .../FilterGeneratedKeysFromCollection.php | 1 - .../GenerateRelationshipsToEagerLoad.php | 7 +- src/Utils/ProxyTo.php | 1 - tests/resources/seed.php | 64 +++++++++---------- 29 files changed, 62 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25000ac..bf1fc36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ Change Log ========== +2021-10-27 - 3.1.0 +------------------ + + * fix bug in `GenerateRelationshipsToEagerLoad` + +2021-10-05 +---------- + + * deprecate passing an array as first argument on `with()`s + * use more PHP8 syntax in classes + 2021-01-25 ---------- diff --git a/composer.json b/composer.json index 7af0d5a..db0569a 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,10 @@ "php": ">=8.0", "ext-json": "*", "doctrine/dbal": "~2.11", - "pagerfanta/pagerfanta": "~2.4", + "pagerfanta/pagerfanta": "~2.6", "pragmarx/ia-str": "^7.0", "somnambulist/attribute-model": "~2.0", - "somnambulist/collection": "~5.0" + "somnambulist/collection": "~5.2" }, "require-dev": { "phpunit/phpunit": "~9.5", diff --git a/src/ConnectionManager.php b/src/ConnectionManager.php index 2081c00..61f50a6 100644 --- a/src/ConnectionManager.php +++ b/src/ConnectionManager.php @@ -14,7 +14,6 @@ */ final class ConnectionManager { - private array $connections = []; public function __construct(array $connections) diff --git a/src/EventSubscribers/IdentityMapClearerMessengerSubscriber.php b/src/EventSubscribers/IdentityMapClearerMessengerSubscriber.php index 2a6d9aa..18470ad 100644 --- a/src/EventSubscribers/IdentityMapClearerMessengerSubscriber.php +++ b/src/EventSubscribers/IdentityMapClearerMessengerSubscriber.php @@ -20,12 +20,8 @@ */ class IdentityMapClearerMessengerSubscriber implements EventSubscriberInterface { - - private Manager $manager; - - public function __construct(Manager $manager) + public function __construct(private Manager $manager) { - $this->manager = $manager; } public static function getSubscribedEvents() diff --git a/src/EventSubscribers/IdentityMapClearerSubscriber.php b/src/EventSubscribers/IdentityMapClearerSubscriber.php index acd7c82..a390cdb 100644 --- a/src/EventSubscribers/IdentityMapClearerSubscriber.php +++ b/src/EventSubscribers/IdentityMapClearerSubscriber.php @@ -21,12 +21,8 @@ */ class IdentityMapClearerSubscriber implements EventSubscriberInterface { - - private Manager $manager; - - public function __construct(Manager $manager) + public function __construct(private Manager $manager) { - $this->manager = $manager; } public static function getSubscribedEvents() diff --git a/src/Exceptions/ConnectionManagerException.php b/src/Exceptions/ConnectionManagerException.php index 9326958..0790551 100644 --- a/src/Exceptions/ConnectionManagerException.php +++ b/src/Exceptions/ConnectionManagerException.php @@ -13,7 +13,6 @@ */ class ConnectionManagerException extends Exception { - public static function missingConnectionFor(string $model): self { return new self(sprintf('No connection found for "%s" or "default"', $model)); diff --git a/src/Exceptions/EntityNotFoundException.php b/src/Exceptions/EntityNotFoundException.php index 1dd2d9a..3b996c8 100644 --- a/src/Exceptions/EntityNotFoundException.php +++ b/src/Exceptions/EntityNotFoundException.php @@ -13,9 +13,8 @@ */ class EntityNotFoundException extends Exception { - public static function noMatchingRecordFor(string $class, string $key, $id): self { - return new self(sprintf('Could not find a record for %s with %s and %s', $class, $key, $id)); + return new self(sprintf('Could not find a record for %s with %s and %s', $class, $key, $id), 404); } } diff --git a/src/Exceptions/JsonEncodingException.php b/src/Exceptions/JsonEncodingException.php index fdf7bcb..743b3d9 100644 --- a/src/Exceptions/JsonEncodingException.php +++ b/src/Exceptions/JsonEncodingException.php @@ -15,7 +15,6 @@ */ class JsonEncodingException extends RuntimeException { - public static function failedToConvertModel(Model $model, string $error): JsonEncodingException { return new self( diff --git a/src/Exceptions/NoResultsException.php b/src/Exceptions/NoResultsException.php index 016744e..09bbbb6 100644 --- a/src/Exceptions/NoResultsException.php +++ b/src/Exceptions/NoResultsException.php @@ -14,7 +14,6 @@ */ class NoResultsException extends Exception { - private QueryBuilder $query; public function __construct(string $class, QueryBuilder $queryBuilder) diff --git a/src/Manager.php b/src/Manager.php index e981172..0b6eabd 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -16,7 +16,6 @@ */ final class Manager { - private static ?Manager $instance = null; private ConnectionManager $connections; private AttributeCaster $caster; diff --git a/src/Model.php b/src/Model.php index a8dc488..498b968 100644 --- a/src/Model.php +++ b/src/Model.php @@ -31,7 +31,6 @@ */ abstract class Model extends AbstractModel implements Arrayable, Jsonable, JsonSerializable { - /** * The table associated with the model */ diff --git a/src/ModelBuilder.php b/src/ModelBuilder.php index 7104bef..1bbd5a7 100644 --- a/src/ModelBuilder.php +++ b/src/ModelBuilder.php @@ -62,7 +62,6 @@ */ class ModelBuilder implements Queryable { - private Model $model; private ModelMetadata $meta; private QueryBuilder $query; @@ -258,7 +257,7 @@ public function paginate(int $page = 1, int $perPage = 30): Pagerfanta /** * Set the relationships that should be eager loaded * - * @param mixed $relations Strings of relationship names, or an array + * @param mixed $relations Strings of relationship names * * @return $this */ @@ -472,6 +471,12 @@ public function where($expression, array $values = []): self */ public function orWhere($expression, array $values = []): self { + if (is_callable($expression)) { + $expression($this); + + return $this; + } + $this->query->orWhere($expression); foreach ($values as $key => $value) { diff --git a/src/ModelExporter.php b/src/ModelExporter.php index 425f800..193bf7f 100644 --- a/src/ModelExporter.php +++ b/src/ModelExporter.php @@ -19,7 +19,6 @@ */ final class ModelExporter implements Jsonable { - private Model $model; private array $attributes; private array $relationships; diff --git a/src/ModelIdentityMap.php b/src/ModelIdentityMap.php index 9d74f04..cc553bf 100644 --- a/src/ModelIdentityMap.php +++ b/src/ModelIdentityMap.php @@ -17,7 +17,6 @@ */ final class ModelIdentityMap { - private array $identityMap = []; private array $aliases = []; private array $relationships = []; diff --git a/src/ModelMetadata.php b/src/ModelMetadata.php index 0ccee2c..206d2e9 100644 --- a/src/ModelMetadata.php +++ b/src/ModelMetadata.php @@ -17,7 +17,6 @@ */ final class ModelMetadata { - private Model $model; private string $table; private string $primaryKey; diff --git a/src/PHPUnit/PHPUnitListener.php b/src/PHPUnit/PHPUnitListener.php index 9823edd..a731767 100644 --- a/src/PHPUnit/PHPUnitListener.php +++ b/src/PHPUnit/PHPUnitListener.php @@ -26,7 +26,6 @@ */ class PHPUnitListener implements BeforeTestHook, AfterTestHook { - public function executeAfterTest(string $test, float $time): void { Manager::clear(); diff --git a/src/PaginatorAdapter.php b/src/PaginatorAdapter.php index 9ad3578..1ac3dcc 100644 --- a/src/PaginatorAdapter.php +++ b/src/PaginatorAdapter.php @@ -14,7 +14,6 @@ */ final class PaginatorAdapter implements AdapterInterface { - private ModelBuilder $builder; public function __construct(ModelBuilder $queryBuilder) diff --git a/src/Relationships/AbstractRelationship.php b/src/Relationships/AbstractRelationship.php index 299b47f..9125f18 100644 --- a/src/Relationships/AbstractRelationship.php +++ b/src/Relationships/AbstractRelationship.php @@ -68,7 +68,6 @@ */ abstract class AbstractRelationship implements Queryable { - /** * A prefix used to flag attributes generated by ReadModels * diff --git a/src/Relationships/BelongsTo.php b/src/Relationships/BelongsTo.php index ede1c21..a377c47 100644 --- a/src/Relationships/BelongsTo.php +++ b/src/Relationships/BelongsTo.php @@ -17,7 +17,6 @@ */ class BelongsTo extends AbstractRelationship { - protected string $foreignKey; protected string $ownerKey; protected bool $nullOnNotFound; diff --git a/src/Relationships/BelongsToMany.php b/src/Relationships/BelongsToMany.php index affce98..88ab380 100644 --- a/src/Relationships/BelongsToMany.php +++ b/src/Relationships/BelongsToMany.php @@ -19,7 +19,6 @@ */ class BelongsToMany extends AbstractRelationship { - private string $joinTable; private string $joinTableSourceKey; private string $joinTableTargetKey; diff --git a/src/Relationships/HasOne.php b/src/Relationships/HasOne.php index 6399550..02a3739 100644 --- a/src/Relationships/HasOne.php +++ b/src/Relationships/HasOne.php @@ -17,7 +17,6 @@ */ class HasOne extends HasOneOrMany { - private bool $nullOnNotFound; public function __construct(ModelBuilder $builder, Model $parent, string $foreignKey, string $localKey, bool $nullOnNotFound = true) diff --git a/src/Relationships/HasOneOrMany.php b/src/Relationships/HasOneOrMany.php index f6a6819..ae84e4e 100644 --- a/src/Relationships/HasOneOrMany.php +++ b/src/Relationships/HasOneOrMany.php @@ -14,7 +14,6 @@ */ abstract class HasOneOrMany extends AbstractRelationship { - protected string $foreignKey; protected string $localKey; diff --git a/src/Relationships/HasOneToMany.php b/src/Relationships/HasOneToMany.php index 830e5a8..3e52fcf 100644 --- a/src/Relationships/HasOneToMany.php +++ b/src/Relationships/HasOneToMany.php @@ -17,7 +17,6 @@ */ class HasOneToMany extends HasOneOrMany { - protected ?string $indexBy; public function __construct(ModelBuilder $builder, Model $parent, string $foreignKey, string $localKey, ?string $indexBy = null) diff --git a/src/TypeCasters/DoctrineTypeCaster.php b/src/TypeCasters/DoctrineTypeCaster.php index 0621698..8bead80 100644 --- a/src/TypeCasters/DoctrineTypeCaster.php +++ b/src/TypeCasters/DoctrineTypeCaster.php @@ -19,12 +19,8 @@ */ final class DoctrineTypeCaster implements AttributeCasterInterface { - - private Connection $connection; - - public function __construct(Connection $connection) + public function __construct(private Connection $connection) { - $this->connection = $connection; } public function types(): array diff --git a/src/Utils/ClassHelpers.php b/src/Utils/ClassHelpers.php index b6548f8..3fc605f 100644 --- a/src/Utils/ClassHelpers.php +++ b/src/Utils/ClassHelpers.php @@ -84,7 +84,7 @@ public static function getCallingMethod(): string * * @return object */ - public static function set(object $object, string $property, mixed $value, mixed $scope = null) + public static function set(object $object, string $property, mixed $value, mixed $scope = null): object { Closure::bind(function () use ($property, $value) { $this->{$property} = $value; @@ -106,7 +106,7 @@ public static function set(object $object, string $property, mixed $value, mixed * * @return object */ - public static function setPropertyArrayKey(object $object, string $property, string $key, mixed $value, mixed $scope = null) + public static function setPropertyArrayKey(object $object, string $property, string $key, mixed $value, mixed $scope = null): object { Closure::bind(function () use ($property, $key, $value) { $this->{$property}[$key] = $value; diff --git a/src/Utils/FilterGeneratedKeysFromCollection.php b/src/Utils/FilterGeneratedKeysFromCollection.php index b9727cf..780e7cd 100644 --- a/src/Utils/FilterGeneratedKeysFromCollection.php +++ b/src/Utils/FilterGeneratedKeysFromCollection.php @@ -15,7 +15,6 @@ */ final class FilterGeneratedKeysFromCollection { - /** * Filters out library generated keys from the set of attributes * diff --git a/src/Utils/GenerateRelationshipsToEagerLoad.php b/src/Utils/GenerateRelationshipsToEagerLoad.php index 4341893..bb62a65 100644 --- a/src/Utils/GenerateRelationshipsToEagerLoad.php +++ b/src/Utils/GenerateRelationshipsToEagerLoad.php @@ -10,6 +10,7 @@ use function implode; use function is_array; use function is_numeric; +use function trigger_deprecation; /** * Class GenerateRelationshipsToEagerLoad @@ -24,19 +25,19 @@ */ final class GenerateRelationshipsToEagerLoad { - /** * Set the relationships that should be eager loaded * * @param array $toEagerLoad The default eager loads defined on the model - * @param mixed $relations Strings of relationship names, or an array + * @param mixed $relations Strings of relationship names * * @return array */ public function __invoke(array $toEagerLoad = [], ...$relations): array { - if (is_array($relations[0])) { + if (count($relations) > 0 && is_array($relations[0])) { // if an array is passed, it will be the first arg and wont unpack + trigger_deprecation('somnambulist/read-models', '3.1.0', 'Passing an array as argument is deprecated, use multiple string arguments'); $relations = $relations[0]; } diff --git a/src/Utils/ProxyTo.php b/src/Utils/ProxyTo.php index 95d5dd8..8550956 100644 --- a/src/Utils/ProxyTo.php +++ b/src/Utils/ProxyTo.php @@ -20,7 +20,6 @@ */ class ProxyTo { - /** * Proxy a call into the specified object, collecting error information if it fails * diff --git a/tests/resources/seed.php b/tests/resources/seed.php index 701aab4..28dff14 100755 --- a/tests/resources/seed.php +++ b/tests/resources/seed.php @@ -85,16 +85,16 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $output->writeln('Deleting all records in configured test db'); - $this->conn->exec('delete from roles'); - $this->conn->exec('delete from users'); - $this->conn->exec('delete from permissions'); - $this->conn->exec('delete from role_permissions'); - $this->conn->exec('delete from user_addresses'); - $this->conn->exec('delete from user_contacts'); - $this->conn->exec('delete from user_profiles'); - $this->conn->exec('delete from user_relations'); - $this->conn->exec('delete from user_roles'); - $this->conn->exec('vacuum'); + $this->conn->executeStatement('delete from roles'); + $this->conn->executeStatement('delete from users'); + $this->conn->executeStatement('delete from permissions'); + $this->conn->executeStatement('delete from role_permissions'); + $this->conn->executeStatement('delete from user_addresses'); + $this->conn->executeStatement('delete from user_contacts'); + $this->conn->executeStatement('delete from user_profiles'); + $this->conn->executeStatement('delete from user_relations'); + $this->conn->executeStatement('delete from user_roles'); + $this->conn->executeStatement('vacuum'); $output->writeln('Done'); } catch (Exception $e) { @@ -133,7 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $output->writeln('Creating tables and indexes for db'); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS roles ( id integer PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(100) NOT NULL, @@ -141,7 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output) updated_at datetime NOT NULL ) '); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS permissions ( id integer PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(100) NOT NULL, @@ -149,7 +149,7 @@ protected function execute(InputInterface $input, OutputInterface $output) updated_at datetime NOT NULL ) '); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS role_permissions ( role_id integer NOT NULL, permission_id integer NOT NULL, @@ -159,7 +159,7 @@ protected function execute(InputInterface $input, OutputInterface $output) ) '); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS users ( id integer PRIMARY KEY AUTOINCREMENT NOT NULL, uuid char(36) NOT NULL, @@ -171,7 +171,7 @@ protected function execute(InputInterface $input, OutputInterface $output) password varchar(255) NOT NULL ) '); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS user_addresses ( id integer PRIMARY KEY AUTOINCREMENT NOT NULL, user_id integer NOT NULL, @@ -187,7 +187,7 @@ protected function execute(InputInterface $input, OutputInterface $output) FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ) '); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS user_contacts ( id integer PRIMARY KEY AUTOINCREMENT NOT NULL, user_id integer NOT NULL, @@ -200,7 +200,7 @@ protected function execute(InputInterface $input, OutputInterface $output) FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ) '); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS user_relations ( user_source integer NOT NULL, user_target integer NOT NULL, @@ -209,7 +209,7 @@ protected function execute(InputInterface $input, OutputInterface $output) PRIMARY KEY(user_source, user_target) ) '); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS user_roles ( user_id integer NOT NULL, role_id integer NOT NULL, @@ -218,7 +218,7 @@ protected function execute(InputInterface $input, OutputInterface $output) PRIMARY KEY(user_id, role_id) ) '); - $this->conn->exec(' + $this->conn->executeStatement(' CREATE TABLE IF NOT EXISTS user_profiles ( id integer PRIMARY KEY AUTOINCREMENT NOT NULL, user_uuid varchar(36) NOT NULL, @@ -229,19 +229,19 @@ protected function execute(InputInterface $input, OutputInterface $output) ) '); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS idx_roles_name ON roles (name ASC)'); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS uniq_permissions_name ON permissions (name ASC)'); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS uniq_role_permissions_role_id_permission_id ON role_permissions (role_id ASC, permission_id ASC)'); - $this->conn->exec('CREATE INDEX IF NOT EXISTS idx_user_addresses_type ON user_addresses (type ASC)'); - $this->conn->exec('CREATE INDEX IF NOT EXISTS idx_users_is_active ON users (is_active ASC)'); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS uniq_users_email ON users (email ASC)'); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS uniq_users_uuid ON users (uuid ASC)'); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS uniq_user_addresses_user_id_type ON user_addresses (user_id ASC, type ASC)'); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS uniq_user_contacts_user_id_type ON user_contacts (user_id ASC, type ASC)'); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS uniq_user_relations_user_relationship ON user_relations (user_source ASC, user_target ASC)'); - $this->conn->exec('CREATE UNIQUE INDEX IF NOT EXISTS uniq_user_roles_user_id_role_id ON user_roles (user_id ASC, role_id ASC)'); - - $this->conn->exec('vacuum'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS idx_roles_name ON roles (name ASC)'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS uniq_permissions_name ON permissions (name ASC)'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS uniq_role_permissions_role_id_permission_id ON role_permissions (role_id ASC, permission_id ASC)'); + $this->conn->executeStatement('CREATE INDEX IF NOT EXISTS idx_user_addresses_type ON user_addresses (type ASC)'); + $this->conn->executeStatement('CREATE INDEX IF NOT EXISTS idx_users_is_active ON users (is_active ASC)'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS uniq_users_email ON users (email ASC)'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS uniq_users_uuid ON users (uuid ASC)'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS uniq_user_addresses_user_id_type ON user_addresses (user_id ASC, type ASC)'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS uniq_user_contacts_user_id_type ON user_contacts (user_id ASC, type ASC)'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS uniq_user_relations_user_relationship ON user_relations (user_source ASC, user_target ASC)'); + $this->conn->executeStatement('CREATE UNIQUE INDEX IF NOT EXISTS uniq_user_roles_user_id_role_id ON user_roles (user_id ASC, role_id ASC)'); + + $this->conn->executeStatement('vacuum'); $output->writeln('Done'); } catch (Exception $e) {