Skip to content

Commit

Permalink
Fixes for PHP 8.4 deprecations
Browse files Browse the repository at this point in the history
Switch to symfony/string from pragmarx/ia-str
  • Loading branch information
dave-redfern committed Nov 28, 2024
1 parent 1a3af31 commit 8bb9bc8
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 87 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 @@ -11,6 +11,7 @@

###> symfony/phpunit-bridge ###
.phpunit
.phpunit.cache
.phpunit.result.cache
/phpunit.xml
###< symfony/phpunit-bridge ###
Expand Down
5 changes: 4 additions & 1 deletion .idea/api-client.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/php-test-framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
==========

2024-11-27 - 4.1.1
------------------

* Fix PHP 8.4 deprecations
* Switch to symfony/string instead of pragmarx/ia-str

2024-02-24 - 4.1.0
------------------

Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
"require": {
"php": ">=8.1",
"ext-json": "*",
"beberlei/assert": "^3.0",
"psr/log": "^1|^2|^3",
"beberlei/assert": "^3.3",
"pagerfanta/pagerfanta": "^3.5",
"psr/log": "^1|^2|^3",
"somnambulist/attribute-model": "^3.0",
"somnambulist/collection": "^5.3",
"somnambulist/collection": "^5.5",
"symfony/event-dispatcher": "^6.4|^7.0",
"symfony/http-client": "^6.4|^7.0",
"symfony/routing": "^6.4|^7.0"
"symfony/routing": "^6.4|^7.0",
"symfony/string": "^6.4|^7.0"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
"somnambulist/domain": "^5.0",
"somnambulist/domain": "^6.0",
"symfony/framework-bundle": "^6.4",
"symfony/phpunit-bridge": "^6.4",
"symfony/var-dumper": "^6.4"
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Decorators/RecordResponseDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RecordResponseDecorator extends AbstractDecorator

private string $mode;

public function __construct(ConnectionInterface $client, string $mode = null)
public function __construct(ConnectionInterface $client, ?string $mode = null)
{
$this->connection = $client;
$this->mode = in_array($mode, [self::RECORD, self::PLAYBACK, self::PASSTHRU], true) ? $mode : self::PASSTHRU;
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Query/Behaviours/EncodeSimpleFilterConditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function encode(QueryBuilder $builder): array
abstract protected function sort(array &$args = []): void;
abstract protected function createFilters(?CompositeExpression $expression): array;
abstract protected function createInclude(array $includes = []): array;
abstract protected function createLimit(int $limit = null, string $marker = null): array;
abstract protected function createLimit(?int $limit = null, ?string $marker = null): array;
abstract protected function createOrderBy(array $orderBy = []): array;
abstract protected function createPagination(int $page = 1, int $perPage = 30): array;
abstract protected function createPaginationFromLimitAndOffset(int $limit = null, int $offset = null): array;
abstract protected function createPaginationFromLimitAndOffset(?int $limit = null, ?int $offset = null): array;
}
9 changes: 5 additions & 4 deletions src/Client/Query/Encoders/AbstractEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function ksort;
use function max;
use function strtolower;
use function Symfony\Component\String\u;

abstract class AbstractEncoder implements QueryEncoderInterface
{
Expand Down Expand Up @@ -56,13 +57,13 @@ protected function createInclude(array $includes = []): array
}

if ($this->snakeCaseIncludes) {
$includes = array_map(fn($include) => Str::snake($include), $includes);
$includes = array_map(fn($include) => u($include)->replace('.', 'aaadotaaa')->snake()->replace('aaadotaaa', '.')->toString(), $includes);
}

return [$this->mappings[self::INCLUDE] => implode(',', $includes)];
}

