Skip to content

Commit 270959f

Browse files
Fixed generation of template context signatures to avoid issues (#470)
2 parents b6a0803 + 682d32e commit 270959f

File tree

12 files changed

+95
-3
lines changed

12 files changed

+95
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"phpunit/phpunit": "^9.5",
1919
"nette/application": "^3.1.11",
2020
"nette/forms": "^3.1.12",
21-
"nikic/php-parser": "5.6.0",
21+
"nikic/php-parser": "^5.6.0",
2222
"efabrica/coding-standard": "^0.7",
2323
"phpstan/phpstan-strict-rules": "^2.0",
2424
"spaze/phpstan-disallowed-calls": "^2.11|^3.0|^4.0"

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,6 @@ parameters:
205205
message: '#^Parameter \#1 .* of static method .*::fromJson\(\) expects array{.*}, array.* given\.$#'
206206
-
207207
message: '#^Parameter \#1 .* of static method .*::.*FromJson\(\) expects array{.*}, array.* given\.$#'
208+
-
209+
message: '#^Method Efabrica\\PHPStanLatte\\Compiler\\LatteVersion::isLatte.\(\) never returns (true|false) so the return type can be changed to (true|false)\.$#'
210+
reportUnmatched: false

src/Template/Component.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\PhpDoc\TypeStringResolver;
1010
use PHPStan\PhpDocParser\Printer\Printer;
1111
use PHPStan\Type\Type;
12+
use PHPStan\Type\VerbosityLevel;
1213
use ReturnTypeWillChange;
1314

1415
final class Component implements NameTypeItem, JsonSerializable
@@ -68,6 +69,18 @@ public function withType(Type $type): self
6869
return $clone;
6970
}
7071

72+
public function getSignatureHash(): string
73+
{
74+
return md5((string)json_encode([
75+
'name' => $this->name,
76+
'type' => $this->type->describe(VerbosityLevel::precise()),
77+
'subcomponents' => array_map(
78+
static fn(Component $component): string => $component->getSignatureHash(),
79+
$this->subcomponents
80+
),
81+
]));
82+
}
83+
7184
/**
7285
* @return array<string, mixed>
7386
*/

src/Template/Filter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\PhpDoc\TypeStringResolver;
1010
use PHPStan\PhpDocParser\Printer\Printer;
1111
use PHPStan\Type\Type;
12+
use PHPStan\Type\VerbosityLevel;
1213
use ReturnTypeWillChange;
1314

1415
final class Filter implements NameTypeItem, JsonSerializable
@@ -45,6 +46,14 @@ public function withType(Type $type): self
4546
return $clone;
4647
}
4748

49+
public function getSignatureHash(): string
50+
{
51+
return md5((string)json_encode([
52+
'name' => $this->name,
53+
'type' => $this->type->describe(VerbosityLevel::precise()),
54+
]));
55+
}
56+
4857
#[ReturnTypeWillChange]
4958
public function jsonSerialize()
5059
{

src/Template/Form/Container.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\PhpDoc\TypeStringResolver;
1010
use PHPStan\PhpDocParser\Printer\Printer;
1111
use PHPStan\Type\Type;
12+
use PHPStan\Type\VerbosityLevel;
1213
use ReturnTypeWillChange;
1314

1415
final class Container implements ControlHolderInterface, ControlInterface
@@ -51,6 +52,16 @@ public function withType(Type $type): self
5152
return $clone;
5253
}
5354

55+
public function getSignatureHash(): string
56+
{
57+
return md5((string)json_encode([
58+
'class' => self::class,
59+
'name' => $this->name,
60+
'type' => $this->type->describe(VerbosityLevel::precise()),
61+
'controls' => array_map(fn(ControlInterface $control) => $control->getSignatureHash(), $this->controls),
62+
]));
63+
}
64+
5465
#[ReturnTypeWillChange]
5566
public function jsonSerialize()
5667
{
@@ -63,7 +74,7 @@ public function jsonSerialize()
6374
}
6475

6576
/**
66-
* @param array{name: string, type: string, controls: array<array<string, mixed>>} $data
77+
* @param array{name: string, type: string, controls: array<array{name: string}>} $data
6778
*/
6879
public static function fromJson(array $data, TypeStringResolver $typeStringResolver): self
6980
{

src/Template/Form/ControlInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99

1010
interface ControlInterface extends NameTypeItem, JsonSerializable
1111
{
12+
public function getSignatureHash(): string;
1213
}

src/Template/Form/Field.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\PhpDoc\TypeStringResolver;
1010
use PHPStan\PhpDocParser\Printer\Printer;
1111
use PHPStan\Type\Type;
12+
use PHPStan\Type\VerbosityLevel;
1213
use ReturnTypeWillChange;
1314

1415
final class Field implements NameTypeItem, ControlInterface
@@ -62,6 +63,16 @@ public function getOptions(): ?array
6263
return $this->options;
6364
}
6465

66+
public function getSignatureHash(): string
67+
{
68+
return md5((string)json_encode([
69+
'class' => self::class,
70+
'name' => $this->name,
71+
'type' => $this->type->describe(VerbosityLevel::precise()),
72+
'options' => $this->options,
73+
]));
74+
}
75+
6576
#[ReturnTypeWillChange]
6677
public function jsonSerialize()
6778
{

src/Template/Form/Form.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public function withType(Type $type): self
9999
return $clone;
100100
}
101101

102+
public function getSignatureHash(): string
103+
{
104+
return md5((string)json_encode([
105+
'name' => $this->name,
106+
'type' => TypeHelper::serializeType($this->type),
107+
'controls' => array_map(fn(ControlInterface $control) => $control->getSignatureHash(), $this->controls),
108+
'groups' => array_map(fn(Group $group) => $group->getSignatureHash(), $this->groups),
109+
]));
110+
}
111+
102112
#[ReturnTypeWillChange]
103113
public function jsonSerialize()
104114
{

src/Template/Form/Group.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public function getName(): string
3030
return $this->name;
3131
}
3232

33+
public function getSignatureHash(): string
34+
{
35+
return md5((string)json_encode([
36+
'name' => $this->name,
37+
'controls' => array_map(fn(ControlInterface $control) => $control->getSignatureHash(), $this->controls),
38+
]));
39+
}
40+
3341
#[ReturnTypeWillChange]
3442
public function jsonSerialize()
3543
{

src/Template/Template.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ public function getParentTemplatePaths(): array
106106

107107
public function getSignatureHash(): string
108108
{
109-
return md5((string)json_encode($this));
109+
return md5((string)json_encode([
110+
'path' => $this->path,
111+
'actualClass' => $this->actualClass,
112+
'actualAction' => $this->actualAction,
113+
'templateContext' => $this->templateContext->getSignatureHash(),
114+
'parentTemplatePaths' => $this->parentTemplatePaths,
115+
]));
110116
}
111117

112118
#[ReturnTypeWillChange]

0 commit comments

Comments
 (0)