Skip to content

Commit

Permalink
Refactor for PHP8+
Browse files Browse the repository at this point in the history
- Use constructor property promotion whenever possible
- Add some missing type hints that are now supported
- Amend some documentation
- Fix order of declared namespaces in generated repositories
  • Loading branch information
iquito committed Dec 15, 2021
1 parent cbffb53 commit aca61aa
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 186 deletions.
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.7.3@38c452ae584467e939d55377aaf83b5a26f19dd1">
<files psalm-version="v4.15.0@a1b5e489e6fcebe40cb804793d964e99fc347820">
<file src="src/MultiRepositoryReadOnly.php">
<MissingConstructor occurrences="2">
<code>$db</code>
Expand Down
21 changes: 6 additions & 15 deletions src/Attribute/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Attribute/EntityProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
29 changes: 8 additions & 21 deletions src/Attribute/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
8 changes: 3 additions & 5 deletions src/Builder/CountEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
class CountEntries implements BuilderInterface
{
private RepositoryReadOnlyInterface $repository;

/**
* @var array<int|string,mixed> WHERE restrictions in query
*/
Expand All @@ -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,
) {
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Builder/DeleteEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
class DeleteEntries implements BuilderInterface
{
private RepositoryWriteableInterface $repository;

/**
* @var array<int|string,mixed> WHERE restrictions in query
*/
Expand All @@ -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,
) {
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Builder/InsertEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
*/
class InsertEntry implements BuilderInterface
{
private RepositoryWriteableInterface $repository;

/**
* @var array<string,mixed> VALUES clauses for the query
*/
private array $values = [];

public function __construct(RepositoryWriteableInterface $repository)
{
$this->repository = $repository;
public function __construct(
private RepositoryWriteableInterface $repository,
) {
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Builder/InsertOrUpdateEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
class InsertOrUpdateEntry implements BuilderInterface
{
private RepositoryWriteableInterface $repository;

/**
* @var array<string,mixed> VALUES clauses for the query
*/
Expand All @@ -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,
) {
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Builder/MultiCountEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
class MultiCountEntries implements BuilderInterface
{
private MultiRepositoryReadOnlyInterface $queryHandler;

/**
* @var array<string,RepositoryBuilderReadOnlyInterface|RepositoryReadOnlyInterface> Repositories used in the multi query
*/
Expand All @@ -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,
) {
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Builder/MultiSelectEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class MultiSelectEntries implements BuilderInterface, \IteratorAggregate
{
use FlattenedFieldsWithTypeTrait;

private MultiRepositoryReadOnlyInterface $queryHandler;

/**
* @var array<int|string,string> Only retrieve these fields of the repositories
*/
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/Builder/MultiSelectEntriesFreeform.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class MultiSelectEntriesFreeform implements BuilderInterface, \IteratorAggregate
{
use FlattenedFieldsWithTypeTrait;

private MultiRepositoryReadOnlyInterface $queryHandler;

/**
* @var array<int|string,string> Only retrieve these fields of the repositories
*/
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Builder/MultiSelectIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 3 additions & 5 deletions src/Builder/MultiUpdateEntriesFreeform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
class MultiUpdateEntriesFreeform implements BuilderInterface
{
private MultiRepositoryWriteableInterface $queryHandler;

/**
* @var array<string,RepositoryBuilderWriteableInterface|RepositoryWriteableInterface> Repositories used in the multi query
*/
Expand All @@ -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,
) {
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Builder/SelectIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 3 additions & 5 deletions src/Builder/UpdateEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
class UpdateEntries implements BuilderInterface
{
private RepositoryWriteableInterface $repository;

/**
* @var array<int|string,mixed> SET clauses for the query
*/
Expand All @@ -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,
) {
}

/**
Expand Down
46 changes: 23 additions & 23 deletions src/Generate/RepositoriesGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RepositoriesGenerateCommand
private array $repositoryPhpFileBlueprint = [
'ReadOnly' => <<<'EOD'
<?php
// phpcs:ignoreFile -- created by SquirrelPHP library, do not alter
// phpcs:ignoreFile -- created by SquirrelPHP entities library, do not alter
/*
* THIS FILE IS AUTOMATICALLY CREATED - DO NOT EDIT, DO NOT COMMIT TO VCS
*
Expand All @@ -26,28 +26,6 @@ class RepositoriesGenerateCommand
*/
// @codeCoverageIgnoreStart
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);
}
}
}
namespace {namespaceOfBuilders} {
/**
* @implements \Iterator<int,\{namespaceOfEntity}\{classOfEntity}>
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/MultiRepositoryBuilderWriteable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(?MultiRepositoryWriteableInterface $multiRepositoryW
}

$this->multiRepositoryWriteable = $multiRepositoryWriteable;

parent::__construct($multiRepositoryWriteable);
}

Expand Down
Loading

0 comments on commit aca61aa

Please sign in to comment.