Skip to content

Commit 83c1e0b

Browse files
committed
Fixed SimpleXMLElement cast to boolean
1 parent b92dc6a commit 83c1e0b

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/Type/ObjectType.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
use PHPStan\Reflection\TrivialParametersAcceptor;
1515
use PHPStan\TrinaryLogic;
1616
use PHPStan\Type\Constant\ConstantArrayType;
17+
use PHPStan\Type\Constant\ConstantBooleanType;
1718
use PHPStan\Type\Constant\ConstantStringType;
1819
use PHPStan\Type\Generic\GenericObjectType;
1920
use PHPStan\Type\Traits\NonGenericTypeTrait;
20-
use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
2121
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
2222

2323
class ObjectType implements TypeWithClassName, SubtractableType
2424
{
2525

26-
use TruthyBooleanTypeTrait;
2726
use NonGenericTypeTrait;
2827
use UndecidedComparisonTypeTrait;
2928

@@ -396,6 +395,15 @@ public function toArray(): Type
396395
return new ConstantArrayType($arrayKeys, $arrayValues);
397396
}
398397

398+
public function toBoolean(): BooleanType
399+
{
400+
if ($this->isInstanceOf('SimpleXMLElement')->yes()) {
401+
return new BooleanType();
402+
}
403+
404+
return new ConstantBooleanType(true);
405+
}
406+
399407
public function canAccessProperties(): TrinaryLogic
400408
{
401409
return TrinaryLogic::createYes();

src/Type/StaticType.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
use PHPStan\Reflection\PropertyReflection;
1010
use PHPStan\TrinaryLogic;
1111
use PHPStan\Type\Traits\NonGenericTypeTrait;
12-
use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
1312
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
1413

1514
class StaticType implements TypeWithClassName
1615
{
1716

18-
use TruthyBooleanTypeTrait;
1917
use NonGenericTypeTrait;
2018
use UndecidedComparisonTypeTrait;
2119

@@ -254,6 +252,11 @@ public function toArray(): Type
254252
return $this->getStaticObjectType()->toArray();
255253
}
256254

255+
public function toBoolean(): BooleanType
256+
{
257+
return $this->getStaticObjectType()->toBoolean();
258+
}
259+
257260
public function traverse(callable $cb): Type
258261
{
259262
return $this;

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10419,6 +10419,11 @@ public function dataBug3760(): array
1041910419
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3760.php');
1042010420
}
1042110421

10422+
public function dataBug2997(): array
10423+
{
10424+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2997.php');
10425+
}
10426+
1042210427
/**
1042310428
* @param string $file
1042410429
* @return array<string, mixed[]>
@@ -10603,6 +10608,7 @@ private function gatherAssertTypes(string $file): array
1060310608
* @dataProvider dataIncDecInConditions
1060410609
* @dataProvider dataBug4099
1060510610
* @dataProvider dataBug3760
10611+
* @dataProvider dataBug2997
1060610612
* @param string $assertType
1060710613
* @param string $file
1060810614
* @param mixed ...$args
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Bug2997;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
function (\SimpleXMLElement $xml): void {
8+
assertType('bool', (bool) $xml->Exists);
9+
};

0 commit comments

Comments
 (0)