Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to prettier 3 #128

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"phpVersion": "8.1",
"singleQuote": true,
"trailingCommaPHP": true
"plugins": ["@prettier/plugin-php"],
"phpVersion": "8.1",
"trailingComma": "all",
"singleQuote": true
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"dependencies": {
"@prettier/plugin-php": "^0.19.7",
"@prettier/plugin-php": "^0.22.1",
"husky": "^3.0.5",
"lint-staged": "^9.4.0",
"prettier": "^1.18.2"
"prettier": "^3.1.1"
},
"scripts": {
"stan": "vendor/bin/phpstan analyse src/ -c phpstan.neon",
Expand Down
33 changes: 5 additions & 28 deletions src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
*
* @author Florent Clerc <florent.clerc@mapado.com>
*/
class Collection implements
\IteratorAggregate,
\Serializable,
\Countable,
\ArrayAccess
class Collection implements \IteratorAggregate, \Serializable, \Countable, \ArrayAccess
{
/**
* The elements of the collection.
Expand All @@ -38,7 +34,7 @@ class Collection implements
*/
public function __construct(
array $elements = [],
array $extraProperties = []
array $extraProperties = [],
) {
$this->elements = $elements;
$this->extraProperties = $extraProperties;
Expand All @@ -53,8 +49,6 @@ public function __serialize(): array
}

/**
* {@inheritdoc}
*
* @param string $values
*/
public function __unserialize($values): void
Expand All @@ -75,8 +69,6 @@ public function toArray(): array
}

/**
* {@inheritdoc}
*
* @deprecated `serialize` method is deprecated, `__serialize` is used instead. See https://php.watch/versions/8.1/serializable-deprecated
*/
public function serialize(): string
Expand All @@ -92,9 +84,6 @@ public function unserialize($data): void
$this->__unserialize($data);
}

/**
* {@inheritdoc}
*/
public function count(): int
{
return count($this->elements);
Expand All @@ -109,10 +98,7 @@ public function getTotalItems(): int
}

/**
* {@inheritdoc}
*
* @param mixed|null $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
{
Expand All @@ -124,8 +110,6 @@ public function offsetSet($offset, $value): void
}

/**
* {@inheritdoc}
*
* @param mixed|null $offset
*/
public function offsetExists($offset): bool
Expand All @@ -134,8 +118,6 @@ public function offsetExists($offset): bool
}

/**
* {@inheritdoc}
*
* @param mixed|null $offset
*/
public function offsetUnset($offset): void
Expand All @@ -144,8 +126,6 @@ public function offsetUnset($offset): void
}

