Skip to content

Commit

Permalink
Refactor normalization methods for consistency and clarity.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Spomky committed Dec 15, 2024
1 parent f03a2ef commit 7b353bb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 42 deletions.
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
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
14 changes: 7 additions & 7 deletions src/Normalizer/IconNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ 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);
$icon = $this->iconResolver->getIcon($object);
$imageType = $this->iconResolver->getType($object->type, $icon->url);
assert($data instanceof Icon);
$icon = $this->iconResolver->getIcon($data);
$imageType = $this->iconResolver->getType($data->type, $icon->url);

$result = [
'src' => $icon->url,
'sizes' => $object->getSizeList(),
'sizes' => $data->getSizeList(),
'type' => $imageType,
'purpose' => $object->purpose,
'purpose' => $data->purpose,
];

$cleanup = static fn (array $data): array => array_filter(
Expand All @@ -44,7 +44,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 Down
23 changes: 11 additions & 12 deletions src/Normalizer/ScreenshotNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,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 @@ -55,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 Expand Up @@ -105,8 +105,7 @@ private function getType(?MappedAsset $asset): ?string
return null;
}

$mime = MimeTypes::getDefault();
return $mime->guessMimeType($asset->sourcePath);
return MimeTypes::getDefault()->guessMimeType($asset->sourcePath);
}

private function getFormFactor(?int $width, ?int $height): ?string
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
16 changes: 8 additions & 8 deletions src/Normalizer/UrlNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ public function __construct(
) {
}

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 Url);
assert($data instanceof Url);

// If the path is a valid URL, we return it directly
if (str_starts_with($object->path, '/') && filter_var($object->path, FILTER_VALIDATE_URL) !== false) {
return $object->path;
if (str_starts_with($data->path, '/') && filter_var($data->path, FILTER_VALIDATE_URL) !== false) {
return $data->path;
}

// If the path is an asset, we return the public path
$asset = $this->assetMapper->getAsset($object->path);
$asset = $this->assetMapper->getAsset($data->path);
if ($asset !== null) {
return $asset->publicPath;
}

// Otherwise, we try to generate the URL
try {
return $this->router->generate($object->path, $object->params, $object->pathTypeReference);
return $this->router->generate($data->path, $data->params, $data->pathTypeReference);
} catch (Throwable) {
// If the URL cannot be generated, we return the path as is
return $object->path;
return $data->path;
}
}

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 Url;
}
Expand Down

0 comments on commit 7b353bb

Please sign in to comment.