2022-09-06 11:05:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2023-07-21 21:50:30 +02:00
|
|
|
use Exception;
|
2023-07-19 23:52:25 +02:00
|
|
|
use Psalm\Context;
|
2022-09-06 11:05:40 +02:00
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2023-07-21 22:57:49 +02:00
|
|
|
use const PHP_VERSION_ID;
|
2023-07-21 21:50:30 +02:00
|
|
|
|
2022-09-06 11:05:40 +02:00
|
|
|
class DateTimeTest extends TestCase
|
|
|
|
{
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
|
2023-07-19 23:52:25 +02:00
|
|
|
public function testModifyWithInvalidConstant(): void
|
|
|
|
{
|
|
|
|
$context = new Context();
|
|
|
|
|
2023-07-21 22:42:27 +02:00
|
|
|
if (PHP_VERSION_ID >= 8_03_00) {
|
2023-07-21 21:50:30 +02:00
|
|
|
$this->expectException(Exception::class);
|
2023-07-19 23:52:25 +02:00
|
|
|
$this->expectExceptionMessage('DateTime::modify(): Failed to parse time string (foo) at position 0 (f)');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return "foo"|"bar"
|
|
|
|
*/
|
|
|
|
function getString(): string
|
|
|
|
{
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
$datetime = new DateTime();
|
|
|
|
$dateTimeImmutable = new DateTimeImmutable();
|
|
|
|
$a = $datetime->modify(getString());
|
|
|
|
$b = $dateTimeImmutable->modify(getString());',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
|
|
|
|
$this->assertSame('false', $context->vars_in_scope['$a']->getId(true));
|
|
|
|
$this->assertSame('false', $context->vars_in_scope['$b']->getId(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testModifyWithBothConstant(): void
|
|
|
|
{
|
|
|
|
$context = new Context();
|
|
|
|
|
2023-07-21 22:57:49 +02:00
|
|
|
if (PHP_VERSION_ID >= 8_03_00) {
|
2023-07-21 21:50:30 +02:00
|
|
|
$this->expectException(Exception::class);
|
2023-07-19 23:52:25 +02:00
|
|
|
$this->expectExceptionMessage('DateTime::modify(): Failed to parse time string (bar) at position 0 (b)');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return "+1 day"|"bar"
|
|
|
|
*/
|
|
|
|
function getString(): string
|
|
|
|
{
|
|
|
|
return "+1 day";
|
|
|
|
}
|
|
|
|
|
|
|
|
$datetime = new DateTime();
|
|
|
|
$dateTimeImmutable = new DateTimeImmutable();
|
|
|
|
$a = $datetime->modify(getString());
|
|
|
|
$b = $dateTimeImmutable->modify(getString());',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
|
|
|
|
$this->assertSame('DateTime|false', $context->vars_in_scope['$a']->getId(false));
|
|
|
|
$this->assertSame('DateTimeImmutable|false', $context->vars_in_scope['$b']->getId(false));
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:05:40 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'modify' => [
|
2022-10-16 13:59:15 +02:00
|
|
|
'code' => '<?php
|
2022-09-06 11:05:40 +02:00
|
|
|
function getString(): string
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$datetime = new DateTime();
|
|
|
|
$dateTimeImmutable = new DateTimeImmutable();
|
|
|
|
$a = $datetime->modify(getString());
|
|
|
|
$b = $dateTimeImmutable->modify(getString());
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'DateTime|false',
|
|
|
|
'$b' => 'DateTimeImmutable|false',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'modifyWithValidConstant' => [
|
2022-10-16 13:59:15 +02:00
|
|
|
'code' => '<?php
|
2022-09-06 11:05:40 +02:00
|
|
|
/**
|
|
|
|
* @return "+1 day"|"+2 day"
|
|
|
|
*/
|
|
|
|
function getString(): string
|
|
|
|
{
|
|
|
|
return "+1 day";
|
|
|
|
}
|
|
|
|
|
|
|
|
$datetime = new DateTime();
|
|
|
|
$dateTimeImmutable = new DateTimeImmutable();
|
|
|
|
$a = $datetime->modify(getString());
|
|
|
|
$b = $dateTimeImmutable->modify(getString());
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2023-01-24 11:32:09 +01:00
|
|
|
'$a' => 'DateTime',
|
|
|
|
'$b' => 'DateTimeImmutable',
|
2022-09-06 11:05:40 +02:00
|
|
|
],
|
|
|
|
],
|
2023-01-24 11:32:09 +01:00
|
|
|
'otherMethodAfterModify' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$datetime = new DateTime();
|
|
|
|
$dateTimeImmutable = new DateTimeImmutable();
|
|
|
|
$a = $datetime->modify("+1 day")->setTime(0, 0);
|
|
|
|
$b = $dateTimeImmutable->modify("+1 day")->setTime(0, 0);
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2023-03-06 11:26:05 +01:00
|
|
|
'$a' => 'DateTime',
|
2023-01-24 11:32:09 +01:00
|
|
|
'$b' => 'DateTimeImmutable',
|
|
|
|
],
|
|
|
|
],
|
2023-01-03 12:09:15 +01:00
|
|
|
'modifyStaticReturn' => [
|
|
|
|
'code' => '<?php
|
|
|
|
|
|
|
|
class Subclass extends DateTimeImmutable
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo = new Subclass("2023-01-01 12:12:13");
|
|
|
|
$mod = $foo->modify("+7 days");
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2023-01-24 11:32:09 +01:00
|
|
|
'$mod' => 'Subclass',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'otherMethodAfterModifyStaticReturn' => [
|
|
|
|
'code' => '<?php
|
|
|
|
|
|
|
|
class Subclass extends DateTimeImmutable
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
$datetime = new Subclass();
|
|
|
|
$mod = $datetime->modify("+1 day")->setTime(0, 0);
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$mod' => 'Subclass',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'formatAfterModify' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$datetime = new DateTime();
|
|
|
|
$dateTimeImmutable = new DateTimeImmutable();
|
|
|
|
$a = $datetime->modify("+1 day")->format("Y-m-d");
|
|
|
|
$b = $dateTimeImmutable->modify("+1 day")->format("Y-m-d");
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'false|string',
|
|
|
|
'$b' => 'string',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'formatAfterModifyStaticReturn' => [
|
|
|
|
'code' => '<?php
|
|
|
|
|
|
|
|
class Subclass extends DateTimeImmutable
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
$datetime = new Subclass();
|
|
|
|
$format = $datetime->modify("+1 day")->format("Y-m-d");
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$format' => 'string',
|
2023-01-03 12:09:15 +01:00
|
|
|
],
|
|
|
|
],
|
2022-09-06 11:05:40 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|