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

DateTimeImmutable::modify() can no longer return false since PHP 8.3 #3094

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build/baseline-8.3.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Strict comparison using === between DateTime and false will always evaluate to false\\.$#"
count: 1
path: ../src/Type/Php/DateTimeModifyReturnTypeExtension.php
3 changes: 3 additions & 0 deletions build/ignore-by-php-version.neon.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
$includes[] = __DIR__ . '/enums.neon';
$includes[] = __DIR__ . '/readonly-property.neon';
}
if (PHP_VERSION_ID >= 80300) {
$includes[] = __DIR__ . '/baseline-8.3.neon';
}

if (PHP_VERSION_ID >= 70400) {
$includes[] = __DIR__ . '/ignore-gte-php7.4-errors.neon';
Expand Down
2 changes: 2 additions & 0 deletions resources/functionMap_php83delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
return [
'new' => [
'DateTime::modify' => ['static', 'modify'=>'string'],
'DateTimeImmutable::modify' => ['static', 'modify'=>'string'],
'str_decrement' => ['non-empty-string', 'string'=>'non-empty-string'],
'str_increment' => ['non-falsy-string', 'string'=>'non-empty-string'],
'gc_status' => ['array{running:bool,protected:bool,full:bool,runs:int,collected:int,threshold:int,buffer_size:int,roots:int,application_time:float,collector_time:float,destructor_time:float,free_time:float}'],
Expand Down
19 changes: 14 additions & 5 deletions src/Type/Php/DateTimeModifyReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
$valueType = $scope->getType($methodCall->getArgs()[0]->value);
$constantStrings = $valueType->getConstantStrings();

/** @var bool $hasFalse */
$hasFalse = false;
/** @var bool $hasThrown */
$hasThrown = false;
/** @var bool $hasDateTime */
$hasDateTime = false;

foreach ($constantStrings as $constantString) {
try {
$result = @(new DateTime())->modify($constantString->getValue());
} catch (Throwable) {
$hasFalse = true;
$hasThrown = true;
$valueType = TypeCombinator::remove($valueType, $constantString);
continue;
}
Expand All @@ -71,10 +75,15 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return null;
}

if ($hasFalse && !$hasDateTime) {
return new ConstantBooleanType(false);
}
if ($hasDateTime && !$hasFalse) {
if (!$hasDateTime) {
if ($hasFalse) {
return new ConstantBooleanType(false);
}

if ($hasThrown) {
return new NeverType();
}
} elseif (!$hasFalse && !$hasThrown) {
return $scope->getType($methodCall->var);
}

Expand Down
15 changes: 13 additions & 2 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,13 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeCreateDynamicReturnTypes.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeDynamicReturnTypes.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeModifyReturnTypes.php');

if (PHP_VERSION_ID < 80300) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeModifyReturnTypes.php');
} else {
yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeModifyReturnTypes-83.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4821.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4838.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4879.php');
Expand Down Expand Up @@ -1058,7 +1064,12 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-str-containing-fns.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/fizz-buzz.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4875.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6609.php');

if (PHP_VERSION_ID < 80300) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6609.php');
} else {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6609-83.php');
}

//define('ALREADY_DEFINED_CONSTANT', true);
//yield from $this->gatherAssertTypes(__DIR__ . '/data/already-defined-constant.php');
Expand Down
40 changes: 40 additions & 0 deletions tests/PHPStan/Analyser/data/DateTimeModifyReturnTypes-83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace DateTimeModifyReturnTypes83;

use DateTime;
use DateTimeImmutable;
use function PHPStan\Testing\assertType;

class Foo
{
public function modify(DateTime $datetime, DateTimeImmutable $dateTimeImmutable, string $modify): void {
assertType('DateTime', $datetime->modify($modify));
assertType('DateTimeImmutable', $dateTimeImmutable->modify($modify));
}

/**
* @param '+1 day'|'+2 day' $modify
*/
public function modifyWithValidConstant(DateTime $datetime, DateTimeImmutable $dateTimeImmutable, string $modify): void {
assertType('DateTime', $datetime->modify($modify));
assertType('DateTimeImmutable', $dateTimeImmutable->modify($modify));
}

/**
* @param 'kewk'|'koko' $modify
*/
public function modifyWithInvalidConstant(DateTime $datetime, DateTimeImmutable $dateTimeImmutable, string $modify): void {
assertType('*NEVER*', $datetime->modify($modify));
assertType('*NEVER*', $dateTimeImmutable->modify($modify));
}

/**
* @param '+1 day'|'koko' $modify
*/
public function modifyWithBothConstant(DateTime $datetime, DateTimeImmutable $dateTimeImmutable, string $modify): void {
assertType('DateTime', $datetime->modify($modify));
assertType('DateTimeImmutable', $dateTimeImmutable->modify($modify));
}

}
58 changes: 58 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6609-83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Bug6609Php83;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* This method should return the same type as a parameter passed.
*
* @template T of \DateTime|\DateTimeImmutable
*
* @param T $date
*
* @return T
*/
function modify(\DateTimeInterface $date) {
$date = $date->modify('+1 day');
assertType('T of DateTime|DateTimeImmutable (method Bug6609Php83\Foo::modify(), argument)', $date);

return $date;
}

/**
* This method should return the same type as a parameter passed.
*
* @template T of \DateTime|\DateTimeImmutable
*
* @param T $date
*
* @return T
*/
function modify2(\DateTimeInterface $date) {
$date = $date->modify('invalidd');
assertType('*NEVER*', $date);

return $date;
}

/**
* This method should return the same type as a parameter passed.
*
* @template T of \DateTime|\DateTimeImmutable
*
* @param T $date
*
* @return T
*/
function modify3(\DateTimeInterface $date, string $s) {
$date = $date->modify($s);
assertType('DateTime|DateTimeImmutable', $date);

return $date;
}

}
26 changes: 16 additions & 10 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,16 +812,22 @@ public function testBug7519(): void
public function testBug8223(): void
{
$this->checkBenevolentUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-8223.php'], [
[
'Method Bug8223\HelloWorld::sayHello() should return DateTimeImmutable but returns (DateTimeImmutable|false).',
11,
],
[
'Method Bug8223\HelloWorld::sayHello2() should return array<DateTimeImmutable> but returns array<int, (DateTimeImmutable|false)>.',
21,
],
]);
$errors = [];

if (PHP_VERSION_ID < 80300) {
$errors = [
[
'Method Bug8223\HelloWorld::sayHello() should return DateTimeImmutable but returns (DateTimeImmutable|false).',
11,
],
[
'Method Bug8223\HelloWorld::sayHello2() should return array<DateTimeImmutable> but returns array<int, (DateTimeImmutable|false)>.',
21,
],
];
}

$this->analyse([__DIR__ . '/data/bug-8223.php'], $errors);
}

public function testBug8146bErrors(): void
Expand Down
Loading