protected function createLimit(int $limit = null, string $marker = null): array
protected function createLimit(?int $limit = null, ?string $marker = null): array
{
if (is_null($limit) && is_null($marker)) {
return [];
Expand All @@ -88,7 +89,7 @@ protected function createOrderBy(array $orderBy = []): array
return [$this->mappings[self::ORDER_BY] => implode(',', $sort)];
}

protected function createPagination(int $page = null, int $perPage = null): array
protected function createPagination(?int $page = null, ?int $perPage = null): array
{
if (is_null($page) && is_null($perPage)) {
return [];
Expand All @@ -100,7 +101,7 @@ protected function createPagination(int $page = null, int $perPage = null): arra
return [$this->mappings[self::PAGE] => $page, $this->mappings[self::PER_PAGE] => $perPage];
}

protected function createPaginationFromLimitAndOffset(int $limit = null, int $offset = null): array
protected function createPaginationFromLimitAndOffset(?int $limit = null, ?int $offset = null): array
{
if (is_null($limit) && is_null($offset)) {
return [];
Expand Down
6 changes: 3 additions & 3 deletions src/Client/Query/Encoders/JsonApiEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ protected function createFilters(?CompositeExpression $expression): array
return [$this->mappings[self::FILTERS] => $filters];
}

protected function createPagination(int $page = null, int $perPage = null): array
protected function createPagination(?int $page = null, ?int $perPage = null): array
{
return ['page' => parent::createPagination($page, $perPage)];
}

protected function createPaginationFromLimitAndOffset(int $limit = null, int $offset = null): array
protected function createPaginationFromLimitAndOffset(?int $limit = null, ?int $offset = null): array
{
return ['page' => parent::createPaginationFromLimitAndOffset($limit, $offset)];
}

protected function createLimit(int $limit = null, string $marker = null): array
protected function createLimit(?int $limit = null, ?string $marker = null): array
{
return ['page' => parent::createLimit($limit, $marker)];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Query/Encoders/OpenStackApiEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ protected function createOrderBy(array $orderBy = []): array
return [$this->mappings[self::ORDER_BY] => implode(',', $sort)];
}

protected function createPagination(int $page = null, int $perPage = null): array
protected function createPagination(?int $page = null, ?int $perPage = null): array
{
return [];
}

protected function createPaginationFromLimitAndOffset(int $limit = null, int $offset = null): array
protected function createPaginationFromLimitAndOffset(?int $limit = null, ?int $offset = null): array
{
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Query/Expression/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getValue(): mixed
return $this->value;
}

public function toString(string $operator = null): string
public function toString(?string $operator = null): string
{
$val = $this->getValueAsString();

Expand Down
8 changes: 4 additions & 4 deletions src/Client/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,28 @@ public function addOrderBy(string $field, string $dir = 'asc'): self
return $this;
}

public function page(int $page = null): self
public function page(?int $page = null): self
{
$this->page = !is_null($page) ? max($page, 1) : null;

return $this;
}

public function perPage(int $perPage = null): self
public function perPage(?int $perPage = null): self
{
$this->perPage = !is_null($perPage) ? ($perPage < 1 ? 30 : $perPage) : null;

return $this;
}

public function limit(int $limit = null): self
public function limit(?int $limit = null): self
{
$this->limit = is_null($limit) ? null : ($limit < 1 ? 100 : $limit);

return $this;
}

public function offset(string $offset = null): self
public function offset(?string $offset = null): self
{
$this->offset = $offset;

Expand Down
4 changes: 2 additions & 2 deletions src/Client/ResponseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class ResponseStore
private static ?ResponseStore $instance = null;
private ?string $store;

private function __construct(string $store = null)
private function __construct(?string $store = null)
{
$this->store = $store;
}

public static function instance(string $store = null): self
public static function instance(?string $store = null): self
{
if (!self::$instance instanceof ResponseStore) {
self::$instance = new ResponseStore($store);
Expand Down
2 changes: 1 addition & 1 deletion src/EntityLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function findOrFail($id): object
return $this->query()->findOrFail($id);
}

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
{
return $this->query()->findBy($criteria, $orderBy, $limit, (string)$offset);
}
Expand Down
16 changes: 9 additions & 7 deletions src/ModelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use function strlen;
use function strtolower;
use function substr;
use function Symfony\Component\String\u;

/**
* Builds queries to fetch models from the configured API endpoint.
Expand All @@ -39,10 +40,10 @@
* @method ModelBuilder orWhere(ExpressionInterface ...$predicates)
* @method ModelBuilder orderBy(string $field, string $dir = 'asc')
* @method ModelBuilder addOrderBy(string $field, string $dir = 'asc')
* @method ModelBuilder page(int $page = null)
* @method ModelBuilder perPage(int $perPage = null)
* @method ModelBuilder limit(int $limit = null)
* @method ModelBuilder offset(string $offset = null)
* @method ModelBuilder page(?int $page = null)
* @method ModelBuilder perPage(?int $perPage = null)
* @method ModelBuilder limit(?int $limit = null)
* @method ModelBuilder offset(?string $offset = null)
* @method ModelBuilder routeRequires(array $params)
*
* @method array getIncludes()
Expand Down Expand Up @@ -111,7 +112,7 @@ public function find(mixed $id): ?Model
*
* @return Collection
*/
public function findBy(array $criteria = [], array $orderBy = [], int $limit = null, string $offset = null): Collection
public function findBy(array $criteria = [], array $orderBy = [], ?int $limit = null, ?string $offset = null): Collection
{
foreach ($criteria as $field => $value) {
$this->whereField($field, 'eq', $value);
Expand Down Expand Up @@ -291,7 +292,8 @@ 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) {
if (Str::contains($name, '.') && Str::startsWith($name, $relation . '.')) {
$str = u($name);
if ($str->containsAny('.') && $str->startsWith($relation . '.')) {
$nested[] = substr($name, strlen($relation . '.'));
}
}
Expand Down Expand Up @@ -362,7 +364,7 @@ public function __call($name, $arguments)
if (method_exists($this->query, $name)) {
$ret = $this->query->{$name}(...$arguments);

if (Str::startsWith($name, 'get') || 'expr' === $name) {
if (u($name)->startsWith('get') || 'expr' === $name) {
return $ret;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/ActionPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ActionPersister implements ActionPersisterInterface, LoggerAwareInterface
protected ConnectionInterface $connection;
protected ResponseDecoderInterface $decoder;

public function __construct(ConnectionInterface $connection, ResponseDecoderInterface $decoder = null)
public function __construct(ConnectionInterface $connection, ?ResponseDecoderInterface $decoder = null)
{
$this->connection = $connection;
$this->decoder = $decoder ?? new SimpleJsonDecoder();
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Actions/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class AbstractAction implements ApiActionInterface
use HasObjectData;
use HasRouteData;

public function __construct(string $class, array $properties = [], string $route = '', array $params = [], string $method = null)
public function __construct(string $class, array $properties = [], string $route = '', array $params = [], ?string $method = null)
{
$this->class = $class;
$this->properties = $properties;
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Behaviours/HasRouteData.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function validateHttpMethod(?string $method): ?string
return in_array(strtolower(trim($method)), ['get', 'post', 'put', 'patch', 'delete', 'head']) ? $method : null;
}

public function route(string $route, array $params = [], string $method = null): self
public function route(string $route, array $params = [], ?string $method = null): self
{
$this->route = $route;
$this->params = $params;
Expand Down
11 changes: 6 additions & 5 deletions src/Relationships/AbstractRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
use Somnambulist\Components\ApiClient\ModelBuilder;
use Somnambulist\Components\Collection\Contracts\Collection;
use function sprintf;
use function Symfony\Component\String\u;

/**
* @method AbstractRelationship include(string ...$relationship)
* @method AbstractRelationship limit(int $limit = null)
* @method AbstractRelationship offset(string $offset = null)
* @method AbstractRelationship page(int $page = null)
* @method AbstractRelationship perPage(int $perPage = null)
* @method AbstractRelationship limit(?int $limit = null)
* @method AbstractRelationship offset(?string $offset = null)
* @method AbstractRelationship page(?int $page = null)
* @method AbstractRelationship perPage(?int $perPage = null)
*
* @method array getIncludes()
* @method array getOrderBy()
Expand Down Expand Up @@ -68,7 +69,7 @@ public function __call($name, $arguments)
{
$allowed = ['include', 'limit', 'offset', 'page', 'perPage'];

if (in_array($name, $allowed) || Str::startsWith($name, 'get')) {
if (in_array($name, $allowed) || u($name)->startsWith('get')) {
$ret = $this->query->{$name}(...$arguments);

if (!$ret instanceof $this->query) {
Expand Down
Loading

0 comments on commit 8bb9bc8

Please sign in to comment.