Skip to content

Commit

Permalink
Fix static analysis (#1729)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 29, 2023
1 parent 83b834a commit 14a3bdb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
42 changes: 24 additions & 18 deletions src/Entity/Transformer.php
Expand Up @@ -145,14 +145,12 @@ public function create(PageInterface $page, ?SnapshotInterface $snapshot = null)
'parent_id' => $parent?->getId(),
'blocks' => $blocks,
];
// need to filter out null values

/**
* @var PageContent $content
*/
$content = array_filter($data, static fn ($v) => null !== $v);
// need to filter out null values
$content = array_filter($data, static fn ($v): bool => null !== $v);
}

/** @psalm-suppress ArgumentTypeCoercion */
$snapshot->setContent($content);

return $snapshot;
Expand Down Expand Up @@ -192,7 +190,7 @@ public function load(SnapshotInterface $snapshot): PageInterface
if (!method_exists($page, 'removeBlock')) {
@trigger_error('Not implementing a `PageInterface::removeBlock` method is deprecated since 4.x and will throw an error in 5.0.', \E_USER_DEPRECATED);
}
$page->setId($content['id']);
$page->setId($content['id'] ?? null);
$page->setJavascript($content['javascript'] ?? null);
$page->setStylesheet($content['stylesheet'] ?? null);
$page->setRawHeaders($content['raw_headers'] ?? null);
Expand All @@ -205,13 +203,17 @@ public function load(SnapshotInterface $snapshot): PageInterface
$page->setTemplateCode($content['template_code'] ?? null);
$page->setRequestMethod($content['request_method'] ?? null);

$createdAt = new \DateTime();
$createdAt->setTimestamp((int) $content['created_at']);
$page->setCreatedAt($createdAt);
if (isset($content['created_at'])) {
$createdAt = new \DateTime();
$createdAt->setTimestamp((int) $content['created_at']);
$page->setCreatedAt($createdAt);
}

$updatedAt = new \DateTime();
$updatedAt->setTimestamp((int) $content['updated_at']);
$page->setUpdatedAt($updatedAt);
if (isset($content['updated_at'])) {
$updatedAt = new \DateTime();
$updatedAt->setTimestamp((int) $content['updated_at']);
$page->setUpdatedAt($updatedAt);
}
}

return $page;
Expand Down Expand Up @@ -263,13 +265,17 @@ public function loadBlock(array $content, PageInterface $page): PageBlockInterfa
$block->setType($content['type']);
}

$createdAt = new \DateTime();
$createdAt->setTimestamp((int) $content['created_at']);
$block->setCreatedAt($createdAt);
if (isset($content['created_at'])) {
$createdAt = new \DateTime();
$createdAt->setTimestamp((int) $content['created_at']);
$block->setCreatedAt($createdAt);
}

$updatedAt = new \DateTime();
$updatedAt->setTimestamp((int) $content['updated_at']);
$block->setUpdatedAt($updatedAt);
if (isset($content['updated_at'])) {
$updatedAt = new \DateTime();
$updatedAt->setTimestamp((int) $content['updated_at']);
$block->setUpdatedAt($updatedAt);
}

/**
* @phpstan-var BlockContent $child
Expand Down
24 changes: 12 additions & 12 deletions src/Model/TransformerInterface.php
Expand Up @@ -20,33 +20,33 @@
*
* NEXT_MAJOR: Restrict and simplify BlockContent type:
* - position: int|null
* - enabled: boolean
* - enabled: bool
*
* @phpstan-type BlockContent array{
* id: int|string|null,
* id?: int|string|null,
* name?: string|null,
* enabled: boolean|'1'|'0',
* enabled: bool|'1'|'0',
* position: int|string|null,
* settings: array<string, mixed>,
* type: string|null,
* created_at: int|numeric-string|null,
* updated_at: int|numeric-string|null,
* created_at?: int|numeric-string|null,
* updated_at?: int|numeric-string|null,
* parent_id?: int|string|null,
* blocks: array<array{
* id: int|string|null,
* id?: int|string|null,
* name?: string|null,
* enabled: boolean|'1'|'0',
* enabled: bool|'1'|'0',
* position: int|string|null,
* settings: array<string, mixed>,
* type: string|null,
* created_at: int|numeric-string|null,
* updated_at: int|numeric-string|null,
* created_at?: int|numeric-string|null,
* updated_at?: int|numeric-string|null,
* parent_id?: int|string|null,
* blocks: array<mixed>,
* }>,
* }
* @phpstan-type PageContent array{
* id: int|string|null,
* id?: int|string|null,
* parent_id?: int|string|null,
* javascript?: string|null,
* stylesheet?: string|null,
Expand All @@ -58,8 +58,8 @@
* slug?: string|null,
* template_code?: string|null,
* request_method?: string|null,
* created_at: int|numeric-string|null,
* updated_at: int|numeric-string|null,
* created_at?: int|numeric-string|null,
* updated_at?: int|numeric-string|null,
* blocks: array<BlockContent>,
* }
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Entity/TransformerTest.php
Expand Up @@ -166,7 +166,7 @@ public function testLoadBlock(array $content): void

$block = $this->transformer->loadBlock($content, new SonataPagePage());

static::assertSame($content['id'], $block->getId());
static::assertSame($content['id'] ?? null, $block->getId());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Entity/TransformerTest.php
Expand Up @@ -396,7 +396,7 @@ protected function getTestContent(\DateTimeInterface $datetime, int|string|null
}

/**
* @param array<string, ?mixed> $settings
* @param array<string, mixed> $settings
*
* @return BlockContent
*/
Expand Down

0 comments on commit 14a3bdb

Please sign in to comment.