From 19a779175c6e690bb609b07fa88d28bcaac3b3e2 Mon Sep 17 00:00:00 2001 From: Dave Redfern Date: Thu, 21 Jan 2021 15:22:58 -0500 Subject: [PATCH] Update for PHP 8 support --- .github/workflows/tests.yml | 2 -- CHANGELOG.md | 8 ++++++- README.md | 3 ++- composer.json | 12 +++++----- docs/upgrading_2.X_to_3.0.md | 10 +++++++++ src/ConnectionManager.php | 2 +- src/Model.php | 8 +++---- src/ModelBuilder.php | 22 +++++++++---------- src/ModelExporter.php | 6 ++--- src/ModelIdentityMap.php | 2 +- src/ModelMetadata.php | 2 +- src/Relationships/AbstractRelationship.php | 4 ++-- src/Relationships/BelongsTo.php | 2 +- src/Relationships/BelongsToMany.php | 2 +- src/Relationships/HasOne.php | 2 +- src/Relationships/HasOneOrMany.php | 2 +- src/Relationships/HasOneToMany.php | 2 +- src/Utils/ClassHelpers.php | 4 ++-- .../FilterGeneratedKeysFromCollection.php | 2 +- tests/ModelBuilderTest.php | 4 ++-- tests/ModelTest.php | 4 ++-- tests/Relationships/BelongsToManyTest.php | 2 +- tests/Relationships/HasManyTest.php | 3 +-- tests/Stubs/Casters/ContactCaster.php | 4 ++-- tests/Stubs/Models/Contact.php | 4 ++-- tests/Stubs/Models/Role.php | 2 +- tests/resources/bootstrap.php | 2 +- 27 files changed, 68 insertions(+), 54 deletions(-) create mode 100644 docs/upgrading_2.X_to_3.0.md diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 52ecb07..3f12673 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,8 +16,6 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 - with: - php-version: '7.4' - name: Validate composer.json and composer.lock run: composer validate diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eaaf9f..7307d04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,15 @@ Change Log ========== -2020-12-18 +2021-01-21 ---------- + * require PHP 8 + * update to collection 5.0, domain 4.0 + +2020-12-18 - 2.2.0 +------------------ + * add bound parameter count check on relationships to prevent running queries without bound args 2020-10-22 diff --git a/README.md b/README.md index 30bd494..55a47f9 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ active-record projects including GranadaORM (IdiORM), PHP ActiveRecord and other ## Requirements - * PHP 7.4+ + * PHP 8.0+ * mb_string * doctrine/dbal * somnambulist/collection @@ -192,6 +192,7 @@ somnambulist/collection project and must have `extract()` and `add()` methods. ## More Reading * [Upgrading from 1.X to 2.0](docs/upgrading_1.X_to_2.0.md) + * [Upgrading from 2.X to 3.0](docs/upgrading_2.X_to_3.0.md) * [Setting up Symfony](docs/setup_symfony.md) * [Querying Data](docs/querying.md) * [Casting Attributes](docs/casting.md) diff --git a/composer.json b/composer.json index 603b13c..7af0d5a 100644 --- a/composer.json +++ b/composer.json @@ -10,21 +10,21 @@ } ], "require": { - "php": ">=7.4", + "php": ">=8.0", "ext-json": "*", "doctrine/dbal": "~2.11", "pagerfanta/pagerfanta": "~2.4", "pragmarx/ia-str": "^7.0", - "somnambulist/attribute-model": "~1.0", - "somnambulist/collection": "~4.0" + "somnambulist/attribute-model": "~2.0", + "somnambulist/collection": "~5.0" }, "require-dev": { - "phpunit/phpunit": "~9", + "phpunit/phpunit": "~9.5", "creof/doctrine2-spatial": "~1.2", "doctrine/doctrine-bundle": "~2.0", "doctrine/orm": "^2.7", - "fzaninotto/faker": "~1.8", - "somnambulist/domain": "~3.0", + "fakerphp/faker": "^1.13", + "somnambulist/domain": "~4.0", "symfony/dotenv": "^5.0", "symfony/framework-bundle": "^5.0", "symfony/messenger": "^5.0", diff --git a/docs/upgrading_2.X_to_3.0.md b/docs/upgrading_2.X_to_3.0.md new file mode 100644 index 0000000..5b86105 --- /dev/null +++ b/docs/upgrading_2.X_to_3.0.md @@ -0,0 +1,10 @@ + +## Upgrading from 2.X to 3.0 + +The biggest change in 3.0 is the requirement for PHP 8. There are some minor type-hint related +changes, however the main APIs are the same as 2.X. + +In addition note the following: + + * `somnambulist/domain` has been updated and is now namespaced `Somnambulist\Components\Domain` + * `somnambulist/collection` has been updated and is now namespaced `Somnambulist\Components\Collection` diff --git a/src/ConnectionManager.php b/src/ConnectionManager.php index 396650d..2081c00 100644 --- a/src/ConnectionManager.php +++ b/src/ConnectionManager.php @@ -22,7 +22,7 @@ public function __construct(array $connections) $this->forAll($connections); } - public function forAll(array $connections) + public function forAll(array $connections): void { foreach ($connections as $model => $connection) { $this->add($connection, $model); diff --git a/src/Model.php b/src/Model.php index a60f938..a8dc488 100644 --- a/src/Model.php +++ b/src/Model.php @@ -5,11 +5,11 @@ use IlluminateAgnostic\Str\Support\Str; use JsonSerializable; use LogicException; -use Somnambulist\Collection\Contracts\Arrayable; -use Somnambulist\Collection\Contracts\Collection; -use Somnambulist\Collection\Contracts\Jsonable; -use Somnambulist\Collection\MutableCollection; use Somnambulist\Components\AttributeModel\AbstractModel; +use Somnambulist\Components\Collection\Contracts\Arrayable; +use Somnambulist\Components\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\Contracts\Jsonable; +use Somnambulist\Components\Collection\MutableCollection; use Somnambulist\Components\ReadModels\Exceptions\EntityNotFoundException; use Somnambulist\Components\ReadModels\Relationships\AbstractRelationship; use Somnambulist\Components\ReadModels\Relationships\BelongsTo; diff --git a/src/ModelBuilder.php b/src/ModelBuilder.php index e46a3dd..7104bef 100644 --- a/src/ModelBuilder.php +++ b/src/ModelBuilder.php @@ -9,9 +9,9 @@ use InvalidArgumentException; use Pagerfanta\Pagerfanta; use RuntimeException; -use Somnambulist\Collection\Contracts\Arrayable; -use Somnambulist\Collection\Contracts\Collection; -use Somnambulist\Collection\MutableCollection; +use Somnambulist\Components\Collection\Contracts\Arrayable; +use Somnambulist\Components\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\MutableCollection; use Somnambulist\Components\ReadModels\Contracts\Queryable; use Somnambulist\Components\ReadModels\Exceptions\EntityNotFoundException; use Somnambulist\Components\ReadModels\Exceptions\NoResultsException; @@ -28,9 +28,9 @@ use function is_callable; use function method_exists; use function sprintf; +use function str_contains; use function str_replace; use function strlen; -use function strpos; use function substr; use function ucfirst; @@ -277,7 +277,7 @@ public function with(...$relations): self private function eagerLoadRelationships(Collection $models): void { foreach ($this->eagerLoad as $name => $constraints) { - if (false === strpos($name, '.')) { + if (false === str_contains($name, '.')) { /** @var AbstractRelationship $load */ $rel = $this->model->new()->getRelationship($name); $rel @@ -546,7 +546,7 @@ public function orWhereNotIn(string $column, $values): self * * @return ModelBuilder */ - public function whereColumn(string $column, string $operator, $value, string $andOr = 'and'): self + public function whereColumn(string $column, string $operator, mixed $value, string $andOr = 'and'): self { $key = $this->createParameterPlaceholderKey($column); $method = $this->getAndOrWhereMethodName($andOr); @@ -568,7 +568,7 @@ public function whereColumn(string $column, string $operator, $value, string $an * * @return ModelBuilder */ - public function orWhereColumn(string $column, string $operator, $value): self + public function orWhereColumn(string $column, string $operator, mixed $value): self { return $this->whereColumn($column, $operator, $value, 'or'); } @@ -621,7 +621,7 @@ public function orWhereNotNull(string $column): self * * @return ModelBuilder */ - public function whereBetween(string $column, $start, $end, string $andOr = 'and', bool $not = false): self + public function whereBetween(string $column, mixed $start, mixed $end, string $andOr = 'and', bool $not = false): self { $method = $this->getAndOrWhereMethodName($andOr); $expr = ($not ? 'NOT' : '') . ' BETWEEN'; @@ -635,17 +635,17 @@ public function whereBetween(string $column, $start, $end, string $andOr = 'and' return $this; } - public function whereNotBetween(string $column, $start, $end): self + public function whereNotBetween(string $column, mixed $start, mixed $end): self { return $this->whereBetween($column, $start, $end, 'and', true); } - public function orWhereBetween(string $column, $start, $end): self + public function orWhereBetween(string $column, mixed $start, mixed $end): self { return $this->whereBetween($column, $start, $end, 'or'); } - public function orWhereNotBetween(string $column, $start, $end): self + public function orWhereNotBetween(string $column, mixed $start, mixed $end): self { return $this->whereBetween($column, $start, $end, 'or', true); } diff --git a/src/ModelExporter.php b/src/ModelExporter.php index 643e0e7..425f800 100644 --- a/src/ModelExporter.php +++ b/src/ModelExporter.php @@ -4,8 +4,8 @@ use Closure; use IlluminateAgnostic\Str\Support\Str; -use Somnambulist\Collection\Contracts\Jsonable; -use Somnambulist\Collection\MutableCollection as Collection; +use Somnambulist\Components\Collection\Contracts\Jsonable; +use Somnambulist\Components\Collection\MutableCollection as Collection; use Somnambulist\Components\ReadModels\Exceptions\JsonEncodingException; use function count; use function explode; @@ -130,7 +130,7 @@ private function extractAttributes(array $attributes): array foreach ($attributes as $key => $value) { if ($this->shouldExtractAttribute($key)) { - $key = Str::snake($this->getAttributeExtractionKey($key), '_'); + $key = Str::snake($this->getAttributeExtractionKey($key)); if (is_object($value)) { $attrs[$key] = $this->extractPropertiesFrom($value); diff --git a/src/ModelIdentityMap.php b/src/ModelIdentityMap.php index 0be3757..9d74f04 100644 --- a/src/ModelIdentityMap.php +++ b/src/ModelIdentityMap.php @@ -68,7 +68,7 @@ public function registerAlias(Model $model, ?string $foreignKeyName = null): voi * @param string $target * @param mixed $targetId */ - public function registerRelationship(string $source, $sourceId, string $target, $targetId): void + public function registerRelationship(string $source, mixed $sourceId, string $target, mixed $targetId): void { $this->relationships[$source][(string)$sourceId][$target][(string)$targetId] = (string)$targetId; } diff --git a/src/ModelMetadata.php b/src/ModelMetadata.php index 0016ca1..0ccee2c 100644 --- a/src/ModelMetadata.php +++ b/src/ModelMetadata.php @@ -91,7 +91,7 @@ public function externalKeyName(): ?string */ public function foreignKey(): string { - $key = sprintf('%s_%s', Str::snake(ClassHelpers::getObjectShortClassName($this->model), '_'), $this->primaryKeyName()); + $key = sprintf('%s_%s', Str::snake(ClassHelpers::getObjectShortClassName($this->model)), $this->primaryKeyName()); return $this->foreignKey ?? $key; } diff --git a/src/Relationships/AbstractRelationship.php b/src/Relationships/AbstractRelationship.php index 7db760e..299b47f 100644 --- a/src/Relationships/AbstractRelationship.php +++ b/src/Relationships/AbstractRelationship.php @@ -5,8 +5,8 @@ use Doctrine\Common\Collections\ExpressionBuilder; use Doctrine\DBAL\Query\QueryBuilder; use Pagerfanta\Pagerfanta; -use Somnambulist\Collection\Contracts\Collection; -use Somnambulist\Collection\MutableCollection; +use Somnambulist\Components\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\MutableCollection; use Somnambulist\Components\ReadModels\Contracts\Queryable; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\ModelBuilder; diff --git a/src/Relationships/BelongsTo.php b/src/Relationships/BelongsTo.php index 1eb79b1..ede1c21 100644 --- a/src/Relationships/BelongsTo.php +++ b/src/Relationships/BelongsTo.php @@ -2,7 +2,7 @@ namespace Somnambulist\Components\ReadModels\Relationships; -use Somnambulist\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\Contracts\Collection; use Somnambulist\Components\ReadModels\Manager; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\ModelBuilder; diff --git a/src/Relationships/BelongsToMany.php b/src/Relationships/BelongsToMany.php index 1afe5ac..affce98 100644 --- a/src/Relationships/BelongsToMany.php +++ b/src/Relationships/BelongsToMany.php @@ -2,7 +2,7 @@ namespace Somnambulist\Components\ReadModels\Relationships; -use Somnambulist\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\Contracts\Collection; use Somnambulist\Components\ReadModels\Manager; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\ModelBuilder; diff --git a/src/Relationships/HasOne.php b/src/Relationships/HasOne.php index 6b12a5b..6399550 100644 --- a/src/Relationships/HasOne.php +++ b/src/Relationships/HasOne.php @@ -2,7 +2,7 @@ namespace Somnambulist\Components\ReadModels\Relationships; -use Somnambulist\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\Contracts\Collection; use Somnambulist\Components\ReadModels\Manager; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\ModelBuilder; diff --git a/src/Relationships/HasOneOrMany.php b/src/Relationships/HasOneOrMany.php index 888ed99..f6a6819 100644 --- a/src/Relationships/HasOneOrMany.php +++ b/src/Relationships/HasOneOrMany.php @@ -2,7 +2,7 @@ namespace Somnambulist\Components\ReadModels\Relationships; -use Somnambulist\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\Contracts\Collection; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\ModelBuilder; diff --git a/src/Relationships/HasOneToMany.php b/src/Relationships/HasOneToMany.php index fb3e231..830e5a8 100644 --- a/src/Relationships/HasOneToMany.php +++ b/src/Relationships/HasOneToMany.php @@ -2,7 +2,7 @@ namespace Somnambulist\Components\ReadModels\Relationships; -use Somnambulist\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\Contracts\Collection; use Somnambulist\Components\ReadModels\Manager; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\ModelBuilder; diff --git a/src/Utils/ClassHelpers.php b/src/Utils/ClassHelpers.php index faf8395..b6548f8 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, $value, $scope = null) + public static function set(object $object, string $property, mixed $value, mixed $scope = null) { Closure::bind(function () use ($property, $value) { $this->{$property} = $value; @@ -106,7 +106,7 @@ public static function set(object $object, string $property, $value, $scope = nu * * @return object */ - public static function setPropertyArrayKey(object $object, string $property, string $key, $value, $scope = null) + public static function setPropertyArrayKey(object $object, string $property, string $key, mixed $value, mixed $scope = null) { Closure::bind(function () use ($property, $key, $value) { $this->{$property}[$key] = $value; diff --git a/src/Utils/FilterGeneratedKeysFromCollection.php b/src/Utils/FilterGeneratedKeysFromCollection.php index 17eb438..b9727cf 100644 --- a/src/Utils/FilterGeneratedKeysFromCollection.php +++ b/src/Utils/FilterGeneratedKeysFromCollection.php @@ -3,7 +3,7 @@ namespace Somnambulist\Components\ReadModels\Utils; use IlluminateAgnostic\Str\Support\Str; -use Somnambulist\Collection\MutableCollection as Collection; +use Somnambulist\Components\Collection\MutableCollection as Collection; use Somnambulist\Components\ReadModels\Relationships\AbstractRelationship; use function is_string; diff --git a/tests/ModelBuilderTest.php b/tests/ModelBuilderTest.php index ed56b70..0c2b2a2 100644 --- a/tests/ModelBuilderTest.php +++ b/tests/ModelBuilderTest.php @@ -4,7 +4,7 @@ use Pagerfanta\Pagerfanta; use PHPUnit\Framework\TestCase; -use Somnambulist\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\Contracts\Collection; use Somnambulist\Components\ReadModels\Exceptions\EntityNotFoundException; use Somnambulist\Components\ReadModels\Exceptions\NoResultsException; use Somnambulist\Components\ReadModels\Model; @@ -14,7 +14,7 @@ use Somnambulist\Components\ReadModels\Tests\Stubs\Models\UserAddress; use Somnambulist\Components\ReadModels\Tests\Stubs\Models\UserContact; use Somnambulist\Components\ReadModels\Tests\Support\Behaviours\GetRandomUserId; -use Somnambulist\Domain\Entities\Types\DateTime\DateTime; +use Somnambulist\Components\Domain\Entities\Types\DateTime\DateTime; /** * Class ModelBuilderTest diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 2fb9495..7b6336b 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -4,12 +4,12 @@ use BadMethodCallException; use PHPUnit\Framework\TestCase; -use Somnambulist\Collection\MutableCollection; +use Somnambulist\Components\Collection\MutableCollection; use Somnambulist\Components\ReadModels\ModelBuilder; use Somnambulist\Components\ReadModels\ModelExporter; use Somnambulist\Components\ReadModels\Relationships\HasOneToMany; use Somnambulist\Components\ReadModels\Tests\Stubs\Models\User; -use Somnambulist\Domain\Entities\Types\DateTime\DateTime; +use Somnambulist\Components\Domain\Entities\Types\DateTime\DateTime; use function date; use function password_hash; diff --git a/tests/Relationships/BelongsToManyTest.php b/tests/Relationships/BelongsToManyTest.php index a6830a3..0c3d643 100644 --- a/tests/Relationships/BelongsToManyTest.php +++ b/tests/Relationships/BelongsToManyTest.php @@ -4,7 +4,7 @@ use Doctrine\DBAL\Query\QueryBuilder; use PHPUnit\Framework\TestCase; -use Somnambulist\Collection\Contracts\Collection; +use Somnambulist\Components\Collection\Contracts\Collection; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\ModelBuilder; use Somnambulist\Components\ReadModels\Relationships\BelongsToMany; diff --git a/tests/Relationships/HasManyTest.php b/tests/Relationships/HasManyTest.php index fdda6e8..bb14d81 100644 --- a/tests/Relationships/HasManyTest.php +++ b/tests/Relationships/HasManyTest.php @@ -4,11 +4,10 @@ use Doctrine\DBAL\Query\QueryBuilder; use PHPUnit\Framework\TestCase; -use Somnambulist\Collection\MutableCollection; +use Somnambulist\Components\Collection\MutableCollection; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\ModelBuilder; use Somnambulist\Components\ReadModels\Relationships\HasOneToMany; -use Somnambulist\Components\ReadModels\Tests\Stubs\Models\Address; use Somnambulist\Components\ReadModels\Tests\Stubs\Models\User; use Somnambulist\Components\ReadModels\Tests\Stubs\Models\UserAddress; use Somnambulist\Components\ReadModels\Tests\Support\Behaviours\GetRandomUserIdWithRelationship; diff --git a/tests/Stubs/Casters/ContactCaster.php b/tests/Stubs/Casters/ContactCaster.php index ea0d210..b893c3c 100644 --- a/tests/Stubs/Casters/ContactCaster.php +++ b/tests/Stubs/Casters/ContactCaster.php @@ -4,8 +4,8 @@ use Somnambulist\Components\AttributeModel\Contracts\AttributeCasterInterface; use Somnambulist\Components\ReadModels\Tests\Stubs\Models\Contact; -use Somnambulist\Domain\Entities\Types\Identity\EmailAddress; -use Somnambulist\Domain\Entities\Types\PhoneNumber; +use Somnambulist\Components\Domain\Entities\Types\Identity\EmailAddress; +use Somnambulist\Components\Domain\Entities\Types\PhoneNumber; /** * Class ContactCaster diff --git a/tests/Stubs/Models/Contact.php b/tests/Stubs/Models/Contact.php index 550ba19..5c630ad 100644 --- a/tests/Stubs/Models/Contact.php +++ b/tests/Stubs/Models/Contact.php @@ -2,8 +2,8 @@ namespace Somnambulist\Components\ReadModels\Tests\Stubs\Models; -use Somnambulist\Domain\Entities\Types\Identity\EmailAddress; -use Somnambulist\Domain\Entities\Types\PhoneNumber; +use Somnambulist\Components\Domain\Entities\Types\Identity\EmailAddress; +use Somnambulist\Components\Domain\Entities\Types\PhoneNumber; /** * Class Contact diff --git a/tests/Stubs/Models/Role.php b/tests/Stubs/Models/Role.php index 3970a8f..e44ad99 100644 --- a/tests/Stubs/Models/Role.php +++ b/tests/Stubs/Models/Role.php @@ -2,7 +2,7 @@ namespace Somnambulist\Components\ReadModels\Tests\Stubs\Models; -use Somnambulist\Collection\MutableCollection; +use Somnambulist\Components\Collection\MutableCollection; use Somnambulist\Components\ReadModels\Model; use Somnambulist\Components\ReadModels\Relationships\BelongsToMany; diff --git a/tests/resources/bootstrap.php b/tests/resources/bootstrap.php index 52d880f..7a12aa3 100644 --- a/tests/resources/bootstrap.php +++ b/tests/resources/bootstrap.php @@ -7,7 +7,7 @@ use Somnambulist\Components\ReadModels\Tests\Stubs\Casters\ContactCaster; use Somnambulist\Components\ReadModels\Tests\Stubs\DataGenerator; use Somnambulist\Components\ReadModels\TypeCasters\DoctrineTypeCaster; -use Somnambulist\Domain\Doctrine\TypeBootstrapper; +use Somnambulist\Components\Domain\Doctrine\TypeBootstrapper; use Symfony\Component\Dotenv\Dotenv; (new Dotenv)->loadEnv(dirname(__DIR__, 2) . '/.env');