Skip to content

Commit 2b30830

Browse files
authored
Fix XMLReader::open() return type
1 parent 6d52302 commit 2b30830

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

conf/config.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,12 @@ services:
12331233
tags:
12341234
- phpstan.broker.dynamicFunctionReturnTypeExtension
12351235

1236+
-
1237+
class: PHPStan\Type\Php\XMLReaderOpenReturnTypeExtension
1238+
tags:
1239+
- phpstan.broker.dynamicMethodReturnTypeExtension
1240+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
1241+
12361242
typeSpecifier:
12371243
class: PHPStan\Analyser\TypeSpecifier
12381244
factory: @typeSpecifierFactory::create

resources/functionMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13060,7 +13060,7 @@
1306013060
'XMLReader::moveToFirstAttribute' => ['bool'],
1306113061
'XMLReader::moveToNextAttribute' => ['bool'],
1306213062
'XMLReader::next' => ['bool', 'localname='=>'string'],
13063-
'XMLReader::open' => ['bool', 'uri'=>'string', 'encoding='=>'?string', 'options='=>'int'],
13063+
'XMLReader::open' => ['bool|XMLReader', 'uri'=>'string', 'encoding='=>'?string', 'options='=>'int'],
1306413064
'XMLReader::read' => ['bool'],
1306513065
'XMLReader::readInnerXML' => ['string'],
1306613066
'XMLReader::readOuterXML' => ['string'],
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PhpParser\Node\Expr\StaticCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Reflection\MethodReflection;
9+
use PHPStan\Type\BooleanType;
10+
use PHPStan\Type\Constant\ConstantBooleanType;
11+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
12+
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
13+
use PHPStan\Type\ObjectType;
14+
use PHPStan\Type\Type;
15+
use PHPStan\Type\UnionType;
16+
17+
class XMLReaderOpenReturnTypeExtension implements DynamicMethodReturnTypeExtension, DynamicStaticMethodReturnTypeExtension
18+
{
19+
20+
private const XML_READER_CLASS = 'XMLReader';
21+
22+
public function getClass(): string
23+
{
24+
return self::XML_READER_CLASS;
25+
}
26+
27+
public function isMethodSupported(MethodReflection $methodReflection): bool
28+
{
29+
return $methodReflection->getName() === 'open';
30+
}
31+
32+
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
33+
{
34+
return $this->isMethodSupported($methodReflection);
35+
}
36+
37+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
38+
{
39+
return new BooleanType();
40+
}
41+
42+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
43+
{
44+
return new UnionType([new ObjectType(self::XML_READER_CLASS), new ConstantBooleanType(false)]);
45+
}
46+
47+
}

tests/PHPStan/Rules/Methods/data/call-methods.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,4 +1669,12 @@ public function doFoo(\XMLReader $xml): void
16691669
$xml->open('http://', null);
16701670
}
16711671

1672+
public function openStatically(): void
1673+
{
1674+
$xml = \XMLReader::open('http://', null);
1675+
if ($xml !== false) {
1676+
$xml->read();
1677+
}
1678+
}
1679+
16721680
}

0 commit comments

Comments
 (0)