Skip to content

Commit fc83426

Browse files
committed
Code: add types, apply codesniffer + phpstan
1 parent 6891010 commit fc83426

File tree

8 files changed

+116
-82
lines changed

8 files changed

+116
-82
lines changed

src/AssetFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types = 1);
42

53
namespace Contributte\Vite;
64

75
final class AssetFilter
86
{
7+
98
private Service $vite;
109

1110
public function __construct(Service $vite)
@@ -17,4 +16,5 @@ public function __invoke(string $path): string
1716
{
1817
return $this->vite->getAsset($path);
1918
}
19+
2020
}

src/Exception/LogicalException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\Vite\Exception;
4+
5+
use LogicException;
6+
7+
class LogicalException extends LogicException
8+
{
9+
10+
}

src/Exception/RuntimeException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\Vite\Exception;
4+
5+
class RuntimeException extends \RuntimeException
6+
{
7+
8+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types = 1);
42

53
namespace Contributte\Vite;
64

75
final class ManifestFileDoesNotExistsException extends \RuntimeException
86
{
7+
98
}

src/Nette/Extension.php

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types = 1);
42

53
namespace Contributte\Vite\Nette;
64

75
use Contributte\Vite\AssetFilter;
86
use Contributte\Vite\ManifestFileDoesNotExistsException;
97
use Contributte\Vite\Service;
108
use Contributte\Vite\Tracy\VitePanel;
11-
use Nette;
129
use Nette\DI\CompilerExtension;
13-
use Nette\DI\Definitions;
14-
use Nette\Schema;
15-
use Tracy;
10+
use Nette\DI\Definitions\FactoryDefinition;
11+
use Nette\DI\Definitions\ServiceDefinition;
12+
use Nette\DI\Definitions\Statement;
13+
use Nette\Safe;
14+
use Nette\Schema\Expect;
15+
use Nette\Schema\Schema;
16+
use Nette\Utils\Finder;
17+
use stdClass;
18+
use Tracy\Bar;
1619

