Skip to content

Commit

Permalink
Fixed SimpleXMLElement cast to boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 28, 2020
1 parent b92dc6a commit 83c1e0b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
use PHPStan\Reflection\TrivialParametersAcceptor;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Traits\NonGenericTypeTrait;
use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;

class ObjectType implements TypeWithClassName, SubtractableType
{

use TruthyBooleanTypeTrait;
use NonGenericTypeTrait;
use UndecidedComparisonTypeTrait;

Expand Down Expand Up @@ -396,6 +395,15 @@ public function toArray(): Type
return new ConstantArrayType($arrayKeys, $arrayValues);
}

public function toBoolean(): BooleanType
{
if ($this->isInstanceOf('SimpleXMLElement')->yes()) {
return new BooleanType();
}

return new ConstantBooleanType(true);
}

public function canAccessProperties(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
7 changes: 5 additions & 2 deletions src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
use PHPStan\Reflection\PropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Traits\NonGenericTypeTrait;
use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;

class StaticType implements TypeWithClassName
{

use TruthyBooleanTypeTrait;
use NonGenericTypeTrait;
use UndecidedComparisonTypeTrait;

Expand Down Expand Up @@ -254,6 +252,11 @@ public function toArray(): Type
return $this->getStaticObjectType()->toArray();
}

public function toBoolean(): BooleanType
{
return $this->getStaticObjectType()->toBoolean();
}

public function traverse(callable $cb): Type
{
return $this;
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10419,6 +10419,11 @@ public function dataBug3760(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3760.php');
}

public function dataBug2997(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2997.php');
}

/**
* @param string $file
* @return array<string, mixed[]>
Expand Down Expand Up @@ -10603,6 +10608,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataIncDecInConditions
* @dataProvider dataBug4099
* @dataProvider dataBug3760
* @dataProvider dataBug2997
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2997.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Bug2997;

use function PHPStan\Analyser\assertType;

function (\SimpleXMLElement $xml): void {
assertType('bool', (bool) $xml->Exists);
};

0 comments on commit 83c1e0b

Please sign in to comment.