diff --git a/psalm-baseline.xml b/psalm-baseline.xml index edaa10a..5295df3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + $db diff --git a/src/Attribute/Entity.php b/src/Attribute/Entity.php index b7d6014..ca51b81 100644 --- a/src/Attribute/Entity.php +++ b/src/Attribute/Entity.php @@ -5,21 +5,12 @@ #[\Attribute(\Attribute::TARGET_CLASS)] class Entity { - /** - * @var string Name of the SQL table - */ - private string $name = ''; - - /** - * @var string Database connection - if empty the default connection is used - */ - private string $connection = ''; - - public function __construct(string $name, string $connection = '') - { - $this->name = $name; - $this->connection = $connection; - + public function __construct( + /** @var string Name of the SQL table */ + private string $name, + /** @var string Database connection - if empty the default connection is used */ + private string $connection = '', + ) { if (\strlen($this->name) === 0) { throw new \InvalidArgumentException('No name provided for entity'); } diff --git a/src/Attribute/EntityProcessor.php b/src/Attribute/EntityProcessor.php index 02f362b..55e11f6 100644 --- a/src/Attribute/EntityProcessor.php +++ b/src/Attribute/EntityProcessor.php @@ -13,7 +13,7 @@ class EntityProcessor /** * Processes a class according to its attributes * - * @psalm-param object|class-string $class + * @param object|class-string $class */ public function process(object|string $class): ?RepositoryConfigInterface { diff --git a/src/Attribute/Field.php b/src/Attribute/Field.php index 8ffb52c..fbca166 100644 --- a/src/Attribute/Field.php +++ b/src/Attribute/Field.php @@ -5,27 +5,14 @@ #[\Attribute(\Attribute::TARGET_PROPERTY)] class Field { - /** - * @var string Name of the field in the SQL table - */ - private string $name = ''; - - /** - * @var bool Whether this is the autoincrement field for the table - only one per table is legal! - */ - private bool $autoincrement = false; - - /** - * @var bool Whether this is a blob field (binary large object) - needed for Postgres compatibility - */ - private bool $blob = false; - - public function __construct(string $name, bool $autoincrement = false, bool $blob = false) - { - $this->name = $name; - $this->autoincrement = $autoincrement; - $this->blob = $blob; - + public function __construct( + /** @var string Name of the field in the SQL table */ + private string $name, + /** @var bool Whether this is the autoincrement field for the table - only one per table is legal! */ + private bool $autoincrement = false, + /** @var bool Whether this is a blob field (binary large object) - needed for Postgres compatibility */ + private bool $blob = false, + ) { if (\strlen($this->name) === 0) { throw new \InvalidArgumentException('No name provided for field'); } diff --git a/src/Builder/CountEntries.php b/src/Builder/CountEntries.php index 1186bff..9e8563e 100644 --- a/src/Builder/CountEntries.php +++ b/src/Builder/CountEntries.php @@ -10,8 +10,6 @@ */ class CountEntries implements BuilderInterface { - private RepositoryReadOnlyInterface $repository; - /** * @var array WHERE restrictions in query */ @@ -22,9 +20,9 @@ class CountEntries implements BuilderInterface */ private bool $blocking = false; - public function __construct(RepositoryReadOnlyInterface $repository) - { - $this->repository = $repository; + public function __construct( + private RepositoryReadOnlyInterface $repository, + ) { } /** diff --git a/src/Builder/DeleteEntries.php b/src/Builder/DeleteEntries.php index 94d9056..40dd3a3 100644 --- a/src/Builder/DeleteEntries.php +++ b/src/Builder/DeleteEntries.php @@ -12,8 +12,6 @@ */ class DeleteEntries implements BuilderInterface { - private RepositoryWriteableInterface $repository; - /** * @var array WHERE restrictions in query */ @@ -24,9 +22,9 @@ class DeleteEntries implements BuilderInterface */ private bool $confirmNoWhere = false; - public function __construct(RepositoryWriteableInterface $repository) - { - $this->repository = $repository; + public function __construct( + private RepositoryWriteableInterface $repository, + ) { } /** diff --git a/src/Builder/InsertEntry.php b/src/Builder/InsertEntry.php index e803cb8..1a3f792 100644 --- a/src/Builder/InsertEntry.php +++ b/src/Builder/InsertEntry.php @@ -10,16 +10,14 @@ */ class InsertEntry implements BuilderInterface { - private RepositoryWriteableInterface $repository; - /** * @var array VALUES clauses for the query */ private array $values = []; - public function __construct(RepositoryWriteableInterface $repository) - { - $this->repository = $repository; + public function __construct( + private RepositoryWriteableInterface $repository, + ) { } /** diff --git a/src/Builder/InsertOrUpdateEntry.php b/src/Builder/InsertOrUpdateEntry.php index d2db622..5309946 100644 --- a/src/Builder/InsertOrUpdateEntry.php +++ b/src/Builder/InsertOrUpdateEntry.php @@ -10,8 +10,6 @@ */ class InsertOrUpdateEntry implements BuilderInterface { - private RepositoryWriteableInterface $repository; - /** * @var array VALUES clauses for the query */ @@ -27,9 +25,9 @@ class InsertOrUpdateEntry implements BuilderInterface */ private ?array $valuesOnUpdate = null; - public function __construct(RepositoryWriteableInterface $repository) - { - $this->repository = $repository; + public function __construct( + private RepositoryWriteableInterface $repository, + ) { } /** diff --git a/src/Builder/MultiCountEntries.php b/src/Builder/MultiCountEntries.php index a6f2602..7745b04 100644 --- a/src/Builder/MultiCountEntries.php +++ b/src/Builder/MultiCountEntries.php @@ -12,8 +12,6 @@ */ class MultiCountEntries implements BuilderInterface { - private MultiRepositoryReadOnlyInterface $queryHandler; - /** * @var array Repositories used in the multi query */ @@ -34,9 +32,9 @@ class MultiCountEntries implements BuilderInterface */ private bool $blocking = false; - public function __construct(MultiRepositoryReadOnlyInterface $queryHandler) - { - $this->queryHandler = $queryHandler; + public function __construct( + private MultiRepositoryReadOnlyInterface $queryHandler, + ) { } /** diff --git a/src/Builder/MultiSelectEntries.php b/src/Builder/MultiSelectEntries.php index 6900231..e684831 100644 --- a/src/Builder/MultiSelectEntries.php +++ b/src/Builder/MultiSelectEntries.php @@ -17,8 +17,6 @@ class MultiSelectEntries implements BuilderInterface, \IteratorAggregate { use FlattenedFieldsWithTypeTrait; - private MultiRepositoryReadOnlyInterface $queryHandler; - /** * @var array Only retrieve these fields of the repositories */ @@ -64,9 +62,9 @@ class MultiSelectEntries implements BuilderInterface, \IteratorAggregate */ private bool $blocking = false; - public function __construct(MultiRepositoryReadOnlyInterface $queryHandler) - { - $this->queryHandler = $queryHandler; + public function __construct( + private MultiRepositoryReadOnlyInterface $queryHandler, + ) { } public function field(string $getThisField): self diff --git a/src/Builder/MultiSelectEntriesFreeform.php b/src/Builder/MultiSelectEntriesFreeform.php index edc73eb..febfc41 100644 --- a/src/Builder/MultiSelectEntriesFreeform.php +++ b/src/Builder/MultiSelectEntriesFreeform.php @@ -19,8 +19,6 @@ class MultiSelectEntriesFreeform implements BuilderInterface, \IteratorAggregate { use FlattenedFieldsWithTypeTrait; - private MultiRepositoryReadOnlyInterface $queryHandler; - /** * @var array Only retrieve these fields of the repositories */ @@ -46,9 +44,9 @@ class MultiSelectEntriesFreeform implements BuilderInterface, \IteratorAggregate */ private bool $confirmBadPractice = false; - public function __construct(MultiRepositoryReadOnlyInterface $queryHandler) - { - $this->queryHandler = $queryHandler; + public function __construct( + private MultiRepositoryReadOnlyInterface $queryHandler, + ) { } public function field(string $getThisField): self diff --git a/src/Builder/MultiSelectIterator.php b/src/Builder/MultiSelectIterator.php index ef640c0..41fb7b2 100644 --- a/src/Builder/MultiSelectIterator.php +++ b/src/Builder/MultiSelectIterator.php @@ -16,13 +16,13 @@ class MultiSelectIterator implements \Iterator, BuilderInterface { use SelectIteratorTrait; - private MultiRepositoryReadOnlyInterface $source; private ?MultiRepositorySelectQueryInterface $selectReference = null; private ?array $lastResult = null; - public function __construct(MultiRepositoryReadOnlyInterface $repository, array $query) - { - $this->source = $repository; + public function __construct( + private MultiRepositoryReadOnlyInterface $source, + array $query, + ) { $this->query = $query; } diff --git a/src/Builder/MultiUpdateEntriesFreeform.php b/src/Builder/MultiUpdateEntriesFreeform.php index 1c0502a..9f22ee0 100644 --- a/src/Builder/MultiUpdateEntriesFreeform.php +++ b/src/Builder/MultiUpdateEntriesFreeform.php @@ -14,8 +14,6 @@ */ class MultiUpdateEntriesFreeform implements BuilderInterface { - private MultiRepositoryWriteableInterface $queryHandler; - /** * @var array Repositories used in the multi query */ @@ -36,9 +34,9 @@ class MultiUpdateEntriesFreeform implements BuilderInterface */ private bool $confirmBadPractice = false; - public function __construct(MultiRepositoryWriteableInterface $queryHandler) - { - $this->queryHandler = $queryHandler; + public function __construct( + private MultiRepositoryWriteableInterface $queryHandler, + ) { } /** diff --git a/src/Builder/SelectIterator.php b/src/Builder/SelectIterator.php index 93d5486..b782a0c 100644 --- a/src/Builder/SelectIterator.php +++ b/src/Builder/SelectIterator.php @@ -16,13 +16,13 @@ class SelectIterator implements \Iterator, BuilderInterface { use SelectIteratorTrait; - private RepositoryReadOnlyInterface $source; private ?RepositorySelectQueryInterface $selectReference = null; private ?object $lastResult = null; - public function __construct(RepositoryReadOnlyInterface $repository, array $query) - { - $this->source = $repository; + public function __construct( + private RepositoryReadOnlyInterface $source, + array $query, + ) { $this->query = $query; } diff --git a/src/Builder/UpdateEntries.php b/src/Builder/UpdateEntries.php index c10a8a5..c358517 100644 --- a/src/Builder/UpdateEntries.php +++ b/src/Builder/UpdateEntries.php @@ -12,8 +12,6 @@ */ class UpdateEntries implements BuilderInterface { - private RepositoryWriteableInterface $repository; - /** * @var array SET clauses for the query */ @@ -29,9 +27,9 @@ class UpdateEntries implements BuilderInterface */ private bool $confirmNoWhere = false; - public function __construct(RepositoryWriteableInterface $repository) - { - $this->repository = $repository; + public function __construct( + private RepositoryWriteableInterface $repository, + ) { } /** diff --git a/src/Generate/RepositoriesGenerateCommand.php b/src/Generate/RepositoriesGenerateCommand.php index 14312b0..1b46731 100644 --- a/src/Generate/RepositoriesGenerateCommand.php +++ b/src/Generate/RepositoriesGenerateCommand.php @@ -13,7 +13,7 @@ class RepositoriesGenerateCommand private array $repositoryPhpFileBlueprint = [ 'ReadOnly' => <<<'EOD' repository); - } - - public function select(): \{namespaceOfBuilders}\SelectEntries - { - return new \{namespaceOfBuilders}\SelectEntries($this->repository); - } - } -} - namespace {namespaceOfBuilders} { /** * @implements \Iterator @@ -116,6 +94,28 @@ public function getIterator(): SelectIterator } } } + +namespace {namespaceOfEntity} { + use Squirrel\Entities\RepositoryBuilderReadOnlyInterface; + use Squirrel\Entities\RepositoryReadOnlyInterface; + + class {classOfEntity}RepositoryReadOnly implements RepositoryBuilderReadOnlyInterface + { + public function __construct(private RepositoryReadOnlyInterface $repository) + { + } + + public function count(): \Squirrel\Entities\Builder\CountEntries + { + return new \Squirrel\Entities\Builder\CountEntries($this->repository); + } + + public function select(): \{namespaceOfBuilders}\SelectEntries + { + return new \{namespaceOfBuilders}\SelectEntries($this->repository); + } + } +} // @codeCoverageIgnoreEnd EOD diff --git a/src/MultiRepositoryBuilderWriteable.php b/src/MultiRepositoryBuilderWriteable.php index b93eeef..4fb3140 100644 --- a/src/MultiRepositoryBuilderWriteable.php +++ b/src/MultiRepositoryBuilderWriteable.php @@ -16,6 +16,7 @@ public function __construct(?MultiRepositoryWriteableInterface $multiRepositoryW } $this->multiRepositoryWriteable = $multiRepositoryWriteable; + parent::__construct($multiRepositoryWriteable); } diff --git a/src/MultiRepositorySelectQuery.php b/src/MultiRepositorySelectQuery.php index a9730dd..0f868d3 100644 --- a/src/MultiRepositorySelectQuery.php +++ b/src/MultiRepositorySelectQuery.php @@ -6,15 +6,11 @@ class MultiRepositorySelectQuery implements MultiRepositorySelectQueryInterface { - private DBSelectQueryInterface $selectQuery; - private array $types; - private array $typesNullable; - - public function __construct(DBSelectQueryInterface $selectQuery, array $types, array $typesNullable) - { - $this->selectQuery = $selectQuery; - $this->types = $types; - $this->typesNullable = $typesNullable; + public function __construct( + private DBSelectQueryInterface $selectQuery, + private array $types, + private array $typesNullable, + ) { } public function getQuery(): DBSelectQueryInterface diff --git a/src/RepositoryBuilderReadOnlyInterface.php b/src/RepositoryBuilderReadOnlyInterface.php index c216831..56d0777 100644 --- a/src/RepositoryBuilderReadOnlyInterface.php +++ b/src/RepositoryBuilderReadOnlyInterface.php @@ -3,17 +3,11 @@ namespace Squirrel\Entities; use Squirrel\Entities\Builder\CountEntries; +use Squirrel\Entities\Builder\SelectEntries; interface RepositoryBuilderReadOnlyInterface { - /** - * Returns class Squirrel\Entities\Action\SelectEntries as a SELECT query builder, - * but we omit a return docblock to avoid confusion and errors in linting, because when - * repositories are generated we specify the exact return class - * - * @return mixed - */ - public function select(); + public function select(): SelectEntries; public function count(): CountEntries; } diff --git a/src/RepositoryConfig.php b/src/RepositoryConfig.php index b5d924f..f1ce179 100644 --- a/src/RepositoryConfig.php +++ b/src/RepositoryConfig.php @@ -7,39 +7,21 @@ */ class RepositoryConfig implements RepositoryConfigInterface { - private string $connectionName; - private string $tableName = ''; - /** @var string Autoincrement / SERIAL column */ - private string $autoincrementField = ''; - /** Conversion from table to object fields */ - private array $tableToObjectFields = []; - /** Conversion from object to table fields */ - private array $objectToTableFields = []; - /** Object class for conversion of table data to object */ - private string $objectClass = ''; - /** Types of the variables in the object for type casting */ - private array $objectTypes = []; - /** Whether NULL is a valid type for a field */ - private array $objectTypesNullable; - public function __construct( - string $connectionName, - string $tableName, - array $tableToObjectFields, - array $objectToTableFields, - string $objectClass, - array $objectTypes, - array $objectTypesNullable, - string $autoincrementField = '', + private string $connectionName, + private string $tableName, + /** Conversion from table to object fields */ + private array $tableToObjectFields, + /** Conversion from object to table fields */ + private array $objectToTableFields, + /** Object class for conversion of table data to object */ + private string $objectClass, + /** Types of the variables in the object for type casting */ + private array $objectTypes, + /** Whether NULL is a valid type for a field */ + private array $objectTypesNullable, + private string $autoincrementField = '', ) { - $this->connectionName = $connectionName; - $this->tableName = $tableName; - $this->tableToObjectFields = $tableToObjectFields; - $this->objectToTableFields = $objectToTableFields; - $this->objectClass = $objectClass; - $this->objectTypes = $objectTypes; - $this->objectTypesNullable = $objectTypesNullable; - $this->autoincrementField = $autoincrementField; } public function getConnectionName(): string diff --git a/src/RepositoryConfigInterface.php b/src/RepositoryConfigInterface.php index 8781d36..7acc92f 100644 --- a/src/RepositoryConfigInterface.php +++ b/src/RepositoryConfigInterface.php @@ -20,32 +20,32 @@ public function getConnectionName(): string; public function getTableName(): string; /** - * @return array + * @return array Conversion from table to object fields */ public function getTableToObjectFields(): array; /** - * @return array + * @return array Conversion from object to table fields */ public function getObjectToTableFields(): array; /** - * @return string + * @return string Object class for conversion of table data to object */ public function getObjectClass(): string; /** - * @return array + * @return array Types of the variables in the object for type casting */ public function getObjectTypes(): array; /** - * @return array + * @return array Whether NULL is a valid type for a field */ public function getObjectTypesNullable(): array; /** - * @return string + * @return string Autoincrement / SERIAL field if any exists for the table (otherwise an empty string) */ public function getAutoincrementField(): string; } diff --git a/src/RepositoryReadOnly.php b/src/RepositoryReadOnly.php index c94f48d..cd57148 100644 --- a/src/RepositoryReadOnly.php +++ b/src/RepositoryReadOnly.php @@ -14,8 +14,6 @@ */ class RepositoryReadOnly implements RepositoryReadOnlyInterface { - protected DBInterface $db; - protected RepositoryConfigInterface $config; protected array $tableToObjectFields = []; protected array $objectToTableFields = []; protected array $objectTypes = []; @@ -34,10 +32,10 @@ class RepositoryReadOnly implements RepositoryReadOnlyInterface */ protected array $reflectionProperties = []; - public function __construct(DBInterface $db, RepositoryConfigInterface $config) - { - $this->db = $db; - $this->config = $config; + public function __construct( + protected DBInterface $db, + protected RepositoryConfigInterface $config, + ) { $this->tableToObjectFields = $config->getTableToObjectFields(); $this->objectToTableFields = $config->getObjectToTableFields(); $this->objectTypes = $config->getObjectTypes(); diff --git a/src/RepositorySelectQuery.php b/src/RepositorySelectQuery.php index 89d8713..4ad9a43 100644 --- a/src/RepositorySelectQuery.php +++ b/src/RepositorySelectQuery.php @@ -6,13 +6,10 @@ class RepositorySelectQuery implements RepositorySelectQueryInterface { - private DBSelectQueryInterface $selectQuery; - private RepositoryConfigInterface $repositoryConfig; - - public function __construct(DBSelectQueryInterface $selectQuery, RepositoryConfigInterface $repositoryConfig) - { - $this->selectQuery = $selectQuery; - $this->repositoryConfig = $repositoryConfig; + public function __construct( + private DBSelectQueryInterface $selectQuery, + private RepositoryConfigInterface $repositoryConfig, + ) { } public function getQuery(): DBSelectQueryInterface diff --git a/src/Transaction.php b/src/Transaction.php index 8e12ee8..f991fe0 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -12,11 +12,9 @@ */ class Transaction implements TransactionInterface { - private DBInterface $db; - - public function __construct(DBInterface $db) - { - $this->db = $db; + public function __construct( + private DBInterface $db, + ) { } /**