-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix returntype for DateIntervall::createFromDateString
- Loading branch information
1 parent
8c2be72
commit 97efd4d
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateInterval; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use function count; | ||
use function in_array; | ||
|
||
class DateIntervalDynamicReturnTypeExtension | ||
implements DynamicStaticMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return DateInterval::class; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'createFromDateString'; | ||
} | ||
|
||
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type | ||
{ | ||
$arguments = $methodCall->getArgs(); | ||
|
||
if (!isset($arguments[0])) { | ||
return null; | ||
} | ||
|
||
$strings = $scope->getType($arguments[0]->value)->getConstantStrings(); | ||
|
||
$possibleReturnTypes = []; | ||
foreach ($strings as $string) { | ||
$possibleReturnTypes[] = @DateInterval::createFromDateString($string->getValue()) instanceof DateInterval ? DateInterval::class : false; | ||
} | ||
|
||
// the error case, when wrong types are passed | ||
if (count($possibleReturnTypes) === 0) { | ||
return null; | ||
} | ||
|
||
if (in_array(false, $possibleReturnTypes, true) && in_array(DateInterval::class, $possibleReturnTypes, true)) { | ||
return null; | ||
} | ||
|
||
if (in_array(false, $possibleReturnTypes, true)) { | ||
return new ConstantBooleanType(false); | ||
} | ||
|
||
return new ObjectType(DateInterval::class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug8442; | ||
|
||
use stdClass; | ||
use function PHPStan\Testing\assertType; | ||
use DateInterval; | ||
|
||
function () { | ||
assertType('false', DateInterval::createFromDateString('foo')); | ||
assertType('DateInterval', DateInterval::createFromDateString('1 Day')); | ||
|
||
if (rand(0,1)) { | ||
$interval = '1 day'; | ||
} else { | ||
$interval = '2 day'; | ||
} | ||
|
||
assertType('DateInterval', DateInterval::createFromDateString($interval)); | ||
|
||
if (rand(0,1)) { | ||
$interval = 'foo'; | ||
} else { | ||
$interval = '2 day'; | ||
} | ||
|
||
assertType('DateInterval|false', DateInterval::createFromDateString($interval)); | ||
|
||
if (rand(0,1)) { | ||
$interval = 'foo'; | ||
} else { | ||
$interval = 'foo'; | ||
} | ||
|
||
assertType('false', DateInterval::createFromDateString($interval)); | ||
|
||
assertType('DateInterval|false',DateInterval::createFromDateString(str_shuffle('1 day'))); | ||
assertType('DateInterval|false',DateInterval::createFromDateString()); | ||
assertType('DateInterval|false',DateInterval::createFromDateString(new stdClass())); | ||
}; | ||
|