Skip to content

Commit

Permalink
Fix PHP 8.4 deprecations
Browse files Browse the repository at this point in the history
Replace pragmarx/ia-str with symfony/string
Remove psalm from dev dependencies
  • Loading branch information
dave-redfern committed Nov 28, 2024
1 parent 6354c36 commit 1763481
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.1', '8.2', '8.3', '8.4']

steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ composer.lock

tests/output
.idea
.phpunit.cache
.phpunit.result.cache
.env*
phpunit.xml
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Change Log
==========

2024-11-28 - 4.2.0
------------------

* fix PHP 8.4 deprecations
* replace pragmarx/ia-str with symfony/string
* removed psalm

2024-03-02 - 4.1.0
------------------

Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext-json": "*",
"doctrine/dbal": "^4.0",
"pagerfanta/pagerfanta": "^4.3",
"pragmarx/ia-str": "^7.3",
"symfony/string": "^6.4|^7.0",
"somnambulist/attribute-model": "^3.0",
"somnambulist/collection": "^5.3"
},
Expand All @@ -32,8 +32,7 @@
"symfony/stopwatch": "^6.4",
"symfony/var-dumper": "^6.4",
"victorjonsson/markdowndocs": "dev-master",
"symfony/yaml": "^6.4",
"vimeo/psalm": "^5.4"
"symfony/yaml": "^6.4"
},
"autoload": {
"psr-4": {
Expand Down
54 changes: 0 additions & 54 deletions psalm.xml

This file was deleted.

4 changes: 2 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Somnambulist\Components\ReadModels;

use IlluminateAgnostic\Str\Support\Str;
use JsonSerializable;
use LogicException;
use Somnambulist\Components\AttributeModel\AbstractModel;
Expand All @@ -22,6 +21,7 @@
use function is_null;
use function method_exists;
use function sprintf;
use function Symfony\Component\String\u;

abstract class Model extends AbstractModel implements Arrayable, Jsonable, JsonSerializable
{
Expand Down Expand Up @@ -438,7 +438,7 @@ protected function belongsTo(
/** @var Model $instance */
$instance = new $class();
$relation = $relation ?: ClassHelpers::getCallingMethod();
$foreignKey = $foreignKey ?: sprintf('%s_%s', Str::snake($relation), $instance->meta()->primaryKeyName());
$foreignKey = $foreignKey ?: sprintf('%s_%s', u($relation)->snake()->toString(), $instance->meta()->primaryKeyName());
$ownerKey = $ownerKey ?: $instance->meta()->primaryKeyName();

return new BelongsTo($instance->newQuery(), $this, $foreignKey, $ownerKey, $nullOnNotFound);
Expand Down
18 changes: 9 additions & 9 deletions src/ModelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
use IlluminateAgnostic\Str\Support\Str;
use InvalidArgumentException;
use Pagerfanta\Pagerfanta;
use RuntimeException;
Expand All @@ -31,10 +30,9 @@
use function method_exists;
use function sprintf;
use function str_contains;
use function str_replace;
use function str_starts_with;
use function strlen;
use function substr;
use function Symfony\Component\String\u;
use function ucfirst;

/**
Expand Down Expand Up @@ -108,7 +106,7 @@ public function find($id, ...$columns): ?Model
*
* @return Collection
*/
public function findBy(array $criteria, array $orderBy = [], int $limit = null, int $offset = null): Collection
public function findBy(array $criteria, array $orderBy = [], ?int $limit = null, ?int $offset = null): Collection
{
foreach ($criteria as $field => $value) {
$this->whereColumn($field, '=', $value);
Expand Down Expand Up @@ -219,7 +217,7 @@ public function count(): int

foreach ($groupBy as $item) {
foreach ($selects as $select) {
if (Str::contains($select, $item)) {
if (u($select)->containsAny($item)) {
$new[] = $select;
}
}
Expand Down Expand Up @@ -301,8 +299,10 @@ private function findNestedRelationshipsFor(string $relation): array
// the given top-level relationship. We will just check for any relations
// that start with the given top relations and add them to our arrays.
foreach ($this->eagerLoad as $name => $constraints) {
if (Str::contains($name, '.') && Str::startsWith($name, $relation . '.')) {
$nested[substr($name, strlen($relation . '.'))] = $constraints;
$name = u($name);

if ($name->containsAny('.') && $name->startsWith($relation . '.')) {
$nested[$name->after($relation . '.')->toString()] = $constraints;
}
}

Expand Down Expand Up @@ -386,7 +386,7 @@ public function select(...$columns): ModelBuilder
public function hasSelectExpression(string $expression): bool
{
foreach (ClassHelpers::get($this->query, 'select') as $select) {
if (Str::contains($select, $expression)) {
if (u($select)->containsAny($expression)) {
return true;
}
}
Expand All @@ -412,7 +412,7 @@ private function createParameterPlaceholderKey(string $column): string
// placeholder name can only be ascii with underscores, hyphens and dots are not allowed
return sprintf(
':bind_%s_%s',
Str::slug(str_replace(['.', '-'], '_', $this->prefixColumnWithTableAlias($column)), '_'),
u($this->prefixColumnWithTableAlias($column))->snake(),
++$index
);
}
Expand Down
10 changes: 6 additions & 4 deletions src/ModelExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Somnambulist\Components\ReadModels;

use Closure;
use IlluminateAgnostic\Str\Support\Str;
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;
use function Symfony\Component\String\u;

final class ModelExporter implements Jsonable
{
Expand Down Expand Up @@ -70,11 +70,13 @@ public function toArray(): array

foreach ($this->relationships as $relationship) {
$nested = $attributes = [];
$test = u($relationship);

if (Str::contains($relationship, '.')) {
if ($test->containsAny('.')) {
[$relationship, $nested] = explode('.', $relationship, 2);
$test = u($relationship);
}
if (Str::contains($relationship, ':')) {
if ($test->containsAny(':')) {
[$relationship, $attributes] = explode(':', $relationship, 2);
$attributes = explode(',', $attributes);
}
Expand Down Expand Up @@ -114,7 +116,7 @@ private function extractAttributes(array $attributes): array

foreach ($attributes as $key => $value) {
if ($this->shouldExtractAttribute($key)) {
$key = Str::snake($this->getAttributeExtractionKey($key));
$key = $this->getAttributeExtractionKey($key);

if (is_object($value)) {
$attrs[$key] = $this->extractPropertiesFrom($value);
Expand Down
4 changes: 2 additions & 2 deletions src/ModelIdentityMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Somnambulist\Components\ReadModels;

use IlluminateAgnostic\Str\Support\Str;
use function array_intersect_key;
use function array_key_exists;
use function array_map;
use function explode;
use function get_class;
use function Symfony\Component\String\u;

final class ModelIdentityMap
{
Expand Down Expand Up @@ -87,7 +87,7 @@ public function inferRelationshipFromAttributes(Model $model, array &$attributes
foreach ($attributes as $key => $value) {
$ref = null;

if (Str::startsWith($key, Relationships\AbstractRelationship::RELATIONSHIP_SOURCE_MODEL_REF)) {
if (u($key)->startsWith(Relationships\AbstractRelationship::RELATIONSHIP_SOURCE_MODEL_REF)) {
$ref = array_values(array_slice(explode('__', $key), -1))[0];
unset($attributes[$key]);
} elseif ($model->meta()->foreignKey() === $key || $model->getOwningKey() === $key) {
Expand Down
4 changes: 2 additions & 2 deletions src/ModelMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Somnambulist\Components\ReadModels;

use IlluminateAgnostic\Str\Support\Arr;
use IlluminateAgnostic\Str\Support\Str;
use Somnambulist\Components\ReadModels\Utils\ClassHelpers;
use function explode;
use function sprintf;
use function stripos;
use function Symfony\Component\String\u;

final class ModelMetadata
{
Expand Down Expand Up @@ -84,7 +84,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', u(ClassHelpers::getObjectShortClassName($this->model))->snake(), $this->primaryKeyName());

return $this->foreignKey ?? $key;
}
Expand Down
8 changes: 4 additions & 4 deletions src/TypeCasters/DoctrineTypeCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Type;
use IlluminateAgnostic\Str\Support\Str;
use Somnambulist\Components\AttributeModel\Contracts\AttributeCasterInterface;
use Somnambulist\Components\ReadModels\Utils\StrConverter;
use function array_key_exists;
use function array_keys;
use function is_null;
use function Symfony\Component\String\u;

final class DoctrineTypeCaster implements AttributeCasterInterface
{
Expand All @@ -24,7 +24,7 @@ public function types(): array

public function supports(string $type): bool
{
return in_array($type, $this->types()) || Str::startsWith($type, 'resource:');
return in_array($type, $this->types()) || u($type)->startsWith('resource:');
}

public function cast(array &$attributes, $attribute, string $type): void
Expand All @@ -35,9 +35,9 @@ public function cast(array &$attributes, $attribute, string $type): void

$value = $attributes[$attribute];

if (Str::startsWith($type, 'resource:')) {
if (u($type)->startsWith('resource:')) {
$value = is_null($value) ? null : StrConverter::toResource($value);
$type = Str::replaceFirst('resource:', '', $type);
$type = u($type)->replace('resource:', '')->toString();
}

$attributes[$attribute] = Type::getType($type)->convertToPHPValue($value, $this->connection->getDatabasePlatform());
Expand Down
9 changes: 5 additions & 4 deletions src/Utils/FilterGeneratedKeysFromCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Somnambulist\Components\ReadModels\Utils;

use IlluminateAgnostic\Str\Support\Str;
use Somnambulist\Components\Collection\MutableCollection as Collection;
use Somnambulist\Components\ReadModels\Relationships\AbstractRelationship;
use function is_string;
use function Symfony\Component\String\u;

final class FilterGeneratedKeysFromCollection
{
Expand All @@ -22,11 +22,12 @@ public function __invoke($attributes): array
Collection::collect($attributes)
->filter(function ($value, $key) {
$ignorable =
Str::contains($key, [AbstractRelationship::INTERNAL_KEY_PREFIX])
is_string($key) && u($key)->containsAny([AbstractRelationship::INTERNAL_KEY_PREFIX])
||
(
is_string($value) && Str::contains($value, [
AbstractRelationship::RELATIONSHIP_SOURCE_MODEL_REF, AbstractRelationship::RELATIONSHIP_TARGET_MODEL_REF
is_string($value) && u($value)->containsAny([
AbstractRelationship::RELATIONSHIP_SOURCE_MODEL_REF,
AbstractRelationship::RELATIONSHIP_TARGET_MODEL_REF,
])
)
;
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/GenerateRelationshipsToEagerLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Somnambulist\Components\ReadModels\Utils;

use IlluminateAgnostic\Str\Support\Str;
use Somnambulist\Components\ReadModels\Contracts\Queryable;
use function array_merge;
use function count;
use function explode;
use function implode;
use function is_numeric;
use function Symfony\Component\String\u;

/**
* Encapsulates the logic for generating eager loaded relationships.
Expand Down Expand Up @@ -55,7 +55,7 @@ private function parseWithRelationships(array $relations): array
if (is_numeric($name)) {
$name = $constraints;

[$name, $constraints] = Str::contains($name, ':')
[$name, $constraints] = u($name)->containsAny(':')
? $this->createSelectWithConstraint($name)
: [
$name, function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Models/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Address
private ?string $county;
private ?string $postcode;

public function __construct(string $line1 = null, string $line2 = null, string $town = null, string $county = null, string $postcode = null)
public function __construct(?string $line1 = null, ?string $line2 = null, ?string $town = null, ?string $county = null, ?string $postcode = null)
{
$this->line1 = $line1;
$this->line2 = $line2;
Expand Down

0 comments on commit 1763481

Please sign in to comment.