Skip to content

Commit ed51951

Browse files
committed
Chore: Improvements
- Replaced json_decode with Symfony DecoderInterface; - Tests fixes; - Small psalm-type fixes; - Added --show-info=true to psalm GitHub job; - Fixed CI for installing php-cs-fixer; Signed-off-by: Oleksii Bulba <[email protected]>
1 parent 8e88a16 commit ed51951

File tree

8 files changed

+41
-14
lines changed

8 files changed

+41
-14
lines changed

.github/workflows/static.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
tools: cs2pr
4343

4444
- name: Install php-cs-fixer
45-
uses: ramsey/composer-install@v1
45+
uses: ramsey/composer-install@v2
4646

4747
- name: PHP-CS-Fixer
4848
run: ./vendor/bin/php-cs-fixer fix --verbose --dry-run --format=checkstyle --using-cache=no | cs2pr
@@ -68,7 +68,7 @@ jobs:
6868
uses: ramsey/composer-install@v2
6969

7070
- name: Psalm
71-
run: ./vendor/bin/psalm --no-progress
71+
run: ./vendor/bin/psalm --no-progress --show-info=true
7272

7373
composer-normalize:
7474
name: Composer Normalize

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
],
1313
"require": {
1414
"php": "^8.1 || ^8.2",
15-
"micro/plugin-twig": "^0"
15+
"micro/plugin-twig": "^0",
16+
"symfony/serializer": "^6.2"
1617
},
1718
"require-dev": {
1819
"ergebnis/composer-normalize": "^2.29",

src/Asset/EntrypointLookup.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313

1414
use Boo\WebpackEncorePlugin\Exception\EntrypointNotFoundException;
1515
use Boo\WebpackEncorePlugin\WebpackEncorePluginConfigurationInterface;
16+
use Symfony\Component\Serializer\Encoder\DecoderInterface;
17+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
18+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
1619

1720
class EntrypointLookup implements EntrypointLookupInterface
1821
{
19-
private mixed $entriesData = null;
22+
private ?array $entriesData = null;
2023

2124
private array $returnedFiles = [];
2225

23-
public function __construct(private readonly WebpackEncorePluginConfigurationInterface $configuration)
24-
{
26+
public function __construct(
27+
private readonly WebpackEncorePluginConfigurationInterface $configuration,
28+
private readonly DecoderInterface $decoder
29+
) {
2530
}
2631

2732
/**
@@ -51,6 +56,7 @@ private function getEntryFiles(string $entryName, string $key): array
5156
{
5257
$this->validateEntryName($entryName);
5358
$entriesData = $this->getEntriesData();
59+
/** @var array $entryData */
5460
$entryData = $entriesData['entrypoints'][$entryName] ?? [];
5561

5662
if (!isset($entryData[$key])) {
@@ -59,15 +65,17 @@ private function getEntryFiles(string $entryName, string $key): array
5965
}
6066

6167
// make sure to not return the same file multiple times
68+
/** @var array $entryFiles */
6269
$entryFiles = $entryData[$key];
6370
$newFiles = array_values(array_diff($entryFiles, $this->returnedFiles));
6471
$this->returnedFiles = array_merge($this->returnedFiles, $newFiles);
6572

6673
return $newFiles;
6774
}
6875

69-
private function validateEntryName(string $entryName)
76+
private function validateEntryName(string $entryName): void
7077
{
78+
/** @var array<string, array> $entriesData */
7179
$entriesData = $this->getEntriesData();
7280
if (!isset($entriesData['entrypoints'][$entryName])) {
7381
if (false !== $dotPos = strrpos($entryName, '.')) {
@@ -92,7 +100,11 @@ private function getEntriesData(): array
92100
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->getEntrypointJsonPath()));
93101
}
94102

95-
$this->entriesData = json_decode(file_get_contents($this->getEntrypointJsonPath()), true);
103+
try {
104+
$this->entriesData = $this->decoder->decode(file_get_contents($this->getEntrypointJsonPath()), JsonEncoder::FORMAT);
105+
} catch (UnexpectedValueException $e) {
106+
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->getEntrypointJsonPath()), 0, $e);
107+
}
96108

