Skip to content

Commit f84e4ba

Browse files
dermatzCopilot
andcommitted
🐛 fix(theme): type inheritance chain with ThemeAncestor value object
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent cc26b34 commit f84e4ba

4 files changed

Lines changed: 36 additions & 15 deletions

File tree

‎src/Console/Command/AbstractCommand.php‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Laravel\Prompts\ConfirmPrompt;
88
use Laravel\Prompts\SelectPrompt;
99
use Magento\Framework\Console\Cli;
10+
use OpenForgeProject\MageForge\Model\ThemeAncestor;
1011
use OpenForgeProject\MageForge\Model\ThemeList;
1112
use OpenForgeProject\MageForge\Service\ThemeSuggester;
1213
use Symfony\Component\Console\Command\Command;
@@ -223,7 +224,7 @@ protected function addParentThemeOptions(): void
223224
* @param OutputInterface $output
224225
* @param string $label
225226
* @param array<string> $parents
226-
* @param array<mixed> $chains
227+
* @param array<array<ThemeAncestor>> $chains
227228
* @return bool
228229
*/
229230
protected function confirmParentThemes(
@@ -269,23 +270,23 @@ protected function confirmParentThemes(
269270
* Render a theme and its ancestors as an indented tree, child on top.
270271
*
271272
* @param string $themeCode
272-
* @param array<mixed> $chain
273+
* @param array<ThemeAncestor> $chain
273274
* @return void
274275
*/
275276
protected function renderInheritanceTree(string $themeCode, array $chain): void
276277
{
277278
$this->io->newLine();
278279
$this->io->writeln(sprintf(' <fg=cyan;options=bold>%s</> <fg=gray>(selected)</>', $themeCode));
279280

280-
foreach ($chain as $level => $ancestor) {
281+
foreach (array_values($chain) as $level => $ancestor) {
281282
$indent = str_repeat(' ', $level);
282-
$status = $ancestor['skipReason'] === null
283+
$status = $ancestor->skipReason === null
283284
? '<fg=green>will be built</>'
284-
: sprintf('<fg=gray>skipped: %s</>', $ancestor['skipReason']);
285+
: sprintf('<fg=gray>skipped: %s</>', $ancestor->skipReason);
285286
$this->io->writeln(sprintf(
286287
' %s└─ <fg=yellow>%s</> <fg=gray>(parent)</> · %s',
287288
$indent,
288-
$ancestor['code'],
289+
$ancestor->code,
289290
$status,
290291
));
291292
}

‎src/Model/ThemeAncestor.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenForgeProject\MageForge\Model;
6+
7+
/**
8+
* An ancestor in a theme's inheritance chain and why it is not built (if applicable)
9+
*/
10+
class ThemeAncestor
11+
{
12+
/**
13+
* @param string $code
14+
* @param string|null $skipReason
15+
*/
16+
public function __construct(
17+
public readonly string $code,
18+
public readonly ?string $skipReason = null,
19+
) {
20+
}
21+
}

‎src/Service/ThemeParentResolver.php‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace OpenForgeProject\MageForge\Service;
66

7+
use OpenForgeProject\MageForge\Model\ThemeAncestor;
78
use OpenForgeProject\MageForge\Model\ThemeList;
89
use OpenForgeProject\MageForge\Model\ThemePath;
910
use OpenForgeProject\MageForge\Service\ThemeBuilder\BuilderPool;
@@ -38,8 +39,8 @@ public function getBuildableParents(string $themeCode): array
3839
{
3940
$parents = [];
4041
foreach ($this->getInheritanceChain($themeCode) as $ancestor) {
41-
if ($ancestor['skipReason'] === null) {
42-
array_unshift($parents, $ancestor['code']);
42+
if ($ancestor->skipReason === null) {
43+
array_unshift($parents, $ancestor->code);
4344
}
4445
}
4546

@@ -50,7 +51,7 @@ public function getBuildableParents(string $themeCode): array
5051
* Get all ancestors of a theme, direct parent first, with the reason a build is skipped (if any).
5152
*
5253
* @param string $themeCode
53-
* @return array<array{code: string, skipReason: ?string}>
54+
* @return array<ThemeAncestor>
5455
*/
5556
public function getInheritanceChain(string $themeCode): array
5657
{
@@ -80,7 +81,7 @@ public function getInheritanceChain(string $themeCode): array
8081
$this->builderPool->getBuilder($path) === null => 'no builder',
8182
default => null,
8283
};
83-
$chain[] = ['code' => $code, 'skipReason' => $skipReason];
84+
$chain[] = new ThemeAncestor($code, $skipReason);
8485

8586
$parent = $parent->getParentTheme();
8687
}

‎tests/Unit/Service/ThemeParentResolverTest.php‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OpenForgeProject\MageForge\Test\Unit\Service;
66

77
use Magento\Framework\View\Design\ThemeInterface;
8+
use OpenForgeProject\MageForge\Model\ThemeAncestor;
89
use OpenForgeProject\MageForge\Model\ThemeList;
910
use OpenForgeProject\MageForge\Model\ThemePath;
1011
use OpenForgeProject\MageForge\Service\ThemeBuilder\BuilderInterface;
@@ -74,11 +75,8 @@ public function testSkipsVendorThemes(): void
7475
$resolver = new ThemeParentResolver($themeList, $themePath, $builderPool);
7576

7677
$this->assertSame(['App/base'], $resolver->getBuildableParents('App/child'));
77-
$this->assertSame(
78-
[
79-
['code' => 'Hyva/default', 'skipReason' => 'vendor'],
80-
['code' => 'App/base', 'skipReason' => null],
81-
],
78+
$this->assertEquals(
79+
[new ThemeAncestor('Hyva/default', 'vendor'), new ThemeAncestor('App/base')],
8280
$resolver->getInheritanceChain('App/child'),
8381
);
8482
}

0 commit comments

Comments
 (0)