Skip to content

Commit

Permalink
[Twig Hooks] Fix PHPStan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Apr 29, 2024
1 parent 6b0a93a commit 4f00f19
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/TwigHooks/src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private function addHooksConfiguration(ArrayNodeDefinition $rootNode): void
$isDisabled => 'disabled',
$isComponentDefined => 'component',
$isTemplateDefined => 'template',
default => 'undefined',
};

return $v;
Expand Down
2 changes: 2 additions & 0 deletions src/TwigHooks/src/Hookable/AbstractHookable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ abstract class AbstractHookable

public readonly string $name;

/** @var array<string, mixed> */
public readonly array $context;

/** @var array<string, mixed> */
public readonly array $configuration;

private readonly ?int $priority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function hydrate(mixed $value, string $className): ?object

public function dehydrate(object $object): mixed
{
if (!$object instanceof DataBagInterface) {
throw new \InvalidArgumentException(sprintf('Object must implement %s', DataBagInterface::class));
}

return $object->all();
}
}
15 changes: 13 additions & 2 deletions src/TwigHooks/src/Profiler/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sylius\TwigHooks\Profiler\Dumper;

use Sylius\TwigHooks\Hookable\AbstractHookable;
use Sylius\TwigHooks\Hookable\HookableComponent;
use Sylius\TwigHooks\Hookable\HookableTemplate;
use Sylius\TwigHooks\Profiler\HookableProfile;
Expand Down Expand Up @@ -50,9 +51,10 @@ private function dumpHookProfile(HookProfile $hookProfile, string $prefix = '',

private function dumpHookableProfile(HookableProfile $hookableProfile, string $prefix = '', bool $sibling = false): string
{
list($targetName, $targetValue) = match (get_class($hookableProfile->getHookable())) {
$targetName = match (get_class($hookableProfile->getHookable())) {
HookableTemplate::class => 'Template',
HookableComponent::class => 'Component',
default => throw new \InvalidArgumentException(sprintf('Unsupported hookable type %s', get_class($hookableProfile->getHookable()))),
};

$str = sprintf(
Expand All @@ -62,7 +64,7 @@ private function dumpHookableProfile(HookableProfile $hookableProfile, string $p
$hookableProfile->getHookable()->priority(),
$hookableProfile->getDuration(),
$hookableProfile->getName(),
$targetValue,
$this->getTargetValue($hookableProfile->getHookable()),
);
$str .= PHP_EOL;
$prefix .= $sibling ? '' : ' ';
Expand All @@ -74,4 +76,13 @@ private function dumpHookableProfile(HookableProfile $hookableProfile, string $p

return $str;
}

private function getTargetValue(AbstractHookable $hookable): string
{
return match (get_class($hookable)) {
HookableTemplate::class => $hookable->template,
HookableComponent::class => $hookable->component,
default => throw new \InvalidArgumentException(sprintf('Unsupported hookable type %s', get_class($hookable))),
};
}
}

0 comments on commit 4f00f19

Please sign in to comment.