-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add return type provider for date/gmdate
- Loading branch information
Showing
5 changed files
with
91 additions
and
12 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,5 @@ | ||
|
||
Psalm\Type\Atomic\TLiteralInt | ||
Psalm\Type\Atomic\TMixed | ||
Psalm\Type\Atomic\TLiteralInt | ||
Psalm\Type\Atomic\TMixed |
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
66 changes: 66 additions & 0 deletions
66
src/Psalm/Internal/Provider/ReturnTypeProvider/DateReturnTypeProvider.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psalm\Internal\Provider\ReturnTypeProvider; | ||
|
||
use Psalm\Internal\Analyzer\StatementsAnalyzer; | ||
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; | ||
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface; | ||
use Psalm\Type; | ||
use Psalm\Type\Union; | ||
|
||
use function array_values; | ||
use function preg_match; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class DateReturnTypeProvider implements FunctionReturnTypeProviderInterface | ||
{ | ||
/** | ||
* @return array<lowercase-string> | ||
*/ | ||
public static function getFunctionIds(): array | ||
{ | ||
return ['date', 'gmdate']; | ||
} | ||
|
||
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union | ||
{ | ||
$source = $event->getStatementsSource(); | ||
if (!$source instanceof StatementsAnalyzer) { | ||
return null; | ||
} | ||
|
||
$call_args = $event->getCallArgs(); | ||
|
||
if (isset($call_args[0])) { | ||
$type = $source->node_data->getType($call_args[0]->value); | ||
if ($type !== null && $type->isSingle()) { | ||
$atomic_type = array_values($type->getAtomicTypes())[0]; | ||
if ($atomic_type instanceof Type\Atomic\TLiteralString | ||
&& ($format_val = $atomic_type->value) | ||
&& preg_match('/[djNwzWmntLoYyBgGhHisuvZUI]+/', $format_val) | ||
) { | ||
return Type::getNumericString(); | ||
} | ||
} | ||
} | ||
|
||
if (!isset($call_args[1])) { | ||
return Type::getString(); | ||
} | ||
|
||
$type = $source->node_data->getType($call_args[1]->value); | ||
if ($type !== null && $type->isSingle()) { | ||
$atomic_type = array_values($type->getAtomicTypes())[0]; | ||
if ($atomic_type instanceof Type\Atomic\TNumeric | ||
|| $atomic_type instanceof Type\Atomic\TInt | ||
) { | ||
return Type::getString(); | ||
} | ||
} | ||
return Type::combineUnionTypes(Type::getString(), Type::getFalse()); | ||
} | ||
} |
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