Skip to content

Commit

Permalink
Merge pull request #126 from mapado/upgrade-phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienRAVIA authored Jan 5, 2024
2 parents 2dd4c21 + 806c022 commit a881783
Show file tree
Hide file tree
Showing 23 changed files with 563 additions and 434 deletions.
2 changes: 1 addition & 1 deletion .scenarios.lock/symfony4/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require-dev": {
"symfony/cache": "^4.0 || ^5.0 || ^6.0",
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^0.12.38",
"phpstan/phpstan": "^1.10",
"mapado/php-cs-fixer-config": "^3.2",
"g1a/composer-test-scenarios": "^3.0",
"giggsey/libphonenumber-for-php": "^8.0",
Expand Down
226 changes: 113 additions & 113 deletions .scenarios.lock/symfony4/composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .scenarios.lock/symfony5/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require-dev": {
"symfony/cache": "^4.0 || ^5.0 || ^6.0",
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^0.12.38",
"phpstan/phpstan": "^1.10",
"mapado/php-cs-fixer-config": "^3.2",
"g1a/composer-test-scenarios": "^3.0",
"giggsey/libphonenumber-for-php": "^8.0",
Expand Down
238 changes: 119 additions & 119 deletions .scenarios.lock/symfony5/composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .scenarios.lock/symfony6/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require-dev": {
"symfony/cache": "^4.0 || ^5.0 || ^6.0",
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^0.12.38",
"phpstan/phpstan": "^1.10",
"mapado/php-cs-fixer-config": "^3.2",
"g1a/composer-test-scenarios": "^3.0",
"giggsey/libphonenumber-for-php": "^8.0",
Expand Down
226 changes: 113 additions & 113 deletions .scenarios.lock/symfony6/composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Tests/Model/JsonLd/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Product
* @var int
*
* @Rest\Id
*
* @Rest\Attribute(name="id", type="integer")
*/
private $id;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require-dev": {
"symfony/cache": "^4.0 || ^5.0 || ^6.0",
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^0.12.38",
"phpstan/phpstan": "^1.10",
"mapado/php-cs-fixer-config": "^3.2",
"g1a/composer-test-scenarios": "^3.0",
"giggsey/libphonenumber-for-php": "^8.0",
Expand Down
30 changes: 16 additions & 14 deletions composer.lock

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"lint-staged": "^9.4.0",
"prettier": "^1.18.2"
},
"scripts": {
"stan": "vendor/bin/phpstan analyse src/ -c phpstan.neon",
"phpunit": "vendor/bin/phpunit"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
Expand Down
20 changes: 19 additions & 1 deletion src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public function __serialize(): array
*/
public function __unserialize($values): void
{
$this->elements = unserialize($values);
$unserializedValues = unserialize($values);

if (is_array($unserializedValues)) {
$this->elements = $unserializedValues;
}
}

/**
Expand Down Expand Up @@ -167,6 +171,20 @@ public function getExtraProperties(): array
return $this->extraProperties;
}

public function getStringExtraProperty(string $key): ?string
{
$value = $this->getExtraProperty($key);

return is_string($value) ? $value : null;
}

public function getIntExtraProperty(string $key): ?int
{
$value = $this->getExtraProperty($key);

return is_int($value) ? $value : null;
}

/**
* return the value of an extra property
*
Expand Down
8 changes: 4 additions & 4 deletions src/Collection/HydraPaginatedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ class HydraPaginatedCollection extends Collection
*/
public function getFirstPage(): ?string
{
return $this->getExtraProperty('hydra:firstPage');
return $this->getStringExtraProperty('hydra:firstPage');
}

/**
* Returns last page URI.
*/
public function getLastPage(): ?string
{
return $this->getExtraProperty('hydra:lastPage');
return $this->getStringExtraProperty('hydra:lastPage');
}

/**
* Returns next page URI.
*/
public function getNextPage(): ?string
{
return $this->getExtraProperty('hydra:nextPage');
return $this->getStringExtraProperty('hydra:nextPage');
}

/**
* Returns total item count.
*/
public function getTotalItems(): int
{
return $this->getExtraProperty('hydra:totalItems') ?? 0;
return $this->getIntExtraProperty('hydra:totalItems') ?? 0;
}
}
32 changes: 20 additions & 12 deletions src/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EntityRepository
protected $sdk;

