-
Notifications
You must be signed in to change notification settings - Fork 533
Add DateIntervalFormatDynamicReturnTypeExtension #4165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
37924d1
Add DateIntervalFormatDynamicReturnTypeExtension
VincentLanglet 68ad9c2
Simplify
VincentLanglet b2b2369
Fix
VincentLanglet e6490cd
Fix
VincentLanglet cfa69ca
Add tests
VincentLanglet 2c501da
Simplify
VincentLanglet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/Type/Php/DateIntervalFormatDynamicReturnTypeExtension.php
This file contains hidden or 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,91 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateInterval; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\DependencyInjection\AutowiredService; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\Accessory\AccessoryLowercaseStringType; | ||
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; | ||
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; | ||
use PHPStan\Type\Accessory\AccessoryNumericStringType; | ||
use PHPStan\Type\Accessory\AccessoryUppercaseStringType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use function count; | ||
use function is_numeric; | ||
use function strtolower; | ||
use function strtoupper; | ||
|
||
#[AutowiredService] | ||
final class DateIntervalFormatDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return DateInterval::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'format'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
$arguments = $methodCall->getArgs(); | ||
|
||
if (!isset($arguments[0])) { | ||
return null; | ||
} | ||
|
||
$arg = $scope->getType($arguments[0]->value); | ||
|
||
$constantStrings = $arg->getConstantStrings(); | ||
if (count($constantStrings) === 0) { | ||
if ($arg->isNonEmptyString()->yes()) { | ||
return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
// The worst case scenario for the non-falsy-string check is that every number is 0. | ||
$dateInterval = new DateInterval('P0D'); | ||
|
||
$possibleReturnTypes = []; | ||
foreach ($constantStrings as $string) { | ||
$value = $dateInterval->format($string->getValue()); | ||
|
||
$accessories = []; | ||
if (is_numeric($value)) { | ||
$accessories[] = new AccessoryNumericStringType(); | ||
} | ||
if ($value !== '0' && $value !== '') { | ||
$accessories[] = new AccessoryNonFalsyStringType(); | ||
} elseif ($value !== '') { | ||
$accessories[] = new AccessoryNonEmptyStringType(); | ||
} | ||
if (strtolower($value) === $value) { | ||
$accessories[] = new AccessoryLowercaseStringType(); | ||
} | ||
if (strtoupper($value) === $value) { | ||
$accessories[] = new AccessoryUppercaseStringType(); | ||
} | ||
VincentLanglet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (count($accessories) === 0) { | ||
return null; | ||
} | ||
|
||
$possibleReturnTypes[] = new IntersectionType([new StringType(), ...$accessories]); | ||
} | ||
|
||
return TypeCombinator::union(...$possibleReturnTypes); | ||
} | ||
|
||
} |
This file contains hidden or 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,40 @@ | ||
<?php | ||
|
||
namespace DateIntervalFormat; | ||
|
||
use DateInterval; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
/** | ||
* @param string $string | ||
* @param non-empty-string $nonEmptyString | ||
* @param '%Y'|'%D' $unionString1 | ||
* @param '%Y'|'%y' $unionString2 | ||
* | ||
* @return void | ||
*/ | ||
public function test( | ||
DateInterval $dateInterval, | ||
string $string, | ||
string $nonEmptyString, | ||
string $unionString1, | ||
string $unionString2, | ||
): void { | ||
assertType('string', $dateInterval->format($string)); | ||
assertType('non-empty-string', $dateInterval->format($nonEmptyString)); | ||
|
||
assertType('lowercase-string&non-falsy-string&numeric-string&uppercase-string', $dateInterval->format('%Y')); // '00' | ||
assertType('lowercase-string&non-empty-string&numeric-string&uppercase-string', $dateInterval->format('%y')); // '0' | ||
assertType('lowercase-string&non-falsy-string&numeric-string&uppercase-string', $dateInterval->format($unionString1)); | ||
assertType('lowercase-string&non-empty-string&numeric-string&uppercase-string', $dateInterval->format($unionString2)); | ||
|
||
assertType('non-falsy-string&uppercase-string', $dateInterval->format('%Y DAYS')); | ||
assertType('non-falsy-string&uppercase-string', $dateInterval->format($unionString1. ' DAYS')); | ||
|
||
assertType('lowercase-string&non-falsy-string', $dateInterval->format('%Y days')); | ||
assertType('lowercase-string&non-falsy-string', $dateInterval->format($unionString1. ' days')); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeCombinator::intersect()
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally use directly IntersectionType for string type + accessories because it doesn't require any extra manipulation while intersect does lot of different things useless in this case