-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
date
function can sometimes return numeric string
- Loading branch information
1 parent
25da584
commit 1053ee7
Showing
4 changed files
with
80 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Accessory\AccessoryNumericStringType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeUtils; | ||
|
||
class DateFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'date'; | ||
} | ||
|
||
public function getTypeFromFunctionCall( | ||
FunctionReflection $functionReflection, | ||
FuncCall $functionCall, | ||
Scope $scope | ||
): Type | ||
{ | ||
if (count($functionCall->args) === 0) { | ||
return new StringType(); | ||
} | ||
$argType = $scope->getType($functionCall->args[0]->value); | ||
$constantStrings = TypeUtils::getConstantStrings($argType); | ||
if (count($constantStrings) === 0) { | ||
return new StringType(); | ||
} | ||
|
||
foreach ($constantStrings as $constantString) { | ||
$formattedDate = date($constantString->getValue()); | ||
if (!is_numeric($formattedDate)) { | ||
return new StringType(); | ||
} | ||
} | ||
|
||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNumericStringType(), | ||
]); | ||
} | ||
|
||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Bug2899; | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(string $s, $mixed) | ||
{ | ||
assertType('string&numeric', date('Y')); | ||
assertType('string', date('Y.m.d')); | ||
assertType('string', date($s)); | ||
assertType('string', date($mixed)); | ||
} | ||
|
||
} |