/**
* @var string
* @var class-string
*/
protected $entityName;

Expand All @@ -53,7 +53,7 @@ class EntityRepository
*
* @param SdkClient $sdkClient The client to connect to the datasource with
* @param RestClient $restClient The client to process the http requests
* @param string $entityName The entity to work with
* @param class-string $entityName The entity to work with
*/
public function __construct(
SdkClient $sdkClient,
Expand Down Expand Up @@ -122,7 +122,7 @@ public function __call(string $method, array $arguments)

$data = $this->assertArray($data, $methodName);
$entityList = ArrayHelper::arrayGet($data, $collectionKey);
if (!empty($entityList)) {
if (!empty($entityList) && is_array($entityList)) {
$data = current($entityList);
$hydratedData = $hydrator->hydrate($data, $this->entityName);

Expand All @@ -144,8 +144,11 @@ public function __call(string $method, array $arguments)
// then cache each entity from list
foreach ($hydratedData as $entity) {
$identifier = $entity->{$this->getClassMetadata()->getIdGetter()}();
$this->saveToCache($identifier, $entity);
$this->unitOfWork->registerClean($identifier, $entity);

if (is_object($entity)) {
$this->saveToCache($identifier, $entity);
$this->unitOfWork->registerClean($identifier, $entity);
}
}
}

Expand All @@ -157,10 +160,9 @@ public function __call(string $method, array $arguments)
/**
* find - finds one item of the entity based on the @REST\Id field in the entity
*
* @param string|int|mixed $id id of the element to fetch
* @param array $queryParams query parameters to add to the query
*/
public function find($id, array $queryParams = []): ?object
public function find(string|int $id, array $queryParams = []): ?object
{
$hydrator = $this->sdk->getModelHydrator();
$id = $hydrator->convertId($id, $this->entityName);
Expand All @@ -169,7 +171,7 @@ public function find($id, array $queryParams = []): ?object

// if entity is found in cache, return it
$entityFromCache = $this->fetchFromCache($id);
if (false != $entityFromCache) {
if ($entityFromCache) {
return $entityFromCache;
}

Expand Down Expand Up @@ -216,7 +218,12 @@ 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.");
}

$identifier = $entity->{$this->getClassMetadata()->getIdGetter()}();

$this->unitOfWork->registerClean($identifier, $entity);
$this->saveToCache($identifier, $entity);
}
Expand Down Expand Up @@ -325,10 +332,7 @@ public function persist(
return $out;
}

/**
* @return object|false
*/
protected function fetchFromCache(string $key)
protected function fetchFromCache(string $key): object|false
{
$key = $this->normalizeCacheKey($key);
$cacheItemPool = $this->sdk->getCacheItemPool();
Expand All @@ -338,6 +342,10 @@ protected function fetchFromCache(string $key)
$cacheItem = $cacheItemPool->getItem($cacheKey);
$cacheData = $cacheItem->get();

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

return $cacheData;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class ArrayHelper
*
* @return mixed
*/
public static function arrayGet(array $array, ?string $key, $default = null)
{
public static function arrayGet(
array $array,
?string $key,
$default = null
): mixed {
if (null === $key) {
return $array;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function setMapping(array $classMetadataList): self

/**
* return a model class name for a given key
*
* @return class-string
*/
public function getModelName(string $key): string
{
Expand Down Expand Up @@ -211,7 +213,7 @@ private function checkMappingExistence(
}

if ($checkModelName) {
if ('' === $metadata->getModelName()) {
if (empty($metadata->getModelName())) {
throw new MappingException(
$key . ' key is mapped but the model name is empty'
);
Expand Down
12 changes: 12 additions & 0 deletions src/Mapping/Annotations/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @author Julien Deniau <julien.deniau@mapado.com>
*
* @Annotation
*
* @Target("CLASS")
*/
final class Entity
Expand All @@ -29,4 +30,15 @@ final class Entity
* @var string
*/
public $repository;

/**
* @return ?class-string
*/
public function getRepository(): ?string
{
/** @var ?class-string $repository */
$repository = $this->repository;

return $repository;
}
}
Loading

0 comments on commit a881783

Please sign in to comment.