-
Notifications
You must be signed in to change notification settings - Fork 478
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
1245ea7
commit aa7989f
Showing
20 changed files
with
339 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^If condition is always false\\.$#" | ||
count: 1 | ||
path: ../src/Type/Php/DateTimeModifyReturnTypeExtension.php | ||
|
||
- | ||
message: "#^Strict comparison using \\=\\=\\= between DateTime and false will always evaluate to false\\.$#" | ||
count: 1 | ||
path: ../src/Type/Php/DateTimeModifyReturnTypeExtension.php |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,71 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodThrowTypeExtension; | ||
use PHPStan\Type\NeverType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use function count; | ||
use function in_array; | ||
|
||
final class DateTimeModifyMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'modify' && in_array($methodReflection->getDeclaringClass()->getName(), [DateTime::class, DateTimeImmutable::class], true); | ||
} | ||
|
||
public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
if (count($methodCall->getArgs()) === 0) { | ||
return null; | ||
} | ||
|
||
if (!$this->phpVersion->hasDateTimeExceptions()) { | ||
return null; | ||
} | ||
|
||
$valueType = $scope->getType($methodCall->getArgs()[0]->value); | ||
$constantStrings = $valueType->getConstantStrings(); | ||
|
||
foreach ($constantStrings as $constantString) { | ||
try { | ||
$dateTime = new DateTime(); | ||
$dateTime->modify($constantString->getValue()); | ||
} catch (\Exception $e) { // phpcs:ignore | ||
return $this->exceptionType(); | ||
} | ||
|
||
$valueType = TypeCombinator::remove($valueType, $constantString); | ||
} | ||
|
||
if (!$valueType instanceof NeverType) { | ||
return $this->exceptionType(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function exceptionType(): Type | ||
{ | ||
if ($this->phpVersion->hasDateTimeExceptions()) { | ||
return new ObjectType('DateMalformedStringException'); | ||
} | ||
|
||
return new ObjectType('Exception'); | ||
} | ||
|
||
} |
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
Oops, something went wrong.