Increased phpstan level to 8

This commit is contained in:
Alejandro Celaya 2021-07-20 12:51:07 +02:00
parent 2eeb762cd9
commit 95770ac104
8 changed files with 27 additions and 20 deletions

View file

@ -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",

View file

@ -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();

View file

@ -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),

View file

@ -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()
{

View file

@ -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

View file

@ -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(

View file

@ -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;
}

View file

@ -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(),
};
}