Skip to content

Commit

Permalink
Refactor normalization methods for consistency and clarity. (#257)
Browse files Browse the repository at this point in the history
* Refactor normalization methods for consistency and clarity.

Updated method parameter names from `$object` to `$data` across multiple normalizers to align with coding standards. Improved nullable type hints and applied minor cleanups for better readability and maintainability.

* Update dependencies and clean up PHPStan baseline

Add support for newer versions of development dependencies in `composer.json` by updating version constraints. Remove resolved deprecation issues from the PHPStan baseline to maintain a cleaner codebase.

* Update test assertions and refine PHPStan configurations

Updated file path assertions in `CompileCommandTest` to align with new screenshot file naming conventions. Enhanced PHPStan configuration by adding banned code settings, adjusting indentation, and significantly expanding the baseline for ignored errors.

* Refactor CompileCommandTest and update PHPStan configurations

Remove redundant assertions in CompileCommandTest for improved clarity and tweak PHPStan to allow ignorable banned code. Added new rules to PHPStan baseline for managing specific edge cases in code analysis.
Spomky authored Dec 16, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a699a9e commit c24711e
Showing 40 changed files with 1,022 additions and 204 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
@@ -70,6 +70,7 @@ jobs:
php-version:
- "8.2"
- "8.3"
- "8.4"
dependencies:
- "highest"
runs-on: ${{ matrix.operating-system }}
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
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
@@ -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;
1 change: 1 addition & 0 deletions src/CachingStrategy/ImageCache.php
Original file line number Diff line number Diff line change
@@ -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
{
1 change: 1 addition & 0 deletions src/CachingStrategy/ManifestCache.php
Original file line number Diff line number Diff line change
@@ -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
{
1 change: 1 addition & 0 deletions src/CachingStrategy/PreloadUrlsGeneratorManager.php
Original file line number Diff line number Diff line change
@@ -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
{
1 change: 1 addition & 0 deletions src/CachingStrategy/ResourceCaches.php
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/CachingStrategy/WorkboxCacheStrategy.php
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/Command/CreateIconsCommand.php
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/Command/CreateScreenshotCommand.php
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
use function assert;
use function is_int;
use function is_string;
use function sprintf;

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

/**
* @internal
2 changes: 1 addition & 1 deletion src/DataCollector/PwaCollector.php
Original file line number Diff line number Diff line change
@@ -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,
1 change: 1 addition & 0 deletions src/ImageProcessor/Configuration.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

use InvalidArgumentException;
use Stringable;
use function sprintf;

final readonly class Configuration implements Stringable
{
9 changes: 4 additions & 5 deletions src/ImageProcessor/GDImageProcessor.php
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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
{
Original file line number Diff line number Diff line change
@@ -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
{
1 change: 1 addition & 0 deletions src/MatchCallbackHandler/OriginMatchCallbackHandler.php
Original file line number Diff line number Diff line change
@@ -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
{
Original file line number Diff line number Diff line change
@@ -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
{
Original file line number Diff line number Diff line change
@@ -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
{
1 change: 1 addition & 0 deletions src/MatchCallbackHandler/RouteMatchCallbackHandler.php
Original file line number Diff line number Diff line change
@@ -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
{
16 changes: 8 additions & 8 deletions src/Normalizer/AssetNormalizer.php
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 10 additions & 11 deletions src/Normalizer/IconNormalizer.php
Original file line number Diff line number Diff line change
@@ -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(
@@ -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;
}
@@ -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
@@ -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
{
@@ -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,
];

@@ -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;
}
12 changes: 6 additions & 6 deletions src/Normalizer/ServiceWorkerNormalizer.php
Original file line number Diff line number Diff line change
@@ -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(
@@ -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;
}
Loading

0 comments on commit c24711e

Please sign in to comment.