Skip to content

Commit 0031d2c

Browse files
VincentLangletondrejmirtes
authored andcommitted
Fix DatePeriodConstructorReturnTypeExtension
1 parent 99efae3 commit 0031d2c

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

src/Type/Php/DatePeriodConstructorReturnTypeExtension.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
3232
return $methodReflection->getName() === '__construct';
3333
}
3434

35-
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
35+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
3636
{
37-
if (!isset($methodCall->getArgs()[0])) {
38-
return new ObjectType(DatePeriod::class);
39-
}
40-
4137
if (!$methodCall->class instanceof Name) {
42-
return new ObjectType(DatePeriod::class);
38+
return null;
4339
}
4440

4541
$className = $scope->resolveName($methodCall->class);
4642
if (strtolower($className) !== 'dateperiod') {
47-
return new ObjectType($className);
43+
return null;
44+
}
45+
46+
if (!isset($methodCall->getArgs()[0])) {
47+
return null;
4848
}
4949

5050
$firstArgType = $scope->getType($methodCall->getArgs()[0]->value);
@@ -80,7 +80,7 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
8080
]);
8181
}
8282

83-
return new ObjectType(DatePeriod::class);
83+
return null;
8484
}
8585

8686
}

tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ public function testBug12274(): void
357357
]);
358358
}
359359

360+
public function testBug13484(): void
361+
{
362+
$this->checkExplicitMixed = true;
363+
$this->checkNullables = true;
364+
365+
$this->analyse([__DIR__ . '/data/bug-13484.php'], []);
366+
}
367+
360368
public function testBug9401(): void
361369
{
362370
$this->checkExplicitMixed = true;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13484;
4+
5+
class MyPeriod extends \DatePeriod {
6+
protected array $arguments;
7+
public function __construct(...$arguments) {
8+
$this->arguments = $arguments;
9+
}
10+
}
11+
12+
function test(): MyPeriod
13+
{
14+
return new MyPeriod();
15+
}

0 commit comments

Comments
 (0)