Skip to content
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

Add DateTimeModifyReturnTypeExtension #1267

Merged
merged 4 commits into from May 4, 2022

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Apr 30, 2022

Closes phpstan/phpstan#4927

I think it also closes phpstan/phpstan#1934. There is a lot more method listed:

  • diff
  • format
  • getOffset
  • getTimezone
  • setTime

But I never succeed to get false with those method, except when I passed a parameter with a wrong type.

$dateTime->diff('foo') // false in php 7.4, exception in 8.0

This is reported by phpstan as an error, so I don't think a returnType extension might be needed to say it's return false in php 7.4.

$hasDateTime = false;

foreach ($constantStrings as $constantString) {
if ((new DateTime())->modify($constantString->getValue()) === false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code can cause PHPStan to report warnings: https://3v4l.org/9eHGQ The @ operator should be used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

$valueType = $scope->getType($methodCall->getArgs()[0]->value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will crash if someone calls ->modify() without args.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


use DateTimeImmutable;

class DateTimeImmutableModifyReturnTypeExtension extends DateTimeModifyReturnTypeExtension
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the usage of inheritance here. Instead, the class name should be asked for in the constructor and the class should be registered two times with different arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

if (!$valueType instanceof NeverType) {
return $defaultReturnType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the scenario for this if? I'd rather have you to check count($constantStrings) === 0 first and return $defaultReturnType then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's similar too DateTimeConstructorThrowTypeExtension.

I would say it's to handle a phpdoc like 'foo'|'bar'|somethingElseWhichIsNotAConstantString or a situation

if ($foo) {
     $arg = 'foo';
} else {
    $arg = ... ;
}
$dt->modify($arg);

@VincentLanglet
Copy link
Contributor Author

I don't think the failure are related to my PR @ondrejmirtes

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding standard failed, otherwise it looks good to merge.

@ondrejmirtes
Copy link
Member

Also please note I changed the configuration: c2e3f22 (no backslash needed)

@VincentLanglet
Copy link
Contributor Author

Coding standard failed, otherwise it looks good to merge.

Fixed.

Also, should it target the 1.7.x branch or 1.6.x one ?

@ondrejmirtes
Copy link
Member

1.6.x would be better, thanks.

@VincentLanglet VincentLanglet changed the base branch from 1.7.x to 1.6.x May 4, 2022 14:36
@VincentLanglet
Copy link
Contributor Author

1.6.x would be better, thanks.

Done :)

@ondrejmirtes ondrejmirtes merged commit a8a37ed into phpstan:1.6.x May 4, 2022
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants