Skip to content

Commit

Permalink
Minor cleanup and bug fixes
Browse files Browse the repository at this point in the history
Deprecated passing array as arg to with
Add more PHP8 code features
  • Loading branch information
dave-redfern committed Oct 27, 2021
1 parent beefb53 commit d19cf70
Show file tree
Hide file tree
Showing 29 changed files with 62 additions and 77 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
----------

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
final class ConnectionManager
{

private array $connections = [];

public function __construct(array $connections)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 1 addition & 5 deletions src/EventSubscribers/IdentityMapClearerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion src/Exceptions/ConnectionManagerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 1 addition & 2 deletions src/Exceptions/EntityNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 0 additions & 1 deletion src/Exceptions/JsonEncodingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
class JsonEncodingException extends RuntimeException
{

public static function failedToConvertModel(Model $model, string $error): JsonEncodingException
{
return new self(
Expand Down
1 change: 0 additions & 1 deletion src/Exceptions/NoResultsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class NoResultsException extends Exception
{

private QueryBuilder $query;

public function __construct(string $class, QueryBuilder $queryBuilder)
Expand Down
1 change: 0 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
final class Manager
{

private static ?Manager $instance = null;
private ConnectionManager $connections;
private AttributeCaster $caster;
Expand Down
1 change: 0 additions & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/
abstract class Model extends AbstractModel implements Arrayable, Jsonable, JsonSerializable
{

/**
* The table associated with the model
*/
Expand Down
9 changes: 7 additions & 2 deletions src/ModelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
*/
class ModelBuilder implements Queryable
{

private Model $model;
private ModelMetadata $meta;
private QueryBuilder $query;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/ModelExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
final class ModelExporter implements Jsonable
{

private Model $model;
private array $attributes;
private array $relationships;
Expand Down
1 change: 0 additions & 1 deletion src/ModelIdentityMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
final class ModelIdentityMap
{

private array $identityMap = [];
private array $aliases = [];
private array $relationships = [];
Expand Down
1 change: 0 additions & 1 deletion src/ModelMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
final class ModelMetadata
{

private Model $model;
private string $table;
private string $primaryKey;
Expand Down
1 change: 0 additions & 1 deletion src/PHPUnit/PHPUnitListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*/
class PHPUnitListener implements BeforeTestHook, AfterTestHook
{

public function executeAfterTest(string $test, float $time): void
{
Manager::clear();
Expand Down
1 change: 0 additions & 1 deletion src/PaginatorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
final class PaginatorAdapter implements AdapterInterface
{

private ModelBuilder $builder;

public function __construct(ModelBuilder $queryBuilder)
Expand Down
1 change: 0 additions & 1 deletion src/Relationships/AbstractRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
*/
abstract class AbstractRelationship implements Queryable
{

/**
* A prefix used to flag attributes generated by ReadModels
*
Expand Down
1 change: 0 additions & 1 deletion src/Relationships/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
class BelongsTo extends AbstractRelationship
{

protected string $foreignKey;
protected string $ownerKey;
protected bool $nullOnNotFound;
Expand Down
1 change: 0 additions & 1 deletion src/Relationships/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class BelongsToMany extends AbstractRelationship
{

private string $joinTable;
private string $joinTableSourceKey;
private string $joinTableTargetKey;
Expand Down
1 change: 0 additions & 1 deletion src/Relationships/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/Relationships/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
abstract class HasOneOrMany extends AbstractRelationship
{

protected string $foreignKey;
protected string $localKey;

Expand Down
1 change: 0 additions & 1 deletion src/Relationships/HasOneToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions src/TypeCasters/DoctrineTypeCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/ClassHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/Utils/FilterGeneratedKeysFromCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
final class FilterGeneratedKeysFromCollection
{

/**
* Filters out library generated keys from the set of attributes
*
Expand Down
7 changes: 4 additions & 3 deletions src/Utils/GenerateRelationshipsToEagerLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function implode;
use function is_array;
use function is_numeric;
use function trigger_deprecation;

/**
* Class GenerateRelationshipsToEagerLoad
Expand All @@ -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];
}

Expand Down
1 change: 0 additions & 1 deletion src/Utils/ProxyTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
class ProxyTo
{

/**
* Proxy a call into the specified object, collecting error information if it fails
*
Expand Down
Loading

0 comments on commit d19cf70

Please sign in to comment.