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

Refactor normalization methods for consistency and clarity. #257

Merged
merged 4 commits into from
Dec 16, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
php-version:
- "8.2"
- "8.3"
- "8.4"
dependencies:
- "highest"
runs-on: ${{ matrix.operating-system }}
Expand Down
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@
"require-dev": {
"ext-sockets": "*",
"dbrekelmans/bdi": "^1.1",
"ekino/phpstan-banned-code": "^1.0",
"ekino/phpstan-banned-code": "^1.0|^2.0|^3.0",
"ergebnis/phpunit-slow-test-detector": "^2.14",
"infection/infection": "^0.28",
"infection/infection": "^0.28|^0.29",
"matthiasnoback/symfony-config-test": "^5.1",
"php-parallel-lint/php-parallel-lint": "^1.4",
"phpstan/extension-installer": "^1.1",
"phpstan/phpdoc-parser": "^1.28",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-beberlei-assert": "^1.0",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.4",
"phpstan/phpstan-strict-rules": "^1.0",
"phpstan/phpstan-symfony": "^1.4",
"phpstan/phpdoc-parser": "^1.28|^2.0",
"phpstan/phpstan": "^1.0|^2.0",
"phpstan/phpstan-beberlei-assert": "^1.0|^2.0",
"phpstan/phpstan-deprecation-rules": "^1.0|^2.0",
"phpstan/phpstan-phpunit": "^1.4|^2.0",
"phpstan/phpstan-strict-rules": "^1.0|^2.0",
"phpstan/phpstan-symfony": "^1.4|^2.0",
"phpunit/phpunit": "^10.1|^11.0",
"rector/rector": "^1.0",
"staabm/phpstan-todo-by": "^0.1.27",
"struggle-for-php/sfp-phpstan-psr-log": "^0.21.0",
"rector/rector": "^1.0|^2.0",
"staabm/phpstan-todo-by": "^0.1.27|^0.2",
"struggle-for-php/sfp-phpstan-psr-log": "^0.21.0|^0.22|^0.23",
"symfony/filesystem": "^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/mime": "^6.4|^7.0",
Expand Down
1,032 changes: 910 additions & 122 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
parameters:
level: max
paths:
- src
- tests
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
checkUninitializedProperties: true
treatPhpDocTypesAsCertain: false
scanFiles:
- vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php
level: max
paths:
- src
- tests

banned_code:
non_ignorable: false
checkUninitializedProperties: true
treatPhpDocTypesAsCertain: false
scanFiles:
- vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php

includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
- phpstan-baseline.neon
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
- phpstan-baseline.neon
1 change: 1 addition & 0 deletions src/CachingStrategy/AssetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\SerializerInterface;
use function count;
use function sprintf;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
Expand Down
1 change: 1 addition & 0 deletions src/CachingStrategy/ImageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use function sprintf;

final class ImageCache implements HasCacheStrategiesInterface, CanLogInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/CachingStrategy/ManifestCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use function sprintf;

final class ManifestCache implements HasCacheStrategiesInterface, CanLogInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/CachingStrategy/PreloadUrlsGeneratorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
use function array_key_exists;
use function sprintf;

final class PreloadUrlsGeneratorManager implements CanLogInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/CachingStrategy/ResourceCaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\SerializerInterface;
use function count;
use function sprintf;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
Expand Down
1 change: 1 addition & 0 deletions src/CachingStrategy/WorkboxCacheStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use SpomkyLabs\PwaBundle\WorkboxPlugin\CachePluginInterface;
use function in_array;
use function sprintf;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
Expand Down
1 change: 1 addition & 0 deletions src/Command/CreateIconsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Yaml\Yaml;
use function is_string;
use function sprintf;

#[AsCommand(name: 'pwa:create:icons', description: 'Generate icons for your PWA')]
final class CreateIconsCommand extends Command
Expand Down
1 change: 1 addition & 0 deletions src/Command/CreateScreenshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function assert;
use function is_int;
use function is_string;
use function sprintf;

#[AsCommand(
name: 'pwa:create:screenshot',
Expand Down
1 change: 1 addition & 0 deletions src/CompilerPass/PreloadUrlCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Throwable;
use function array_key_exists;
use function is_string;
use function sprintf;

/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion src/DataCollector/PwaCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
) {
}

