From 95770ac1046329cd609da8d3daf9bad1e31c4400 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 20 Jul 2021 12:51:07 +0200 Subject: [PATCH] Increased phpstan level to 8 --- composer.json | 8 ++++---- module/Core/src/EventDispatcher/LocateVisit.php | 2 +- .../Core/src/Mercure/MercureUpdatesGenerator.php | 2 +- module/Core/src/Model/ShortUrlsParams.php | 2 +- module/Core/src/Model/Visitor.php | 9 ++++++--- module/Core/src/Model/VisitsParams.php | 2 +- .../Core/src/Repository/ShortUrlRepository.php | 16 ++++++++++------ module/Rest/src/Service/ApiKeyService.php | 6 +++--- 8 files changed, 27 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index 96b098f0..7193099b 100644 --- a/composer.json +++ b/composer.json @@ -62,9 +62,9 @@ }, "require-dev": { "devster/ubench": "^2.1", - "dms/phpunit-arraysubset-asserts": "^0.2.1", + "dms/phpunit-arraysubset-asserts": "^v0.3.0", "eaglewu/swoole-ide-helper": "dev-master", - "infection/infection": "^0.21.0", + "infection/infection": "^0.23.0", "phpspec/prophecy-phpunit": "^2.0", "phpstan/phpstan": "^0.12.92", "phpstan/phpstan-symfony": "^0.12.41", @@ -74,7 +74,7 @@ "shlinkio/php-coding-standard": "~2.1.1", "shlinkio/shlink-test-utils": "^2.1", "symfony/var-dumper": "^5.2", - "veewee/composer-run-parallel": "^0.1.0" + "veewee/composer-run-parallel": "^1.0" }, "autoload": { "psr-4": { @@ -113,7 +113,7 @@ ], "cs": "phpcs", "cs:fix": "phpcbf", - "stan": "phpstan analyse module/*/src module/*/config config docker/config data/migrations --level=7", + "stan": "phpstan analyse module/*/src module/*/config config docker/config data/migrations --level=8", "test": [ "@test:unit", "@test:db", diff --git a/module/Core/src/EventDispatcher/LocateVisit.php b/module/Core/src/EventDispatcher/LocateVisit.php index 046430df..bb6ba1d0 100644 --- a/module/Core/src/EventDispatcher/LocateVisit.php +++ b/module/Core/src/EventDispatcher/LocateVisit.php @@ -55,7 +55,7 @@ class LocateVisit } $isLocatable = $originalIpAddress !== null || $visit->isLocatable(); - $addr = $originalIpAddress ?? $visit->getRemoteAddr(); + $addr = $originalIpAddress ?? $visit->getRemoteAddr() ?? ''; try { $location = $isLocatable ? $this->ipLocationResolver->resolveIpLocation($addr) : Location::emptyInstance(); diff --git a/module/Core/src/Mercure/MercureUpdatesGenerator.php b/module/Core/src/Mercure/MercureUpdatesGenerator.php index c6c7a4d6..f2489da3 100644 --- a/module/Core/src/Mercure/MercureUpdatesGenerator.php +++ b/module/Core/src/Mercure/MercureUpdatesGenerator.php @@ -42,7 +42,7 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface public function newShortUrlVisitUpdate(Visit $visit): Update { $shortUrl = $visit->getShortUrl(); - $topic = sprintf('%s/%s', self::NEW_VISIT_TOPIC, $shortUrl->getShortCode()); + $topic = sprintf('%s/%s', self::NEW_VISIT_TOPIC, $shortUrl?->getShortCode()); return new Update($topic, $this->serialize([ 'shortUrl' => $this->shortUrlTransformer->transform($shortUrl), diff --git a/module/Core/src/Model/ShortUrlsParams.php b/module/Core/src/Model/ShortUrlsParams.php index b27a6187..a916704b 100644 --- a/module/Core/src/Model/ShortUrlsParams.php +++ b/module/Core/src/Model/ShortUrlsParams.php @@ -19,7 +19,7 @@ final class ShortUrlsParams private array $tags; private ShortUrlsOrdering $orderBy; private ?DateRange $dateRange; - private ?int $itemsPerPage = null; + private int $itemsPerPage; private function __construct() { diff --git a/module/Core/src/Model/Visitor.php b/module/Core/src/Model/Visitor.php index b73ed68a..9436e900 100644 --- a/module/Core/src/Model/Visitor.php +++ b/module/Core/src/Model/Visitor.php @@ -29,13 +29,16 @@ final class Visitor $this->userAgent = $this->cropToLength($userAgent, self::USER_AGENT_MAX_LENGTH); $this->referer = $this->cropToLength($referer, self::REFERER_MAX_LENGTH); $this->visitedUrl = $this->cropToLength($visitedUrl, self::VISITED_URL_MAX_LENGTH); - $this->remoteAddress = $this->cropToLength($remoteAddress, self::REMOTE_ADDRESS_MAX_LENGTH); + $this->remoteAddress = $remoteAddress === null ? null : $this->cropToLength( + $remoteAddress, + self::REMOTE_ADDRESS_MAX_LENGTH, + ); $this->potentialBot = isCrawler($userAgent); } - private function cropToLength(?string $value, int $length): ?string + private function cropToLength(string $value, int $length): string { - return $value === null ? null : substr($value, 0, $length); + return substr($value, 0, $length); } public static function fromRequest(ServerRequestInterface $request): self diff --git a/module/Core/src/Model/VisitsParams.php b/module/Core/src/Model/VisitsParams.php index 659eb5dc..1f78de00 100644 --- a/module/Core/src/Model/VisitsParams.php +++ b/module/Core/src/Model/VisitsParams.php @@ -13,7 +13,7 @@ final class VisitsParams private const FIRST_PAGE = 1; private const ALL_ITEMS = -1; - private ?DateRange $dateRange; + private DateRange $dateRange; private int $itemsPerPage; public function __construct( diff --git a/module/Core/src/Repository/ShortUrlRepository.php b/module/Core/src/Repository/ShortUrlRepository.php index c4ccedbf..4c3a4e9c 100644 --- a/module/Core/src/Repository/ShortUrlRepository.php +++ b/module/Core/src/Repository/ShortUrlRepository.php @@ -18,7 +18,6 @@ use Shlinkio\Shlink\Core\Model\ShortUrlsOrdering; use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl; use function array_column; -use function array_key_exists; use function count; use function Functional\contains; @@ -59,6 +58,7 @@ class ShortUrlRepository extends EntitySpecificationRepository implements ShortU // visitsCount and visitCount are deprecated. Only visits should work if (contains(['visits', 'visitsCount', 'visitCount'], $fieldName)) { + // FIXME This query is inefficient. Debug it. $qb->addSelect('COUNT(DISTINCT v) AS totalVisits') ->leftJoin('s.visits', 'v') ->groupBy('s') @@ -75,9 +75,11 @@ class ShortUrlRepository extends EntitySpecificationRepository implements ShortU 'dateCreated' => 'dateCreated', 'title' => 'title', ]; - if (array_key_exists($fieldName, $fieldNameMap)) { - $qb->orderBy('s.' . $fieldNameMap[$fieldName], $order); + $resolvedFieldName = $fieldNameMap[$fieldName] ?? null; + if ($resolvedFieldName !== null) { + $qb->orderBy('s.' . $resolvedFieldName, $order); } + return $qb->getQuery()->getResult(); } @@ -194,10 +196,12 @@ class ShortUrlRepository extends EntitySpecificationRepository implements ShortU private function doShortCodeIsInUse(ShortUrlIdentifier $identifier, ?Specification $spec, ?int $lockMode): bool { - $qb = $this->createFindOneQueryBuilder($identifier, $spec); - $qb->select('s.id'); + $qb = $this->createFindOneQueryBuilder($identifier, $spec)->select('s.id'); + $query = $qb->getQuery(); - $query = $qb->getQuery()->setLockMode($lockMode); + if ($lockMode !== null) { + $query = $query->setLockMode($lockMode); + } return $query->getOneOrNullResult() !== null; } diff --git a/module/Rest/src/Service/ApiKeyService.php b/module/Rest/src/Service/ApiKeyService.php index 545ff310..d66e70e2 100644 --- a/module/Rest/src/Service/ApiKeyService.php +++ b/module/Rest/src/Service/ApiKeyService.php @@ -38,12 +38,12 @@ class ApiKeyService implements ApiKeyServiceInterface private function buildApiKeyWithParams(?Chronos $expirationDate, ?string $name): ApiKey { return match (true) { - $expirationDate === null && $name === null => ApiKey::create(), $expirationDate !== null && $name !== null => ApiKey::fromMeta( ApiKeyMeta::withNameAndExpirationDate($name, $expirationDate), ), - $name === null => ApiKey::fromMeta(ApiKeyMeta::withExpirationDate($expirationDate)), - default => ApiKey::fromMeta(ApiKeyMeta::withName($name)), + $expirationDate !== null => ApiKey::fromMeta(ApiKeyMeta::withExpirationDate($expirationDate)), + $name !== null => ApiKey::fromMeta(ApiKeyMeta::withName($name)), + default => ApiKey::create(), }; }