From 75e2be8aec41751de50e59d5d7cc53e9326c51ab Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Mon, 18 Jun 2018 10:07:05 -0400 Subject: [PATCH] Fix #825 - ignore falsable issues on builtin *methods* --- src/Psalm/CallMap.php | 4 ++-- .../Statements/Expression/Call/MethodCallChecker.php | 3 +++ tests/MethodCallTest.php | 9 +++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Psalm/CallMap.php b/src/Psalm/CallMap.php index e5b061c7f..ccdcc4f1b 100644 --- a/src/Psalm/CallMap.php +++ b/src/Psalm/CallMap.php @@ -1620,7 +1620,7 @@ return [ 'DateTime::add' => ['static', 'interval'=>'DateInterval'], 'DateTime::createFromFormat' => ['static|false', 'format'=>'string', 'time'=>'string', 'timezone='=>'DateTimeZone'], 'DateTime::diff' => ['DateInterval', 'datetime2'=>'DateTimeInterface', 'absolute='=>'bool'], -'DateTime::format' => ['string', 'format'=>'string'], +'DateTime::format' => ['string|false', 'format'=>'string'], 'DateTime::getLastErrors' => ['array'], 'DateTime::getOffset' => ['int'], 'DateTime::getTimestamp' => ['int'], @@ -1639,7 +1639,7 @@ return [ 'DateTimeImmutable::createFromFormat' => ['static', 'format'=>'string', 'time'=>'string', 'timezone='=>'DateTimeZone'], 'DateTimeImmutable::createFromMutable' => ['static', 'datetime'=>'DateTime'], 'DateTimeImmutable::diff' => ['DateInterval', 'datetime2'=>'DateTimeInterface', 'absolute='=>'bool'], -'DateTimeImmutable::format' => ['string', 'format'=>'string'], +'DateTimeImmutable::format' => ['string|false', 'format'=>'string'], 'DateTimeImmutable::getLastErrors' => ['array'], 'DateTimeImmutable::getOffset' => ['int'], 'DateTimeImmutable::getTimestamp' => ['int'], diff --git a/src/Psalm/Checker/Statements/Expression/Call/MethodCallChecker.php b/src/Psalm/Checker/Statements/Expression/Call/MethodCallChecker.php index 923799b8f..ce6935a0a 100644 --- a/src/Psalm/Checker/Statements/Expression/Call/MethodCallChecker.php +++ b/src/Psalm/Checker/Statements/Expression/Call/MethodCallChecker.php @@ -522,6 +522,9 @@ class MethodCallChecker extends \Psalm\Checker\Statements\Expression\CallChecker $return_type_candidate = Type::parseString('string|false'); } else { $return_type_candidate = CallMap::getReturnTypeFromCallMap($call_map_id); + if ($return_type_candidate->isFalsable()) { + $return_type_candidate->ignore_falsable_issues = true; + } } } diff --git a/tests/MethodCallTest.php b/tests/MethodCallTest.php index 33f28b7e0..639227d0a 100644 --- a/tests/MethodCallTest.php +++ b/tests/MethodCallTest.php @@ -146,6 +146,15 @@ class MethodCallTest extends TestCase '$b' => 'string|bool', ], ], + 'datetimeformatNotFalse' => [ + 'format($format); + if (false !== $formatted) {} + function takesString(string $s) : void {} + takesString($formatted);' + ], ]; }