Skip to content

Commit 2ce093a

Browse files
Caching
1 parent 1ffe36b commit 2ce093a

33 files changed

+221
-48
lines changed

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,7 @@ parameters:
198198
# present only in nette/utils 4
199199
message: '#^PHPDoc tag @var with type SplFileInfo is not subtype of native type Nette\\Utils\\FileInfo\.$#'
200200
reportUnmatched: false
201+
-
202+
message: '#^Parameter \#1 .* of static method .*::fromJson\(\) expects array{.*}, array.* given\.$#'
203+
-
204+
message: '#^Parameter \#1 .* of static method .*::.*FromJson\(\) expects array{.*}, array.* given\.$#'

src/Analyser/LatteContextAnalyser.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private function cacheFilename(string $file): string
204204
return $this->tmpDir . basename($file) . '.' . $cacheKey . '.json';
205205
}
206206

207-
private function saveLatteContextDataToCache(string $file, LatteContextData $fileResult)
207+
private function saveLatteContextDataToCache(string $file, LatteContextData $fileResult): void
208208
{
209209
if (!is_dir($this->tmpDir)) {
210210
Filesystem::createDir($this->tmpDir, 0777);
@@ -258,28 +258,47 @@ private function loadLatteContextDataFromCache(string $file): ?LatteContextData
258258
return null;
259259
}
260260

261+
$file = $cacheData['file'];
262+
$fileHash = $cacheData['fileHash'];
263+
264+
if (!is_string($file) || !is_string($fileHash)) {
265+
FileSystem::delete($cacheFile);
266+
return null;
267+
}
268+
261269
// Check if the file has changed since the cache was created
262-
if (sha1(Filesystem::read($cacheData['file'])) !== $cacheData['fileHash']) {
270+
if (sha1(Filesystem::read($file)) !== $fileHash) {
263271
return null;
264272
}
265273

266274
if (isset($cacheData['dependencies']) && is_array($cacheData['dependencies'])) {
267275
foreach ($cacheData['dependencies'] as $dependency) {
268-
if (!isset($dependency['file'], $dependency['fileHash'])) {
276+
if (!is_array($dependency) || !isset($dependency['file'], $dependency['fileHash'])) {
269277
return null;
270278
}
271-
if (!is_file($dependency['file'])) {
279+
$dependencyFile = $dependency['file'];
280+
$dependencyFileHash = $dependency['fileHash'];
281+
if (!is_string($dependencyFile) || !is_string($dependencyFileHash)) {
282+
return null;
283+
}
284+
if (!is_file($dependencyFile)) {
272285
return null;
273286
}
274287
// Check if the dependency file has changed since the cache was created
275-
if (sha1(Filesystem::read($dependency['file'])) !== $dependency['fileHash']) {
288+
if (sha1(Filesystem::read($dependencyFile)) !== $dependencyFileHash) {
276289
return null;
277290
}
278291
}
279292
}
280293

294+
$data = $cacheData['data'];
295+
if (!is_array($data)) {
296+
FileSystem::delete($cacheFile);
297+
return null;
298+
}
299+
281300
try {
282-
return LatteContextData::fromJson($cacheData['data'], $this->typeStringResolver);
301+
return LatteContextData::fromJson($data, $this->typeStringResolver);
283302
} catch (Exception) {
284303
FileSystem::delete($cacheFile);
285304
return null;

src/Analyser/LatteContextData.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Efabrica\PHPStanLatte\LatteContext\CollectedData\CollectedError;
88
use Efabrica\PHPStanLatte\LatteContext\CollectedData\CollectedLatteContextObject;
99
use Efabrica\PHPStanLatte\LatteContext\CollectedData\CollectedRelatedFiles;
10+
use InvalidArgumentException;
1011
use JsonSerializable;
1112
use PHPStan\PhpDoc\TypeStringResolver;
1213
use PHPStan\Rules\IdentifierRuleError;
1314
use PHPStan\Rules\RuleErrorBuilder;
15+
use ReturnTypeWillChange;
1416
use function get_class;
1517

1618
final class LatteContextData implements JsonSerializable
@@ -104,6 +106,10 @@ public function getRelatedFiles(): array
104106
return array_unique($relatedFiles);
105107
}
106108

109+
/**
110+
* @return array<string, mixed>
111+
*/
112+
#[ReturnTypeWillChange]
107113
public function jsonSerialize(): array
108114
{
109115
$data = [];
@@ -118,11 +124,17 @@ public function jsonSerialize(): array
118124
];
119125
}
120126

121-
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): self
127+
/**
128+
* @param array{items: array{class: class-string<CollectedLatteContextObject>, data: mixed}[]} $data
129+
*/
130+
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): static
122131
{
123132
$collectedData = [];
124133
foreach ($data['items'] as $item) {
125134
$class = $item['class'];
135+
if (!class_exists($class) || !is_array($item['data'])) {
136+
throw new InvalidArgumentException("Cannot deserialize collected data, class $class not found or data is not array");
137+
}
126138
$collectedData[] = $class::fromJson($item['data'], $typeStringResolver);
127139
}
128140
return new self($collectedData, []);

src/Compiler/Compiler/Latte2Compiler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
final class Latte2Compiler extends AbstractCompiler
1818
{
19+
/** @var string[] */
1920
private array $macros = [];
2021

2122
/**
@@ -30,7 +31,7 @@ public function __construct(
3031
array $macros = []
3132
) {
3233
parent::__construct($engine, $strictMode, $filters, $functions);
33-
$this->marcos = $macros;
34+
$this->macros = $macros;
3435
$this->installMacros($macros);
3536
}
3637

src/LatteContext/CollectedData/CollectedComponent.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\PhpDoc\TypeStringResolver;
1212
use PHPStan\Type\Type;
13+
use ReturnTypeWillChange;
1314

1415
final class CollectedComponent extends CollectedLatteContextObject
1516
{
@@ -80,6 +81,10 @@ public static function build(?Node $node, Scope $scope, string $name, Type $type
8081
);
8182
}
8283

84+
/**
85+
* @return array<string, mixed>
86+
*/
87+
#[ReturnTypeWillChange]
8388
public function jsonSerialize(): array
8489
{
8590
return [
@@ -91,14 +96,17 @@ public function jsonSerialize(): array
9196
];
9297
}
9398

94-
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): self
99+
/**
100+
* @param array{class: string, className: string, methodName: string, component: array<string, mixed>, declared: bool} $data
101+
*/
102+
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): static
95103
{
96104

97105
return new self(
98-
$data['className'] ?? '',
99-
$data['methodName'] ?? '',
106+
$data['className'],
107+
$data['methodName'],
100108
Component::fromJson($data['component'], $typeStringResolver),
101-
$data['declared'] ?? false
109+
$data['declared']
102110
);
103111
}
104112
}

src/LatteContext/CollectedData/CollectedError.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\PhpDoc\TypeStringResolver;
10+
use ReturnTypeWillChange;
1011

1112
final class CollectedError extends CollectedLatteContextObject
1213
{
@@ -47,6 +48,10 @@ public static function build(Node $node, Scope $scope, string $message): self
4748
);
4849
}
4950

