-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f9749a
commit b49df58
Showing
7 changed files
with
163 additions
and
10 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,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use function count; | ||
|
||
class DateFormatFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'date_format'; | ||
} | ||
|
||
public function getTypeFromFunctionCall( | ||
FunctionReflection $functionReflection, | ||
FuncCall $functionCall, | ||
Scope $scope, | ||
): Type | ||
{ | ||
if (count($functionCall->getArgs()) < 2) { | ||
return new StringType(); | ||
} | ||
|
||
return $scope->getType( | ||
new FuncCall(new FullyQualified('date'), [ | ||
$functionCall->getArgs()[1], | ||
]), | ||
); | ||
} | ||
|
||
} |
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,42 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateTimeInterface; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use function count; | ||
|
||
class DateFormatMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return DateTimeInterface::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'format'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
if (count($methodCall->getArgs()) === 0) { | ||
return new StringType(); | ||
} | ||
|
||
return $scope->getType( | ||
new FuncCall(new FullyQualified('date'), [ | ||
$methodCall->getArgs()[0], | ||
]), | ||
); | ||
} | ||
|
||
} |
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
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,45 @@ | ||
<?php | ||
|
||
namespace DateFormatReturnType; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
function (string $s): void { | ||
assertType('\'\'', date('')); | ||
assertType('string', date($s)); | ||
assertType('non-empty-string', date('D')); | ||
assertType('numeric-string', date('Y')); | ||
assertType('numeric-string', date('Ghi')); | ||
}; | ||
|
||
function (\DateTime $dt, string $s): void { | ||
assertType('\'\'', date_format($dt, '')); | ||
assertType('string', date_format($dt, $s)); | ||
assertType('non-empty-string', date_format($dt, 'D')); | ||
assertType('numeric-string', date_format($dt, 'Y')); | ||
assertType('numeric-string', date_format($dt, 'Ghi')); | ||
}; | ||
|
||
function (\DateTimeInterface $dt, string $s): void { | ||
assertType('\'\'', $dt->format('')); | ||
assertType('string', $dt->format($s)); | ||
assertType('non-empty-string', $dt->format('D')); | ||
assertType('numeric-string', $dt->format('Y')); | ||
assertType('numeric-string', $dt->format('Ghi')); | ||
}; | ||
|
||
function (\DateTime $dt, string $s): void { | ||
assertType('\'\'', $dt->format('')); | ||
assertType('string', $dt->format($s)); | ||
assertType('non-empty-string', $dt->format('D')); | ||
assertType('numeric-string', $dt->format('Y')); | ||
assertType('numeric-string', $dt->format('Ghi')); | ||
}; | ||
|
||
function (\DateTimeImmutable $dt, string $s): void { | ||
assertType('\'\'', $dt->format('')); | ||
assertType('string', $dt->format($s)); | ||
assertType('non-empty-string', $dt->format('D')); | ||
assertType('numeric-string', $dt->format('Y')); | ||
assertType('numeric-string', $dt->format('Ghi')); | ||
}; |