1720
/**
18-
* @property \stdClass $config
21+
* @property stdClass $config
1922
*/
2023
final class Extension extends CompilerExtension
2124
{
22-
public function getConfigSchema(): Schema\Schema
25+
26+
public function getConfigSchema(): Schema
2327
{
24-
return Schema\Expect::structure([
25-
'server' => Schema\Expect::string('http://localhost:5173'),
26-
'cookie' => Schema\Expect::string('contributte/vite'),
27-
'debugMode' => Schema\Expect::bool($this->getContainerBuilder()->parameters['debugMode'] ?? false),
28-
'manifestFile' => Schema\Expect::string(),
29-
'filterName' => Schema\Expect::string('vite'), // empty string is for disabled
30-
'templateProperty' => Schema\Expect::string('vite'), // empty string is for disabled
31-
'wwwDir' => Schema\Expect::string($this->getContainerBuilder()->parameters['wwwDir'] ?? getcwd()),
32-
'basePath' => Schema\Expect::string(),
28+
return Expect::structure([
29+
'server' => Expect::string('http://localhost:5173'),
30+
'cookie' => Expect::string('contributte/vite'),
31+
'debugMode' => Expect::bool($this->getContainerBuilder()->parameters['debugMode'] ?? false),
32+
'manifestFile' => Expect::string(),
33+
'filterName' => Expect::string('vite'), // empty string is for disabled
34+
'templateProperty' => Expect::string('vite'), // empty string is for disabled
35+
'wwwDir' => Expect::string($this->getContainerBuilder()->parameters['wwwDir'] ?? getcwd()),
36+
'basePath' => Expect::string(),
3337
]);
3438
}
3539

@@ -39,39 +43,18 @@ public function loadConfiguration(): void
3943
$this->buildFilter();
4044
}
4145

42-
private function buildViteService(): void
43-
{
44-
$manifestFile = $this->prepareManifestPath();
45-
$this->getContainerBuilder()->addDefinition($this->prefix('service'))
46-
->setFactory(Service::class)
47-
->setArguments([
48-
'viteServer' => $this->config->server,
49-
'viteCookie' => $this->config->cookie,
50-
'manifestFile' => $manifestFile,
51-
'debugMode' => $this->config->debugMode,
52-
'basePath' => $this->prepareBasePath($manifestFile),
53-
]);
54-
}
55-
56-
private function buildFilter(): void
57-
{
58-
$this->getContainerBuilder()->addDefinition($this->prefix('assetFilter'))
59-
->setFactory(AssetFilter::class)
60-
->setAutowired(false);
61-
}
62-
6346
public function beforeCompile(): void
6447
{
6548
$builder = $this->getContainerBuilder();
6649

6750
$templateFactoryDefinition = $builder->getDefinition('latte.templateFactory');
68-
assert($templateFactoryDefinition instanceof Definitions\ServiceDefinition);
51+
assert($templateFactoryDefinition instanceof ServiceDefinition);
6952

7053
if ($this->config->templateProperty !== '') {
7154
$templateFactoryDefinition->addSetup(
72-
new Nette\DI\Definitions\Statement('$onCreate[]', [
73-
new Definitions\Statement([
74-
self::class,
55+
new Statement('$onCreate[]', [
56+
new Statement([
57+
Helpers::class,
7558
'prepareTemplate',
7659
], [$this->config->templateProperty, $builder->getDefinition($this->prefix('service'))]),
7760
]),
@@ -80,32 +63,45 @@ public function beforeCompile(): void
8063

8164
if ($this->config->filterName !== '' && $builder->hasDefinition('latte.latteFactory')) {
8265
$definition = $builder->getDefinition('latte.latteFactory');
83-
assert($definition instanceof Definitions\FactoryDefinition);
66+
assert($definition instanceof FactoryDefinition);
8467
$definition->getResultDefinition()
8568
->addSetup('addFilter', [
8669
$this->config->filterName,
8770
$builder->getDefinition($this->prefix('assetFilter')),
8871
]);
8972
}
9073

91-
$tracyClass = Tracy\Bar::class;
92-
93-
94-
if ($this->config->debugMode && $builder->getByType($tracyClass)) {
74+
if ($this->config->debugMode && $builder->getByType(Bar::class) !== null) {
9575
$definition = $this->getContainerBuilder()
9676
->getDefinition($this->prefix('service'));
97-
assert($definition instanceof Definitions\ServiceDefinition);
98-
$definition->addSetup("@$tracyClass::addPanel", [
99-
new Definitions\Statement(VitePanel::class),
77+
78+
assert($definition instanceof ServiceDefinition);
79+
80+
$definition->addSetup(sprintf('@%s::addPanel', Bar::class), [
81+
new Statement(VitePanel::class),
10082
]);
10183
}
10284
}
10385

104-
public static function prepareTemplate(string $propertyName, Service $service): \Closure
86+
private function buildViteService(): void
10587
{
106-
return static function (Nette\Application\UI\Template $template) use ($propertyName, $service): void {
107-
$template->{$propertyName} = $service;
108-
};
88+
$manifestFile = $this->prepareManifestPath();
89+
$this->getContainerBuilder()->addDefinition($this->prefix('service'))
90+
->setFactory(Service::class)
91+
->setArguments([
92+
'viteServer' => $this->config->server,
93+
'viteCookie' => $this->config->cookie,
94+
'manifestFile' => $manifestFile,
95+
'debugMode' => $this->config->debugMode,
96+
'basePath' => $this->prepareBasePath($manifestFile),
97+
]);
98+
}
99+
100+
private function buildFilter(): void
101+
{
102+
$this->getContainerBuilder()->addDefinition($this->prefix('assetFilter'))
103+
->setFactory(AssetFilter::class)
104+
->setAutowired(false);
109105
}
110106

111107
private function prepareManifestPath(): string
@@ -124,25 +120,26 @@ private function prepareManifestPath(): string
124120
$manifestFile = $newPath;
125121
}
126122

127-
return Nette\Safe::realpath($manifestFile);
123+
return Safe::realpath($manifestFile);
128124
}
129125

130126
private function prepareBasePath(string $manifestFile): string
131127
{
132128
if ($this->config->basePath === null) {
133-
return str_replace(Nette\Safe::realpath($this->config->wwwDir), '', dirname($manifestFile)) . '/';
129+
return str_replace(Safe::realpath($this->config->wwwDir), '', dirname($manifestFile)) . '/';
134130
}
135131

136132
return $this->config->basePath;
137133
}
138134

139135
private function automaticSearchManifestFile(): string
140136
{
141-
$finder = Nette\Utils\Finder::findFiles('manifest.json')->from($this->config->wwwDir);
137+
$finder = Finder::findFiles('manifest.json')->from($this->config->wwwDir);
142138
$files = [];
143139
foreach ($finder as $file) {
144140
$files[] = $file->getPathname();
145141
}
142+
146143
if ($files === []) {
147144
throw new ManifestFileDoesNotExistsException(sprintf('Define path to manifest.json, because automatic search found nothing in "%s".', $this->config->wwwDir));
148145
} elseif (count($files) > 1) {
@@ -151,4 +148,5 @@ private function automaticSearchManifestFile(): string
151148

152149
return reset($files);
153150
}
151+
154152
}

src/Nette/Helpers.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\Vite\Nette;
4+
5+
use Contributte\Vite\Service;
6+
use Nette\Application\UI\Template;
7+
8+
final class Helpers
9+
{
10+
11+
public static function prepareTemplate(string $propertyName, Service $service): \Closure
12+
{
13+
return static function (Template $template) use ($propertyName, $service): void {
14+
$template->{$propertyName} = $service;
15+
};
16+
}
17+
18+
}

src/Service.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types = 1);
42

53
namespace Contributte\Vite;
64

5+
use Contributte\Vite\Exception\LogicalException;
76
use Nette\Http\Request;
87
use Nette\Utils\FileSystem;
98
use Nette\Utils\Html;
109
use Nette\Utils\Json;
1110

1211
final class Service
1312
{
13+
1414
private string $viteServer;
1515

1616
private string $viteCookie;
@@ -30,7 +30,8 @@ public function __construct(
3030
bool $debugMode,
3131
string $basePath,
3232
Request $httpRequest
33-
) {
33+
)
34+
{
3435
$this->viteServer = $viteServer;
3536
$this->viteCookie = $viteCookie;
3637
$this->manifestFile = $manifestFile;
@@ -50,8 +51,9 @@ public function getAsset(string $entrypoint): string
5051
$asset = '';
5152

5253
if (file_exists($this->manifestFile)) {
53-
$manifest = Json::decode(FileSystem::read($this->manifestFile), Json::FORCE_ARRAY);
54-
$asset = $manifest[$entrypoint]['file'];
54+
/** @var array<array<mixed>> $manifest */
55+
$manifest = Json::decode(FileSystem::read($this->manifestFile), forceArrays: true);
56+
$asset = $manifest[$entrypoint]['file'] ?? throw new LogicalException('Invalid manifest');
5557
} else {
5658
trigger_error('Missing manifest file: ' . $this->manifestFile, E_USER_WARNING);
5759
}
@@ -70,7 +72,8 @@ public function getCssAssets(string $entrypoint): array
7072
if (!$this->isEnabled()) {
7173
if (file_exists($this->manifestFile)) {
7274
$entrypoint = ltrim($entrypoint, '/');
73-
$manifest = Json::decode(FileSystem::read($this->manifestFile), Json::FORCE_ARRAY);
75+
/** @var array<string, array<array<string, string>>> $manifest */
76+
$manifest = Json::decode(FileSystem::read($this->manifestFile), forceArrays: true);
7477
$assets = $manifest[$entrypoint]['css'] ?? [];
7578
} else {
7679
trigger_error('Missing manifest file: ' . $this->manifestFile, E_USER_WARNING);
@@ -91,7 +94,7 @@ public function printTags(string $entrypoint): void
9194
$styles = $this->getCssAssets($entrypoint);
9295

9396
if ($this->isEnabled()) {
94-
echo Html::el('script')->type('module')->src($this->viteServer . '/' . '@vite/client');
97+
echo Html::el('script')->type('module')->src($this->viteServer . '/@vite/client');
9598
}
9699

97100
foreach ($styles as $path) {
@@ -103,11 +106,9 @@ public function printTags(string $entrypoint): void
103106
}
104107
}
105108

106-
/**
107-
* @return string
108-
*/
109109
public function getViteCookie(): string
110110
{
111111
return $this->viteCookie;
112112
}
113+
113114
}

src/Tracy/VitePanel.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types = 1);
42

53
namespace Contributte\Vite\Tracy;
64

75
use Contributte\Vite\Service;
86
use Nette\Safe;
9-
use Tracy;
7+
use Tracy\IBarPanel;
108

11-
final class VitePanel implements Tracy\IBarPanel
9+
final class VitePanel implements IBarPanel
1210
{
11+
1312
private Service $vite;
1413

1514
public function __construct(Service $vite)
1615
{
1716
$this->vite = $vite;
1817
}
1918

20-
public function getTab()
19+
public function getTab(): string
2120
{
2221
$html = Safe::file_get_contents(__DIR__ . '/Vite.html');
2322

2423
return str_replace('%viteCookie%', $this->vite->getViteCookie(), $html);
2524
}
2625

27-
public function getPanel()
26+
public function getPanel(): string
2827
{
2928
return '';
3029
}
30+
3131
}

0 commit comments

Comments
 (0)