51+
/**
52+
* @return array<string, mixed>
53+
*/
54+
#[ReturnTypeWillChange]
5055
public function jsonSerialize(): array
5156
{
5257
return [
@@ -56,7 +61,10 @@ public function jsonSerialize(): array
5661
];
5762
}
5863

59-
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): self
64+
/**
65+
* @param array{message: string, file: string, line?: int|null} $data
66+
*/
67+
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): static
6068
{
6169
return new self(
6270
$data['message'],

src/LatteContext/CollectedData/CollectedFilter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Efabrica\PHPStanLatte\Template\Filter;
88
use PHPStan\PhpDoc\TypeStringResolver;
9+
use ReturnTypeWillChange;
910

1011
final class CollectedFilter extends CollectedLatteContextObject
1112
{
@@ -37,6 +38,10 @@ public function getFilter(): Filter
3738
return $this->filter;
3839
}
3940

41+
/**
42+
* @return array<string, mixed>
43+
*/
44+
#[ReturnTypeWillChange]
4045
public function jsonSerialize(): array
4146
{
4247
return [
@@ -46,7 +51,10 @@ public function jsonSerialize(): array
4651
];
4752
}
4853

49-
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): self
54+
/**
55+
* @param array{className: string, methodName: string, filter: array<string, mixed>} $data
56+
*/
57+
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): static
5058
{
5159
return new self(
5260
$data['className'],

src/LatteContext/CollectedData/CollectedLatteContextObject.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
namespace Efabrica\PHPStanLatte\LatteContext\CollectedData;
66

77
use JsonSerializable;
8+
use PHPStan\PhpDoc\TypeStringResolver;
89

910
abstract class CollectedLatteContextObject implements JsonSerializable
1011
{
11-
12+
/**
13+
* @param array<mixed> $data
14+
*/
15+
abstract public static function fromJson(array $data, TypeStringResolver $typeStringResolver): static;
1216
}

src/LatteContext/CollectedData/CollectedMethod.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Type\Type;
99
use PHPStan\Type\TypeCombinator;
1010
use PHPStan\Type\VerbosityLevel;
11+
use ReturnTypeWillChange;
1112

1213
final class CollectedMethod extends CollectedLatteContextObject
1314
{
@@ -69,6 +70,10 @@ public static function combine(string $className, string $methodName, CollectedM
6970
return new self($className, $methodName, $alwaysTerminated, $returnTypes ? TypeCombinator::union(...$returnTypes) : null);
7071
}
7172

73+
/**
74+
* @return array<string, mixed>
75+
*/
76+
#[ReturnTypeWillChange]
7277
public function jsonSerialize(): array
7378
{
7479
return [
@@ -79,7 +84,10 @@ public function jsonSerialize(): array
7984
];
8085
}
8186

82-
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): self
87+
/**
88+
* @param array{className: string, methodName: string, alwaysTerminated?: bool, returnType?: string|null} $data
89+
*/
90+
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): static
8391
{
8492
return new self(
8593
$data['className'],

src/LatteContext/CollectedData/CollectedMethodCall.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\PhpDoc\TypeStringResolver;
1111
use PHPStan\Reflection\ClassReflection;
12+
use ReturnTypeWillChange;
1213

1314
final class CollectedMethodCall extends CollectedLatteContextObject
1415
{
@@ -181,6 +182,10 @@ public static function build(
181182
);
182183
}
183184

185+
/**
186+
* @return array<string, mixed>
187+
*/
188+
#[ReturnTypeWillChange]
184189
public function jsonSerialize(): array
185190
{
186191
return [
@@ -195,7 +200,10 @@ public function jsonSerialize(): array
195200
];
196201
}
197202

198-
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): self
203+
/**
204+
* @param array{callerClassName?: ?class-string, callerMethodName?: string, calledClassName?: ?class-string, calledMethodName?: string, isCalledConditionally?: bool, type?: string, params?: array<string, string|int|float|bool>, currentClassName?: ?class-string} $data
205+
*/
206+
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): static
199207
{
200208
return new self(
201209
$data['callerClassName'] ?? null,

0 commit comments

Comments
 (0)