public function collect(Request $request, Response $response, Throwable $exception = null): void
public function collect(Request $request, Response $response, ?Throwable $exception = null): void
{
$jsonOptions = [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
Expand Down
1 change: 1 addition & 0 deletions src/ImageProcessor/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use Stringable;
use function sprintf;

final readonly class Configuration implements Stringable
{
Expand Down
9 changes: 4 additions & 5 deletions src/ImageProcessor/GDImageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ private function createMainImage(string $image, Configuration $configuration): G
}

/*if ($configuration->width === $configuration->height) {
$mainImage = imagescale($mainImage, $configuration->width, $configuration->height);
assert($mainImage !== false);

return $mainImage;
}*/
* $mainImage = imagescale($mainImage, $configuration->width, $configuration->height);
* assert($mainImage !== false);
* return $mainImage;
* }*/

$srcWidth = imagesx($mainImage);
$srcHeight = imagesy($mainImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use function sprintf;

final class DestinationMatchCallbackHandler implements MatchCallbackHandlerInterface, CanLogInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use function sprintf;

final class ExactPathnameMatchCallbackHandler implements MatchCallbackHandlerInterface, CanLogInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/MatchCallbackHandler/OriginMatchCallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use function sprintf;

final class OriginMatchCallbackHandler implements MatchCallbackHandlerInterface, CanLogInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use function sprintf;

final class PathnameEndsWithMatchCallbackHandler implements MatchCallbackHandlerInterface, CanLogInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use function sprintf;

final class PathnameStartsWithMatchCallbackHandler implements MatchCallbackHandlerInterface, CanLogInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/MatchCallbackHandler/RouteMatchCallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use Symfony\Component\Routing\RouterInterface;
use function sprintf;

final class RouteMatchCallbackHandler implements MatchCallbackHandlerInterface, CanLogInterface
{
Expand Down
16 changes: 8 additions & 8 deletions src/Normalizer/AssetNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,37 @@ public function __construct(
/**
* @return array{src: string, sizes?: string, form_factor?: string, label?: string, platform?: string, format?: string}
*/
public function normalize(mixed $object, string $format = null, array $context = []): string
public function normalize(mixed $data, ?string $format = null, array $context = []): string
{
assert($object instanceof Asset);
assert($data instanceof Asset);
$url = null;
if (! str_starts_with($object->src, '/')) {
$asset = $this->assetMapper->getAsset($object->src);
if (! str_starts_with($data->src, '/')) {
$asset = $this->assetMapper->getAsset($data->src);
$url = $asset?->publicPath;
}
if ($url === null) {
$url = $object->src;
$url = $data->src;
}

return $url;
}

public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
{
assert(is_string($data));

return Asset::create($data);
}

public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof Asset;
}

public function supportsDenormalization(
mixed $data,
string $type,
string $format = null,
?string $format = null,
array $context = []
): bool {
return $type === Asset::class;
Expand Down
21 changes: 10 additions & 11 deletions src/Normalizer/IconNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public function __construct(
/**
* @return array{src: string, sizes?: string, type?: string, purpose?: string}
*/
public function normalize(mixed $object, string $format = null, array $context = []): array
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
assert($object instanceof Icon);
$imageType = $object->type;
if (! str_starts_with($object->src->src, '/')) {
$asset = $this->assetMapper->getAsset($object->src->src);
assert($data instanceof Icon);
$imageType = $data->type;
if (! str_starts_with($data->src->src, '/')) {
$asset = $this->assetMapper->getAsset($data->src->src);
$imageType = $this->getType($asset);
}

$result = [
'src' => $this->normalizer->normalize($object->src, $format, $context),
'sizes' => $object->getSizeList(),
'src' => $this->normalizer->normalize($data->src, $format, $context),
'sizes' => $data->getSizeList(),
'type' => $imageType,
'purpose' => $object->purpose,
'purpose' => $data->purpose,
];

$cleanup = static fn (array $data): array => array_filter(
Expand All @@ -49,7 +49,7 @@ public function normalize(mixed $object, string $format = null, array $context =
return $cleanup($result);
}

public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof Icon;
}
Expand All @@ -70,7 +70,6 @@ private function getType(?MappedAsset $asset): ?string
return null;
}

$mime = MimeTypes::getDefault();
return $mime->guessMimeType($asset->sourcePath);
return MimeTypes::getDefault()->guessMimeType($asset->sourcePath);
}
}
21 changes: 11 additions & 10 deletions src/Normalizer/ScreenshotNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function assert;
use function sprintf;

final class ScreenshotNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
Expand All @@ -27,23 +28,23 @@ public function __construct(
/**
* @return array{src: string, sizes?: string, form_factor?: string, label?: string, platform?: string, format?: string}
*/
public function normalize(mixed $object, string $format = null, array $context = []): array
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
assert($object instanceof Screenshot);
assert($data instanceof Screenshot);
$asset = null;
$imageType = $object->type;
if ($imageType === null && ! str_starts_with($object->src->src, '/')) {
$asset = $this->assetMapper->getAsset($object->src->src);
$imageType = $data->type;
if ($imageType === null && ! str_starts_with($data->src->src, '/')) {
$asset = $this->assetMapper->getAsset($data->src->src);
$imageType = $this->getType($asset);
}
['sizes' => $sizes, 'formFactor' => $formFactor] = $this->getSizes($object, $asset);
['sizes' => $sizes, 'formFactor' => $formFactor] = $this->getSizes($data, $asset);

$result = [
'src' => $this->normalizer->normalize($object->src, $format, $context),
'src' => $this->normalizer->normalize($data->src, $format, $context),
'sizes' => $sizes,
'form_factor' => $formFactor,
'label' => $object->label,
'platform' => $object->platform,
'label' => $data->label,
'platform' => $data->platform,
'format' => $imageType,
];

Expand All @@ -54,7 +55,7 @@ public function normalize(mixed $object, string $format = null, array $context =
return $cleanup($result);
}

public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof Screenshot;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Normalizer/ServiceWorkerNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
/**
* @return array{scope?: string, src: string, use_cache?: bool}
*/
public function normalize(mixed $object, string $format = null, array $context = []): array
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
assert($object instanceof ServiceWorker);
assert($data instanceof ServiceWorker);

$result = [
'src' => '/' . trim($object->dest, '/'),
'scope' => $object->scope,
'use_cache' => $object->useCache,
'src' => '/' . trim($data->dest, '/'),
'scope' => $data->scope,
'use_cache' => $data->useCache,
];

$cleanup = static fn (array $data): array => array_filter(
Expand All @@ -30,7 +30,7 @@ public function normalize(mixed $object, string $format = null, array $context =
return $cleanup($result);
}

public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof ServiceWorker;
}
Expand Down
Loading
Loading