From c03326d8ed38b78c37ca1b9aa4b2b784f99ed464 Mon Sep 17 00:00:00 2001 From: Daniel Kurowski Date: Thu, 13 Apr 2023 09:05:46 +0200 Subject: [PATCH] cs --- .../Request/AdministrationUnitParameters.php | 6 ++- src/BisClient.php | 13 ++++--- src/BisClientFactory.php | 3 +- src/Endpoint.php | 6 ++- src/Event/Request/EventParameters.php | 38 ++++++++++--------- src/Event/Response/Event.php | 12 +++--- src/HttpClient.php | 10 +++-- src/LimitParameter.php | 4 +- .../Request/OpportunityParameters.php | 9 +++-- src/QueryParameters.php | 4 +- src/Response/Coordinates.php | 5 ++- src/exceptions.php | 16 +++++--- 12 files changed, 79 insertions(+), 47 deletions(-) diff --git a/src/AdministrationUnit/Request/AdministrationUnitParameters.php b/src/AdministrationUnit/Request/AdministrationUnitParameters.php index be1252c..7cda34b 100644 --- a/src/AdministrationUnit/Request/AdministrationUnitParameters.php +++ b/src/AdministrationUnit/Request/AdministrationUnitParameters.php @@ -5,6 +5,8 @@ use HnutiBrontosaurus\BisClient\AdministrationUnit\Category; use HnutiBrontosaurus\BisClient\LimitParameter; use HnutiBrontosaurus\BisClient\QueryParameters; +use function count; +use function implode; final class AdministrationUnitParameters implements QueryParameters @@ -42,8 +44,8 @@ public function toArray(): array { $array = []; - if (\count($this->categories) > 0) { - $array['category'] = \implode(',', $this->categories); + if (count($this->categories) > 0) { + $array['category'] = implode(',', $this->categories); } return $array; diff --git a/src/BisClient.php b/src/BisClient.php index c271db2..3208633 100644 --- a/src/BisClient.php +++ b/src/BisClient.php @@ -8,6 +8,9 @@ use HnutiBrontosaurus\BisClient\Event\Response\Event; use HnutiBrontosaurus\BisClient\Opportunity\Request\OpportunityParameters as OpportunityParameters; use HnutiBrontosaurus\BisClient\Opportunity\Response\Opportunity; +use function array_map; +use function array_slice; +use function count; final class BisClient @@ -28,7 +31,7 @@ public function getEvents(?EventParameters $params = null): array { $params = $params !== null ? $params : new EventParameters(); $events = $this->retrieve(Endpoint::EVENTS(), $params, $params->getLimit()); - return \array_map(static fn($result) => Event::fromResponseData($result), $events); + return array_map(static fn($result) => Event::fromResponseData($result), $events); } @@ -62,7 +65,7 @@ public function getAdministrationUnits(?AdministrationUnitParameters $params = n { $params = $params !== null ? $params : new AdministrationUnitParameters(); $administrationUnits = $this->retrieve(Endpoint::ADMINISTRATION_UNITS(), $params, $params->getLimit()); - return \array_map(static fn($result) => AdministrationUnit::fromResponseData($result), $administrationUnits); + return array_map(static fn($result) => AdministrationUnit::fromResponseData($result), $administrationUnits); } @@ -76,7 +79,7 @@ public function getOpportunities(?OpportunityParameters $params = null): array { $params = $params !== null ? $params : new OpportunityParameters(); $opportunities = $this->retrieve(Endpoint::OPPORTUNITIES(), $params, $params->getLimit()); - return \array_map(static fn($result) => Opportunity::fromResponseData($result), $opportunities); + return array_map(static fn($result) => Opportunity::fromResponseData($result), $opportunities); } @@ -123,7 +126,7 @@ private function retrieve(string $endpoint, ?QueryParameters $params, ?int $limi endpoint: $data['next'], params: null, // params are already included in next URL limit: $limit, - currentCount: $currentCount + \count($results), + currentCount: $currentCount + count($results), topLevel: false, ); $results = [...$results, ...$moreResults]; @@ -131,7 +134,7 @@ private function retrieve(string $endpoint, ?QueryParameters $params, ?int $limi // keep only given count of data if ($topLevel && $limit !== null) { - $results = \array_slice($results, 0, $limit); + $results = array_slice($results, 0, $limit); } return $results; diff --git a/src/BisClientFactory.php b/src/BisClientFactory.php index f0e0796..e6377e2 100644 --- a/src/BisClientFactory.php +++ b/src/BisClientFactory.php @@ -6,6 +6,7 @@ use GuzzleHttp\HandlerStack; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use function rtrim; /* @@ -24,7 +25,7 @@ public function __construct(string $apiUrl) $stack = HandlerStack::create(); $stack->push(self::lastRequestUrlIntoHeaderMiddleware()); $this->underlyingHttpClient = new Client([ - 'base_uri' => \rtrim($apiUrl, '/') . '/', + 'base_uri' => rtrim($apiUrl, '/') . '/', 'handler' => $stack, ]); } diff --git a/src/Endpoint.php b/src/Endpoint.php index a814c0e..574bbed 100644 --- a/src/Endpoint.php +++ b/src/Endpoint.php @@ -2,13 +2,15 @@ namespace HnutiBrontosaurus\BisClient; +use function sprintf; + final class Endpoint { public static function EVENT(int $id): string { - return \sprintf('web/events/%d/', $id); + return sprintf('web/events/%d/', $id); } public static function EVENTS(): string @@ -28,7 +30,7 @@ public static function OPPORTUNITIES(): string public static function OPPORTUNITY(int $id): string { - return \sprintf('web/opportunities/%d/', $id); + return sprintf('web/opportunities/%d/', $id); } } diff --git a/src/Event/Request/EventParameters.php b/src/Event/Request/EventParameters.php index b4f85cb..6f598ea 100644 --- a/src/Event/Request/EventParameters.php +++ b/src/Event/Request/EventParameters.php @@ -9,11 +9,15 @@ use HnutiBrontosaurus\BisClient\Event\Group; use HnutiBrontosaurus\BisClient\Event\IntendedFor; use HnutiBrontosaurus\BisClient\Event\Program; +use HnutiBrontosaurus\BisClient\LimitParameter; +use HnutiBrontosaurus\BisClient\QueryParameters; +use function count; +use function implode; -final class EventParameters implements \HnutiBrontosaurus\BisClient\QueryParameters +final class EventParameters implements QueryParameters { - use \HnutiBrontosaurus\BisClient\LimitParameter; + use LimitParameter; private Ordering $ordering; private TimeZone $timeZone; @@ -51,7 +55,7 @@ public function setAdministrationUnits(array $ids): self // groups - /** @var \HnutiBrontosaurus\BisClient\Event\Group[] */ + /** @var Group[] */ private array $groups = []; public function setGroup(Group $group): self @@ -61,7 +65,7 @@ public function setGroup(Group $group): self } /** - * @param \HnutiBrontosaurus\BisClient\Event\Group[] $groups + * @param Group[] $groups */ public function setGroups(array $groups): self { @@ -72,7 +76,7 @@ public function setGroups(array $groups): self // category - /** @var \HnutiBrontosaurus\BisClient\Event\Category[] */ + /** @var Category[] */ private array $categories = []; public function setCategory(Category $category): self @@ -93,7 +97,7 @@ public function setCategories(array $categories): self // program - /** @var \HnutiBrontosaurus\BisClient\Event\Program[] */ + /** @var Program[] */ private array $programs = []; public function setProgram(Program $program): self @@ -124,7 +128,7 @@ public function setIntendedFor(IntendedFor $intendedFor): self } /** - * @param \HnutiBrontosaurus\BisClient\Event\IntendedFor[] $intendedFor + * @param IntendedFor[] $intendedFor */ public function setMultipleIntendedFor(array $intendedFor): self { @@ -229,20 +233,20 @@ public function toArray(): array 'ordering' => $this->ordering->toScalar(), ]; - if (\count($this->administrationUnits) > 0) { - $array['administration_unit'] = \implode(',', $this->administrationUnits); + if (count($this->administrationUnits) > 0) { + $array['administration_unit'] = implode(',', $this->administrationUnits); } - if (\count($this->groups) > 0) { - $array['group'] = \implode(',', $this->groups); + if (count($this->groups) > 0) { + $array['group'] = implode(',', $this->groups); } - if (\count($this->categories) > 0) { - $array['category'] = \implode(',', $this->categories); + if (count($this->categories) > 0) { + $array['category'] = implode(',', $this->categories); } - if (\count($this->programs) > 0) { - $array['program'] = \implode(',', $this->programs); + if (count($this->programs) > 0) { + $array['program'] = implode(',', $this->programs); } - if (\count($this->intendedFor) > 0) { - $array['intended_for'] = \implode(',', $this->intendedFor); + if (count($this->intendedFor) > 0) { + $array['intended_for'] = implode(',', $this->intendedFor); } if ($this->dateStartLessThanOrEqualTo !== null) { diff --git a/src/Event/Response/Event.php b/src/Event/Response/Event.php index 92245cf..98ad832 100644 --- a/src/Event/Response/Event.php +++ b/src/Event/Response/Event.php @@ -11,6 +11,8 @@ use HnutiBrontosaurus\BisClient\Response\ContactPerson; use HnutiBrontosaurus\BisClient\Response\Coordinates; use HnutiBrontosaurus\BisClient\Response\Location; +use function array_map; +use function array_shift; final class Event @@ -117,11 +119,11 @@ private function __construct( */ public static function fromResponseData(array $data): self { - $photos = \array_map( + $photos = array_map( static fn($photo) => Photo::from($photo['image']), $data['propagation']['images'], ); - $mainPhoto = \array_shift($photos); + $mainPhoto = array_shift($photos); return new self( $data['id'], @@ -149,7 +151,7 @@ public static function fromResponseData(array $data): self $data['propagation']['accommodation'] !== '' ? $data['propagation']['accommodation'] : null, $data['propagation']['working_days'], $data['propagation']['working_hours'], - \array_map(static fn($diet) => Diet::fromScalar($diet['slug']), $data['propagation']['diets']), + array_map(static fn($diet) => Diet::fromScalar($diet['slug']), $data['propagation']['diets']), $data['propagation']['organizers'] !== '' ? $data['propagation']['organizers'] : null, $data['propagation']['web_url'] !== '' ? $data['propagation']['web_url'] : null, $data['propagation']['invitation_text_introduction'], @@ -161,7 +163,7 @@ public static function fromResponseData(array $data): self $data['propagation']['contact_email'] !== null ? $data['propagation']['contact_email'] : '', // todo temp unless BIS returns nulls for some old events $data['propagation']['contact_phone'] !== null && $data['propagation']['contact_phone'] !== '' ? $data['propagation']['contact_phone'] : null, ), - \array_map( + array_map( static fn($photo) => Image::from($photo['image']), $data['propagation']['images'], ), @@ -358,7 +360,7 @@ public function getAccommodation(): ?string */ public function getFood(): array { - return \array_map( + return array_map( static fn(Diet $diet): Food => Food::fromScalar($diet->toScalar()), $this->getPropagation()->getDiets(), ); diff --git a/src/HttpClient.php b/src/HttpClient.php index 8a26742..9b9ccca 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -9,6 +9,10 @@ use GuzzleHttp\Exception\TooManyRedirectsException; use GuzzleHttp\Psr7\Request; use Psr\Http\Client\NetworkExceptionInterface; +use function assert; +use function http_build_query; +use function is_array; +use function json_decode; final class HttpClient @@ -32,7 +36,7 @@ public function send( ): array { $queryString = $queryParameters !== null - ? '?' . \http_build_query($queryParameters->toArray()) + ? '?' . http_build_query($queryParameters->toArray()) : ''; // see Guzzle exceptions docs: https://docs.guzzlephp.org/en/stable/quickstart.html#exceptions @@ -69,8 +73,8 @@ public function send( $content = $response->getBody()->getContents(); $this->lastResponseEncoded = $content; - $decoded = \json_decode($content, flags: JSON_OBJECT_AS_ARRAY); - \assert(\is_array($decoded)); + $decoded = json_decode($content, flags: JSON_OBJECT_AS_ARRAY); + assert(is_array($decoded)); $this->lastResponseDecoded = $decoded; return $decoded; } diff --git a/src/LimitParameter.php b/src/LimitParameter.php index fc659ff..f4240fc 100644 --- a/src/LimitParameter.php +++ b/src/LimitParameter.php @@ -2,6 +2,8 @@ namespace HnutiBrontosaurus\BisClient; +use function sprintf; + /** * Limit parameter is not appended to request as API doesn't support it. @@ -19,7 +21,7 @@ public function setLimit(?int $limit): self throw new UsageException("Limit must be at least 1. If you want to remove limit, use 'removeLimit()' method"); } elseif ($limit < 1) { - throw new UsageException(\sprintf("Limit must be at least 1, '%d' given", $limit)); + throw new UsageException(sprintf("Limit must be at least 1, '%d' given", $limit)); } } diff --git a/src/Opportunity/Request/OpportunityParameters.php b/src/Opportunity/Request/OpportunityParameters.php index 0de9920..eb27105 100644 --- a/src/Opportunity/Request/OpportunityParameters.php +++ b/src/Opportunity/Request/OpportunityParameters.php @@ -4,9 +4,12 @@ use HnutiBrontosaurus\BisClient\LimitParameter; use HnutiBrontosaurus\BisClient\Opportunity\Category; +use HnutiBrontosaurus\BisClient\QueryParameters; +use function count; +use function implode; -final class OpportunityParameters implements \HnutiBrontosaurus\BisClient\QueryParameters +final class OpportunityParameters implements QueryParameters { use LimitParameter; @@ -41,8 +44,8 @@ public function toArray(): array { $array = []; - if (\count($this->categories) > 0) { - $array['category'] = \implode(',', $this->categories); + if (count($this->categories) > 0) { + $array['category'] = implode(',', $this->categories); } return $array; diff --git a/src/QueryParameters.php b/src/QueryParameters.php index 94c73f6..60e93f8 100644 --- a/src/QueryParameters.php +++ b/src/QueryParameters.php @@ -2,13 +2,15 @@ namespace HnutiBrontosaurus\BisClient; +use Stringable; + interface QueryParameters { public function getLimit(): ?int; - /** @return array */ + /** @return array */ public function toArray(): array; } diff --git a/src/Response/Coordinates.php b/src/Response/Coordinates.php index ad1c96a..18a4f5b 100644 --- a/src/Response/Coordinates.php +++ b/src/Response/Coordinates.php @@ -3,6 +3,9 @@ namespace HnutiBrontosaurus\BisClient\Response; +use function sprintf; + + final class Coordinates { @@ -32,7 +35,7 @@ public function getLongitude(): float public function __toString(): string { - return \sprintf('%s,%s', $this->latitude, $this->longitude); + return sprintf('%s,%s', $this->latitude, $this->longitude); } } diff --git a/src/exceptions.php b/src/exceptions.php index dcfa1a7..a300c0e 100644 --- a/src/exceptions.php +++ b/src/exceptions.php @@ -2,6 +2,10 @@ namespace HnutiBrontosaurus\BisClient; +use LogicException; +use RuntimeException; +use Throwable; + // BIS client exceptions @@ -10,7 +14,7 @@ * e.g. called method in a wrong moment * Not meant to be caught in client code. */ -class UsageException extends \LogicException {} +class UsageException extends LogicException {} /** * Exception caused by unpredictable circumstances on server (not preventable) @@ -18,7 +22,7 @@ class UsageException extends \LogicException {} * This is generic exception to catch all possible cases from client. * To handle specific cases, catch specific exceptions from below. */ -class BisClientError extends \RuntimeException {} +class BisClientError extends RuntimeException {} // general error final class ConnectionToBisFailed extends BisClientError {} @@ -31,17 +35,17 @@ final class OpportunityNotFound extends BisClientError {} // underlying HTTP client exceptions -final class NotFound extends \RuntimeException +final class NotFound extends RuntimeException { - public static function withPrevious(\Throwable $previous): self + public static function withPrevious(Throwable $previous): self { return new self('The target you requested was not found. Check again that you\'ve typed correct URL or that the resource exists.', 0, $previous); } } -final class ConnectionError extends \RuntimeException +final class ConnectionError extends RuntimeException { - public static function withPrevious(\Throwable $previous): self + public static function withPrevious(Throwable $previous): self { return new self($previous->getMessage(), $previous->getCode(), $previous); }