97109
if (null === $this->entriesData) {
98110
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->getEntrypointJsonPath()));

src/Asset/EntrypointLookupInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ interface EntrypointLookupInterface
1717
{
1818
/**
1919
* @throws EntrypointNotFoundException if an entry name is passed that does not exist in entrypoints.json
20+
*
21+
* @psalm-return array<string>
2022
*/
2123
public function getJavaScriptFiles(string $entryName): array;
2224

2325
/**
2426
* @throws EntrypointNotFoundException if an entry name is passed that does not exist in entrypoints.json
27+
*
28+
* @psalm-return array<string>
2529
*/
2630
public function getCssFiles(string $entryName): array;
2731

src/TagRenderer/TagRenderer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ private function convertArrayToAttributes(array $attributesMap): string
7070
});
7171

7272
return implode(' ', array_map(
73-
static function ($key, $value) {
73+
static function ($key, string|null|bool $value) {
7474
// allows for things like defer: true to only render "defer"
75-
if (true === $value || null === $value) {
75+
if (\is_bool($value) || null === $value) {
7676
return $key;
7777
}
7878

src/WebpackEncorePlugin.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
use Micro\Framework\Kernel\Plugin\ConfigurableInterface;
2020
use Micro\Framework\Kernel\Plugin\PluginConfigurationTrait;
2121
use Micro\Plugin\Twig\Plugin\TwigExtensionPluginInterface;
22+
use Symfony\Component\Serializer\Encoder\DecoderInterface;
23+
use Symfony\Component\Serializer\Encoder\JsonDecode;
2224
use Twig\Extension\ExtensionInterface;
2325

2426
/**
27+
* @psalm-suppress ImplementedReturnTypeMismatch
28+
*
2529
* @method WebpackEncorePluginConfigurationInterface configuration()
2630
*
2731
* @codeCoverageIgnore
@@ -47,6 +51,11 @@ protected function createTagRenderer(): TagRendererInterface
4751

4852
protected function createEntrypointLookup(): EntrypointLookupInterface
4953
{
50-
return new EntrypointLookup($this->configuration());
54+
return new EntrypointLookup($this->configuration(), $this->createDecoder());
55+
}
56+
57+
protected function createDecoder(): DecoderInterface
58+
{
59+
return new JsonDecode([JsonDecode::ASSOCIATIVE => true]);
5160
}
5261
}

src/WebpackEncorePluginConfiguration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class WebpackEncorePluginConfiguration extends PluginConfiguration implements We
2222

2323
public function getOutputPath(): string
2424
{
25-
return $this->configuration->get(self::CFG_OUTPUT_PATH,
26-
$this->configuration->get('BASE_PATH').'/public/build'
25+
return (string) $this->configuration->get(self::CFG_OUTPUT_PATH,
26+
((string) $this->configuration->get('BASE_PATH')).'/public/build'
2727
);
2828
}
2929
}

tests/unit/Asset/EntrypointLookupTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Boo\WebpackEncorePlugin\WebpackEncorePluginConfigurationInterface;
1717
use PHPUnit\Framework\MockObject\MockObject;
1818
use PHPUnit\Framework\TestCase;
19+
use Symfony\Component\Serializer\Encoder\JsonDecode;
1920

2021
/**
2122
* @covers \Boo\WebpackEncorePlugin\Asset\EntrypointLookup
@@ -33,7 +34,7 @@ protected function setUp(): void
3334
->onlyMethods(['getOutputPath'])
3435
->getMockForAbstractClass();
3536

36-
$this->model = new EntrypointLookup($this->configurationMock);
37+
$this->model = new EntrypointLookup($this->configurationMock, new JsonDecode([JsonDecode::ASSOCIATIVE => true]));
3738
}
3839

3940
/**

0 commit comments

Comments
 (0)