Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ThemeCollector Symfony 7 compatibility #134

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
"@composer validate --strict",
"vendor/bin/ecs check spec src tests || true",
"vendor/bin/psalm",
"vendor/bin/rector process --dry-run"
"vendor/bin/rector process --dry-run",
"tests/Application/bin/console lint:container --env dev",
"tests/Application/bin/console lint:container --env prod"
Comment on lines +84 to +85
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stumbled now over that you also have a working console the lint:container would also catch such errors. I recommend always lint for dev and prod container as they may have different set of services.

],
"fix": [
"vendor/bin/rector process",
Expand Down
15 changes: 5 additions & 10 deletions src/Collector/ThemeCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

/**
* @property $data array{used_theme: ?ThemeInterface, used_themes: ThemeInterface[], themes: ThemeInterface[]}

*/
final class ThemeCollector extends DataCollector
{
private ThemeRepositoryInterface $themeRepository;
Expand All @@ -29,15 +33,6 @@ final class ThemeCollector extends DataCollector

private ThemeHierarchyProviderInterface $themeHierarchyProvider;

/**
* @var array
*
* @psalm-var array{used_theme: ?ThemeInterface, used_themes: ThemeInterface[], themes: ThemeInterface[]}
*
* @psalm-suppress NonInvariantDocblockPropertyType
*/
protected $data;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is defined by DataCollector itself. With beginning of Symfony 7 it has a Union Type here: https://github.com/symfony/symfony/blob/1a16ebc32598faada074e0af12a6a698d2964a5e/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php#L31

For BC reasons best is to move this variable to PHPDoc definition. So we can support Symfony 7 and previous versions of the parent collector class.


public function __construct(
ThemeRepositoryInterface $themeRepository,
ThemeContextInterface $themeContext,
Expand All @@ -60,7 +55,7 @@ public function getUsedTheme(): ?ThemeInterface
}

/**
* @return array|ThemeInterface[]
* @return ThemeInterface[]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see phpdoc above

*/
public function getUsedThemes(): array
{
Expand Down
54 changes: 54 additions & 0 deletions tests/Collector/ThemeCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ThemeBundle\Tests\Collector;

use PHPUnit\Framework\TestCase;
use Sylius\Bundle\ThemeBundle\Collector\ThemeCollector;
use Sylius\Bundle\ThemeBundle\Context\EmptyThemeContext;
use Sylius\Bundle\ThemeBundle\HierarchyProvider\NoopThemeHierarchyProvider;
use Sylius\Bundle\ThemeBundle\Loader\ThemeLoaderInterface;
use Sylius\Bundle\ThemeBundle\Repository\InMemoryThemeRepository;

class ThemeCollectorTest extends TestCase
{
/**
* @test
*/
public function it_has_no_used_theme(): void
{
$themeCollector = $this->createThemeCollector();

$this->assertNull($themeCollector->getUsedTheme());
}

private function createThemeCollector(): ThemeCollector
{
$themeLoader = new class () implements ThemeLoaderInterface {
public function load(): array
{
return [];
}
};

$themeRepository = new InMemoryThemeRepository($themeLoader);
$themeContext = new EmptyThemeContext();
$themeHierarchyProvider = new NoopThemeHierarchyProvider();

return new ThemeCollector(
$themeRepository,
$themeContext,
$themeHierarchyProvider,
);
}
}