/**
* {@inheritdoc}
*
* @param mixed|null $offset
*
* @return mixed|null
Expand All @@ -155,9 +135,6 @@ public function offsetGet($offset): mixed
return $this->elements[$offset] ?? null;
}

/**
* {@inheritdoc}
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->elements);
Expand Down Expand Up @@ -187,13 +164,13 @@ public function getIntExtraProperty(string $key): ?int

/**
* return the value of an extra property
*
* @return mixed
*/
public function getExtraProperty(string $key)
public function getExtraProperty(string $key): mixed
{
if (isset($this->extraProperties[$key])) {
return $this->extraProperties[$key];
}

return null;
}
}
77 changes: 52 additions & 25 deletions src/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(
SdkClient $sdkClient,
RestClient $restClient,
UnitOfWork $unitOfWork,
string $entityName
string $entityName,
) {
$this->sdk = $sdkClient;
$this->restClient = $restClient;
Expand All @@ -86,12 +86,18 @@ public function __call(string $method, array $arguments)
break;

default:
throw new \BadMethodCallException('Undefined method \'' . $method . '\'. The method name must start with
either findBy or findOneBy!');
throw new \BadMethodCallException(
'Undefined method \'' .
$method .
'\'. The method name must start with
either findBy or findOneBy!',
);
}

if (empty($arguments)) {
throw new SdkException('You need to pass a parameter to ' . $method);
throw new SdkException(
'You need to pass a parameter to ' . $method,
);
}

$mapping = $this->sdk->getMapping();
Expand Down Expand Up @@ -130,7 +136,7 @@ public function __call(string $method, array $arguments)
if (null !== $hydratedData) {
$this->unitOfWork->registerClean(
$identifier,
$hydratedData
$hydratedData,
);
}
$this->saveToCache($identifier, $hydratedData);
Expand Down Expand Up @@ -201,7 +207,11 @@ public function findAll(): Collection
// if entityList is found in cache, return it
if (false !== $entityListFromCache) {
if (!$entityListFromCache instanceof Collection) {
throw new \RuntimeException('Entity list in cache should be an instance of ' . Collection::class . '. This should not happen.');
throw new \RuntimeException(
'Entity list in cache should be an instance of ' .
Collection::class .
'. This should not happen.',
);
}

return $entityListFromCache;
Expand All @@ -219,7 +229,9 @@ public function findAll(): Collection
// then cache each entity from list
foreach ($entityList as $entity) {
if (!is_object($entity)) {
throw new \RuntimeException("Entity should be an object. This should not happen.");
throw new \RuntimeException(
'Entity should be an object. This should not happen.',
);
}

$identifier = $entity->{$this->getClassMetadata()->getIdGetter()}();
Expand Down Expand Up @@ -248,27 +260,27 @@ public function remove(object $model): void
public function update(
object $model,
array $serializationContext = [],
array $queryParams = []
array $queryParams = [],
): object {
$identifier = $model->{$this->getClassMetadata()->getIdGetter()}();
$serializer = $this->sdk->getSerializer();
$newSerializedModel = $serializer->serialize(
$model,
$this->entityName,
$serializationContext
$serializationContext,
);

$oldModel = $this->unitOfWork->getDirtyEntity($identifier);
if ($oldModel) {
$oldSerializedModel = $serializer->serialize(
$oldModel,
$this->entityName,
$serializationContext
$serializationContext,
);
$newSerializedModel = $this->unitOfWork->getDirtyData(
$newSerializedModel,
$oldSerializedModel,
$this->getClassMetadata()
$this->getClassMetadata(),
);
}

Expand All @@ -283,7 +295,9 @@ public function update(
$out = $hydrator->hydrate($data, $this->entityName);

if (null === $out) {
throw new HydratorException("Unable to convert data from PUT request ({$path}) to an instance of {$this->entityName}. Maybe you have a custom hydrator returning null?");
throw new HydratorException(
"Unable to convert data from PUT request ({$path}) to an instance of {$this->entityName}. Maybe you have a custom hydrator returning null?",
);
}

return $out;
Expand All @@ -292,7 +306,7 @@ public function update(
public function persist(
object $model,
array $serializationContext = [],
array $queryParams = []
array $queryParams = [],
): object {
$mapping = $this->sdk->getMapping();
$prefix = $mapping->getIdPrefix();
Expand All @@ -308,25 +322,30 @@ public function persist(
$diff = $this->unitOfWork->getDirtyData(
$newSerializedModel,
$oldSerializedModel,
$this->getClassMetadata()
$this->getClassMetadata(),
);

$data = $this->restClient->post(
$this->addQueryParameter($path, $queryParams),
$diff
$diff,
);
$data = $this->assertNotObject($data, __METHOD__);

if (null === $data) {
throw new RestException("No data found after sending a `POST` request to {$path}. Did the server returned a 4xx or 5xx status code?", $path);
throw new RestException(
"No data found after sending a `POST` request to {$path}. Did the server returned a 4xx or 5xx status code?",
$path,
);
}

$hydrator = $this->sdk->getModelHydrator();

$out = $hydrator->hydrate($data, $this->entityName);

if (null === $out) {
throw new HydratorException("Unable to convert data from POST request ({$path}) to an instance of {$this->entityName}. Maybe you have a custom hydrator returning null?");
throw new HydratorException(
"Unable to convert data from POST request ({$path}) to an instance of {$this->entityName}. Maybe you have a custom hydrator returning null?",
);
}

return $out;
Expand All @@ -343,7 +362,9 @@ protected function fetchFromCache(string $key): object|false
$cacheData = $cacheItem->get();

if (!is_object($cacheData)) {
throw new \RuntimeException('Cache data should be an object. This should not happen.');
throw new \RuntimeException(
'Cache data should be an object. This should not happen.',
);
}

return $cacheData;
Expand Down Expand Up @@ -390,7 +411,7 @@ protected function removeFromCache(string $key): bool

protected function addQueryParameter(
string $path,
array $params = []
array $params = [],
): string {
if (empty($params)) {
return $path;
Expand All @@ -405,7 +426,7 @@ private function convertQueryParameters(array $queryParameters): array

return array_map(function ($item) use ($mapping) {
if (is_object($item)) {
$classname = get_class($item);
$classname = $item::class;

if ($mapping->hasClassMetadata($classname)) {
$idGetter = $mapping
Expand All @@ -425,7 +446,9 @@ private function normalizeCacheKey(string $key): string
$out = preg_replace('~[\\/\{\}@:\(\)]~', '_', $key);

if (null === $out) {
throw new \RuntimeException('Unable to normalize cache key. This should not happen.');
throw new \RuntimeException(
'Unable to normalize cache key. This should not happen.',
);
}

return $out;
Expand All @@ -451,9 +474,11 @@ private function assertArray($data, string $methodName): array
return $data;
}

$type = null === $data ? 'null' : get_class($data);
$type = null === $data ? 'null' : $data::class;

throw new UnexpectedTypeException("Return of method {$methodName} should be an array. {$type} given.");
throw new UnexpectedTypeException(
"Return of method {$methodName} should be an array. {$type} given.",
);
}

/**
Expand All @@ -467,8 +492,10 @@ private function assertNotObject($data, string $methodName)
return $data;
}

$type = get_class($data);
$type = $data::class;

throw new UnexpectedTypeException("Return of method {$methodName} should be an array. {$type} given.");
throw new UnexpectedTypeException(
"Return of method {$methodName} should be an array. {$type} given.",
);
}
}
3 changes: 1 addition & 2 deletions src/Exception/RestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Mapado\RestClientSdk\Exception;

use Exception;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -41,7 +40,7 @@ public function __construct(
string $path,
array $params = [],
int $code = 0,
?Exception $previous = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bof, d'enlever ce ?, c'est bien nullable (la valeur par défaut est d'ailleurs null)
C'est prettier qui fait ça ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oui c'est prettier

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK ça doit être la PER1 peut-être. Si ce n'est pas le cas il le remettra tout seul

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est quoi la PER1 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.php-fig.org/per/coding-style/

Per coding style pardon : per = standard évolutif, remplace les PSR2 ET 12

\Exception $previous = null,
) {
parent::__construct($message, $code, $previous);
$this->path = $path;
Expand Down
Loading