Skip to content

Commit

Permalink
run rector
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Feb 7, 2025
1 parent 513dc0f commit 9817937
Show file tree
Hide file tree
Showing 22 changed files with 48 additions and 158 deletions.
9 changes: 5 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/contao',
])
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
# In Vorbereitung für PHP 8.4:
// ExplicitNullableParamTypeRector::class
ExplicitNullableParamTypeRector::class
])

->withImportNames(importShortClasses: false, removeUnusedImports: true)
->withSets([
LevelSetList::UP_TO_PHP_74,
SymfonySetList::SYMFONY_44,
LevelSetList::UP_TO_PHP_81,
SymfonySetList::SYMFONY_54,
SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
# Erst mit Symfony 6 (Contao 5) nutzen:
// SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
ContaoLevelSetList::UP_TO_CONTAO_49,
ContaoLevelSetList::UP_TO_CONTAO_413,
ContaoSetList::FQCN,
ContaoSetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
5 changes: 1 addition & 4 deletions src/Asset/EntrypointCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@

class EntrypointCollectionFactory
{
private EntryCollection $entryCollection;

/**
* EntrypointCollectionFactory constructor.
*/
public function __construct(EntryCollection $entryCollection)
public function __construct(private readonly EntryCollection $entryCollection)
{
$this->entryCollection = $entryCollection;
}

public function createCollection(array $entrypoints): EntrypointCollection
Expand Down
2 changes: 1 addition & 1 deletion src/Asset/FrontendAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FrontendAsset
/**
* Add an active entrypoint.
*/
public function addActiveEntrypoint(string $entrypoint)
public function addActiveEntrypoint(string $entrypoint): void
{
$this->activeEntrypoints[] = $entrypoint;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Asset/GlobalContaoAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@

class GlobalContaoAsset
{
private array $bundleConfig;
private ExtensionCollection $extensionCollection;

public function __construct(array $bundleConfig, ExtensionCollection $extensionCollection)
public function __construct(private array $bundleConfig, private readonly ExtensionCollection $extensionCollection)
{
$this->bundleConfig = $bundleConfig;
$this->extensionCollection = $extensionCollection;
}

public function cleanGlobalArrayFromConfiguration(): void
Expand Down
9 changes: 1 addition & 8 deletions src/Asset/PageEntrypoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ class PageEntrypoints

protected $initialized = false;

private FrontendAsset $frontendAsset;
private EntryCollection $entryCollection;
private Utils $utils;

/**
* PageEntrypoints constructor.
*/
public function __construct(FrontendAsset $frontendAsset, EntryCollection $entryCollection, Utils $utils)
public function __construct(private readonly FrontendAsset $frontendAsset, private readonly EntryCollection $entryCollection, private readonly Utils $utils)
{
$this->frontendAsset = $frontendAsset;
$this->entryCollection = $entryCollection;
$this->utils = $utils;
}

public function generatePageEntrypoints(PageModel $page, LayoutModel $layout, ?string $encoreField = null): bool
Expand Down
28 changes: 3 additions & 25 deletions src/Asset/TemplateAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@

class TemplateAsset
{
/**
* @var Environment
*/
private $twig;
/**
* @var LayoutModel
*/
private $layout;
/**
* @var PageEntrypoints
*/
private $pageEntrypoints;
/**
* @var PageModel
*/
Expand All @@ -44,21 +36,9 @@ class TemplateAsset
* @var array
*/
private $templateData;
/**
* @var array
*/
private $bundleConfig;
/**
* @var string
*/
private $webDir;

public function __construct(array $bundleConfig, string $webDir, Environment $twig, PageEntrypoints $pageEntrypoints)
public function __construct(private array $bundleConfig, private readonly string $webDir, private readonly Environment $twig, private PageEntrypoints $pageEntrypoints)
{
$this->twig = $twig;
$this->pageEntrypoints = $pageEntrypoints;
$this->bundleConfig = $bundleConfig;
$this->webDir = $webDir;
}

public function createInstance(PageModel $pageModel, LayoutModel $layoutModel, ?string $entriesField = null): self
Expand All @@ -69,7 +49,7 @@ public function createInstance(PageModel $pageModel, LayoutModel $layoutModel, ?
return $instance;
}

public function initialize(PageModel $pageModel, LayoutModel $layoutModel, ?string $entriesField = null)
public function initialize(PageModel $pageModel, LayoutModel $layoutModel, ?string $entriesField = null): void
{
$this->page = $pageModel;
$this->layout = $layoutModel;
Expand Down Expand Up @@ -135,9 +115,7 @@ public function inlineCssLinkTag()
preg_match_all('@<link rel="stylesheet" href="([^"]+)">@i', $styleTags, $matches);

if (isset($matches[1]) && !empty($matches[1])) {

Check failure on line 117 in src/Asset/TemplateAsset.php

View workflow job for this annotation

GitHub Actions / phpstan

Offset 1 on array{array<int, string>, array<int, non-empty-string>} in isset() always exists and is not nullable.
$inlineCss = implode("\n", array_map(function ($path) {
return file_get_contents($this->webDir.preg_replace('@<link rel="stylesheet" href="([^"]+)">@i', '$1', $path));
}, $matches[1]));
$inlineCss = implode("\n", array_map(fn($path) => file_get_contents($this->webDir.preg_replace('@<link rel="stylesheet" href="([^"]+)">@i', '$1', $path)), $matches[1]));

return $inlineCss;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Asset/TemplateAssetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ public function inlineCssLinkTag(EntrypointCollection $collection, string $templ
preg_match_all('@<link rel="stylesheet" href="([^"]+)">@i', $styleTags, $matches);

if (isset($matches[1]) && !empty($matches[1])) {

Check failure on line 91 in src/Asset/TemplateAssetGenerator.php

View workflow job for this annotation

GitHub Actions / phpstan

Offset 1 on array{array<int, string>, array<int, non-empty-string>} in isset() always exists and is not nullable.
$inlineCss = implode("\n", array_map(function ($path) {
return file_get_contents($this->webDir.preg_replace('@<link rel="stylesheet" href="([^"]+)">@i', '$1', $path));
}, $matches[1]));
$inlineCss = implode("\n", array_map(fn($path) => file_get_contents($this->webDir.preg_replace('@<link rel="stylesheet" href="([^"]+)">@i', '$1', $path)), $matches[1]));

return $inlineCss;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Collection/ConfigurationCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@

class ConfigurationCollection
{
private ExtensionCollection $extensionCollection;

public function __construct(ExtensionCollection $extensionCollection)
public function __construct(private readonly ExtensionCollection $extensionCollection)
{
$this->extensionCollection = $extensionCollection;
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/Collection/EntryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,17 @@
use Contao\LayoutModel;
use HeimrichHannot\EncoreBundle\Exception\NoEntrypointsException;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;

class EntryCollection
{
private ConfigurationCollection $configurationCollection;
private array $bundleConfig;
private bool $useCache = false;
private array $entries;
private CacheItemPoolInterface $cache;

public function __construct(ConfigurationCollection $configurationCollection, array $bundleConfig, CacheItemPoolInterface $cache)
public function __construct(private readonly ConfigurationCollection $configurationCollection, private array $bundleConfig, private readonly CacheItemPoolInterface $cache)
{
$this->configurationCollection = $configurationCollection;
$this->bundleConfig = $bundleConfig;

if ($bundleConfig['encore_cache_enabled'] ?? false) {
if ($this->bundleConfig['encore_cache_enabled'] ?? false) {
$this->useCache = true;
}
$this->cache = $cache;
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/Command/PrepareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class PrepareCommand extends Command
private SymfonyStyle $io;

public function __construct(
private CacheItemPoolInterface $encoreCache,
private KernelInterface $kernel,
private Environment $twig,
private ExtensionCollection $extensionCollection
private readonly CacheItemPoolInterface $encoreCache,
private readonly KernelInterface $kernel,
private readonly Environment $twig,
private readonly ExtensionCollection $extensionCollection
) {
parent::__construct();

Expand All @@ -56,15 +56,15 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);

$this->io->title('Update project encore data');

$resultFile = $this->kernel->getProjectDir().DIRECTORY_SEPARATOR.'encore.bundles.js';

$skipEntries = $input->getOption('skip-entries') ? explode(',', $input->getOption('skip-entries')) : [];
$skipEntries = $input->getOption('skip-entries') ? explode(',', (string) $input->getOption('skip-entries')) : [];

$this->io->writeln('Using <fg=green>'.$this->kernel->getEnvironment().'</> environment. (Use --env=[ENV] to change environment. See --help for more information!)');

Expand Down Expand Up @@ -92,14 +92,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (!file_exists($bundlePath.DIRECTORY_SEPARATOR.'composer.json')) {
trigger_error(
'[Encore Bundle] Could not find composer.json file for '.$bundle->getName().'.'
.' Skipping EncoreExtension '.\get_class($extension).'.'
.' Skipping EncoreExtension '.$extension::class.'.'
);
continue;
}

try {
$composerData = json_decode(file_get_contents($bundlePath.'/composer.json'), null, 512, \JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
} catch (\JsonException) {
throw new \JsonException('composer.json of '.$reflection->getShortName().' has a syntax error.');
}

Expand All @@ -119,7 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$extensionDependencies = array_merge($extensionDependencies, ($packageData['dependencies'] ?? []));
}

$extensionList[] = [$reflection->getShortName(), \get_class($extension), $bundlePath];
$extensionList[] = [$reflection->getShortName(), $extension::class, $bundlePath];
}

$this->io->newLine();
Expand Down Expand Up @@ -186,6 +186,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
'',
]);

return 0;
return Command::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getBundles(ParserInterface $parser): array
/**
* Allows a plugin to load container configuration.
*/
public function registerContainerConfiguration(LoaderInterface $loader, array $managerConfig)
public function registerContainerConfiguration(LoaderInterface $loader, array $managerConfig): void
{
$loader->load('@HeimrichHannotEncoreBundle/config/config.yml');
$loader->load('@HeimrichHannotEncoreBundle/config/services.yml');
Expand Down
27 changes: 5 additions & 22 deletions src/DataContainer/LayoutContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace HeimrichHannot\EncoreBundle\DataContainer;

use Contao\CoreBundle\DependencyInjection\Attribute\AsCallback;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\DataContainer;
use Contao\LayoutModel;
use Contao\Message;
Expand All @@ -21,29 +21,14 @@

class LayoutContainer
{
protected array $bundleConfig;
protected ContaoFramework $contaoFramework;
private RequestStack $requestStack;
private ScopeMatcher $scopeMatcher;
private EntryCollection $entryCollection;
private TranslatorInterface $translator;

/**
* LayoutContainer constructor.
*/
public function __construct(array $bundleConfig, ContaoFramework $contaoFramework, RequestStack $requestStack, ScopeMatcher $scopeMatcher, EntryCollection $entryCollection, TranslatorInterface $translator)
public function __construct(protected array $bundleConfig, protected ContaoFramework $contaoFramework, private readonly RequestStack $requestStack, private readonly ScopeMatcher $scopeMatcher, private readonly EntryCollection $entryCollection, private readonly TranslatorInterface $translator)
{
$this->bundleConfig = $bundleConfig;
$this->contaoFramework = $contaoFramework;
$this->requestStack = $requestStack;
$this->scopeMatcher = $scopeMatcher;
$this->entryCollection = $entryCollection;
$this->translator = $translator;
}

/**
* @Callback(table="tl_layout", target="config.onload")
*/
#[AsCallback(table: 'tl_layout', target: 'config.onload')]
public function onLoadCallback(DataContainer $dc = null): void
{
$request = $this->requestStack->getCurrentRequest();
Expand All @@ -70,10 +55,8 @@ public function onLoadCallback(DataContainer $dc = null): void
}
}

/**
* @Callback(table="tl_layout", target="fields.encoreStylesheetsImportsTemplate.options")
* @Callback(table="tl_layout", target="fields.encoreScriptsImportsTemplate.options")
*/
#[AsCallback(table: 'tl_layout', target: 'fields.encoreStylesheetsImportsTemplate.options')]
#[AsCallback(table: 'tl_layout', target: 'fields.encoreScriptsImportsTemplate.options')]
public function onImportTemplateOptionsCallback(): array
{
$options = [];
Expand Down
4 changes: 1 addition & 3 deletions src/Dca/EncoreEntriesSelectFieldOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

class EncoreEntriesSelectFieldOptions
{
protected string $table;
protected string $fieldName = 'encoreEntries';
protected bool $includeActiveCheckbox = false;
protected ?array $fieldLabel = null;
protected ?array $selectLabel = null;
protected ?array $checkboxLabel = null;

public function __construct(string $table)
public function __construct(protected string $table)
{
$this->table = $table;
}

public function getTable(): string
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/EncoreConfigCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class EncoreConfigCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->has(ExtensionCollection::class)) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/EncoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getAlias(): string
/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container)
public function prepend(ContainerBuilder $container): void
{
// Load current configuration of the webpack encore bundle
$configs = $container->getExtensionConfig('webpack_encore');
Expand Down Expand Up @@ -70,7 +70,7 @@ public function prepend(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$processedConfig = $this->processConfiguration($configuration, $configs);
Expand Down
9 changes: 1 addition & 8 deletions src/Event/EncoreEnabledEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,8 @@

class EncoreEnabledEvent extends Event
{
private bool $enabled;
private Request $request;
private ?PageModel $pageModel;

public function __construct(bool $enabled, Request $request, ?PageModel $pageModel)
public function __construct(private bool $enabled, private readonly Request $request, private readonly ?PageModel $pageModel)
{
$this->enabled = $enabled;
$this->request = $request;
$this->pageModel = $pageModel;
}

public function isEnabled(): bool
Expand Down
Loading

0 comments on commit 9817937

Please sign in to comment.