Skip to content

Commit 2f45c5c

Browse files
- Fixed unwanted narrowing of template variable types
1 parent ba55f39 commit 2f45c5c

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
### Added
55
- Check if value outputted in template (or escaped for output) can be converted to string
66
- Support for PHP 8.4
7+
### Fixed
8+
- Fixed unwanted narrowing of template variable types
79

810
## [0.18] - 2025-07-22
911
### Updated

src/Resolver/ValueResolver/ValueResolver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPStan\Analyser\Scope;
2222
use PHPStan\Type\UnionType;
2323
use ReflectionMethod;
24+
use function count;
2425
use function is_callable;
2526
use function method_exists;
2627

@@ -38,7 +39,8 @@ public function resolve(Expr $expr, Scope $scope, $fallbackEvaluator = null)
3839
$type = $scope->getType($expr);
3940

4041
$constantScalarValues = $type->getConstantScalarValues();
41-
if ($constantScalarValues !== []) {
42+
43+
if (count($constantScalarValues) === 1) {
4244
return $constantScalarValues[0];
4345
}
4446

@@ -158,7 +160,7 @@ public function resolve(Expr $expr, Scope $scope, $fallbackEvaluator = null)
158160
$options = [];
159161
foreach ($type->getTypes() as $subType) {
160162
$constantScalarValues = $subType->getConstantScalarValues();
161-
if ($constantScalarValues === []) {
163+
if (count($constantScalarValues) !== 1) {
162164
return null;
163165
}
164166
$options[] = $constantScalarValues[0];

tests/Rule/LatteTemplatesRule/SimpleControl/Fixtures/Variables/SomeControl.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
final class SomeControl extends Control
1111
{
12+
public bool $property;
13+
1214
public function render(): void
1315
{
1416
$this->template->a = 'a';
@@ -26,6 +28,7 @@ public function render(): void
2628
[$this->template->{$dynamic1}, $this->template->{$dynamic2}] = [$dynamic1, $dynamic2];
2729

2830
$this->template->object = new stdClass();
31+
$this->template->boolProperty = $this->property;
2932

3033
$this->template->render(__DIR__ . '/default.latte');
3134
}

tests/Rule/LatteTemplatesRule/SimpleControl/Fixtures/Variables/default.latte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
{php \PHPStan\dumpType($someVariableWithDefault)}
1515

1616
{$object}
17+
18+
{php \PHPStan\dumpType($boolProperty)}

tests/Rule/LatteTemplatesRule/SimpleControl/LatteTemplatesRuleForSimpleControlTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,11 @@ public function testVariables(): void
414414
16,
415415
'default.latte',
416416
],
417+
[
418+
'Dumped type: bool',
419+
18,
420+
'default.latte',
421+
],
417422
]);
418423
}
419424

0 commit comments

Comments
 (0)