Skip to content

Commit

Permalink
Ignore deprecation notices for setters
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jul 24, 2022
1 parent 8b77f5f commit 8402848
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 22 deletions.
9 changes: 0 additions & 9 deletions src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -1506,15 +1506,6 @@ public function spec()
public function cast(string $className)
{
if (!method_exists($className, 'instance')) {
if (is_a($className, self::class, true)) {
return new $className(
$this->getStartDate(),
$this->getDateInterval(),
$this->getEndDate() ? $this->getIncludedEndDate() : $this->getRecurrences(),
$this->isStartExcluded() ? DatePeriod::EXCLUDE_START_DATE : 0
);
}

if (is_a($className, DatePeriod::class, true)) {
return new $className(
$this->rawDate($this->getStartDate()),
Expand Down
14 changes: 7 additions & 7 deletions tests/Carbon/SerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ public function testDateSerializationReflectionCompatibility()
{
$d = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();

$d->date = '1990-01-17 10:28:07';
$d->timezone_type = 3;
$d->timezone = 'US/Pacific';
@$d->date = '1990-01-17 10:28:07';
@$d->timezone_type = 3;
@$d->timezone = 'US/Pacific';

$x = unserialize(serialize($d));

$this->assertSame('1990-01-17 10:28:07', $x->format('Y-m-d h:i:s'));

$d = (new ReflectionClass(Carbon::class))->newInstanceWithoutConstructor();

$d->date = '1990-01-17 10:28:07';
$d->timezone_type = 3;
$d->timezone = 'US/Pacific';
@$d->date = '1990-01-17 10:28:07';
@$d->timezone_type = 3;
@$d->timezone = 'US/Pacific';

$x = unserialize(serialize($d));

Expand All @@ -128,7 +128,7 @@ public function testDateSerializationReflectionCompatibility()
return;
}

$target->$key = $value;
@$target->$key = $value;
};

$setValue('date', '1990-01-17 10:28:07');
Expand Down
6 changes: 3 additions & 3 deletions tests/Carbon/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public function testTimeZoneOfUnserialized()

$this->assertSame('America/Vancouver', $new->getTimezone()->getName());

$new->timezone = 'UTC';
@$new->timezone = 'UTC';

$this->assertSame('UTC', $new->getTimezone()->getName());

Expand All @@ -303,13 +303,13 @@ public function testTimeZoneOfUnserialized()

$this->assertSame('America/Vancouver', $date->getTimezone()->getName());

$date->timezone = 'UTC';
@$date->timezone = 'UTC';

$this->assertSame('UTC', $date->getTimezone()->getName());

$this->assertSame('America/Vancouver', $new->getTimezone()->getName());

$new->timezone = 'UTC';
@$new->timezone = 'UTC';

$this->assertSame('UTC', $new->getTimezone()->getName());

Expand Down
2 changes: 1 addition & 1 deletion tests/Carbon/StrictModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testSetAndGetWithoutStrictMode()
Carbon::useStrictMode(false);
/** @var mixed $date */
$date = Carbon::now();
$date->foobar = 'biz';
@$date->foobar = 'biz';
$this->assertSame('biz', $date->foobar);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/CarbonInterval/StrictModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testSetAndGetWithoutStrictMode()
Carbon::useStrictMode(false);
/** @var mixed $interval */
$interval = CarbonInterval::day();
$interval->foobar = 'biz';
@$interval->foobar = 'biz';
$this->assertSame('biz', $interval->foobar);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/CarbonPeriod/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public function testNotEqualToFalse()
$this->assertFalse($period->ne(CarbonPeriod::create('R3/2010-01-01/P1D/2010-02-01')));
$this->assertFalse($period->ne(Carbon::parse('2010-01-01')->daysUntil('2010-02-01')));
$this->assertFalse($period->ne(
new DatePeriod(new DateTime('2010-01-01'), CarbonInterval::day(), new DateTime('2010-02-01'))));
new DatePeriod(new DateTime('2010-01-01'), CarbonInterval::day(), new DateTime('2010-02-01'))
));

$period = CarbonPeriod::create('2010-01-01', '2010-02-01', 'P2D');

Expand Down
45 changes: 45 additions & 0 deletions tests/CarbonPeriod/ToDatePeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@

namespace Tests\CarbonPeriod;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Carbon\Translator;
use DatePeriod;
use DateTime;
use DateTimeImmutable;
use ReflectionMethod;
use Tests\AbstractTestCase;

class ToDatePeriodTest extends AbstractTestCase
Expand Down Expand Up @@ -97,4 +102,44 @@ public function testWithModifiedEnglish()

$translator->resetMessages();
}

public function testRawDate()
{
$period = new CarbonPeriod();
$method = new ReflectionMethod(CarbonPeriod::class, 'rawDate');
$method->setAccessible(true);

$this->assertNull($method->invoke($period, false));
$this->assertNull($method->invoke($period, null));

$date = new DateTime();
$this->assertSame($date, $method->invoke($period, $date));

$date = new DateTimeImmutable();
$this->assertSame($date, $method->invoke($period, $date));

$date = new Carbon();
$raw = $method->invoke($period, $date);
$this->assertInstanceOf(DateTime::class, $raw);
$this->assertEquals($date, $raw);

$date = new CarbonImmutable();
$raw = $method->invoke($period, $date);
$this->assertInstanceOf(DateTimeImmutable::class, $raw);
$this->assertEquals($date, $raw);

$date = new class() extends DateTime {
// void
};
$raw = $method->invoke($period, $date);
$this->assertInstanceOf(DateTime::class, $raw);
$this->assertEquals($date, $raw);

$date = new class() extends DateTimeImmutable {
// void
};
$raw = $method->invoke($period, $date);
$this->assertInstanceOf(DateTimeImmutable::class, $raw);
$this->assertEquals($date, $raw);
}
}

0 comments on commit 8402848

Please sign in to comment.