Skip to content

Commit f29f6e7

Browse files
committed
Merge pull request #68 from cakephp/fix-hhvm
Fixing HHVM
2 parents 47445b4 + ca99c8d commit f29f6e7

12 files changed

+71
-34
lines changed

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ php:
88

99
sudo: false
1010

11+
cache:
12+
directories:
13+
- vendor
14+
- $HOME/.composer/cache
15+
1116
env:
1217
global:
1318
- DEFAULT=1

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"php": ">=5.5.10"
3030
},
3131
"require-dev": {
32-
"phpunit/phpunit": "~4.0",
32+
"phpunit/phpunit": "*",
3333
"athletic/athletic": "~0.1",
3434
"cakephp/cakephp-codesniffer": "dev-master"
3535
},

src/Chronos.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
7575
* @param string|null $time Fixed or relative time
7676
* @param DateTimeZone|string|null $tz The timezone for the instance
7777
*/
78-
public function __construct($time = null, $tz = null)
78+
public function __construct($time = 'now', $tz = null)
7979
{
8080
if ($tz !== null) {
8181
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
8282
}
8383

8484
if (static::$testNow === null) {
85-
return parent::__construct($time, $tz);
85+
return parent::__construct($time === null ? 'now' : $time, $tz);
8686
}
8787

8888
$relative = static::hasRelativeKeywords($time);

src/ChronosInterval.php

+42-16
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class ChronosInterval extends DateInterval
8383
*/
8484
const PHP_DAYS_FALSE = -99999;
8585

86+
/**
87+
* Whether or not this object was created in HHVM
88+
*
89+
* @var bool
90+
*/
91+
protected $isHHVM = false;
92+
8693
/**
8794
* Determine if the interval was created via DateTime:diff() or not.
8895
*
@@ -107,6 +114,7 @@ protected static function wasCreatedFromDiff(DateInterval $interval)
107114
*/
108115
public function __construct($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null)
109116
{
117+
$this->isHHVM = defined('HHVM_VERSION');
110118
$spec = static::PERIOD_PREFIX;
111119

112120
$spec .= $years > 0 ? $years . static::PERIOD_YEARS : '';
@@ -238,30 +246,39 @@ public function __get($name)
238246
{
239247
switch ($name) {
240248
case 'years':
241-
return $this->y;
249+
return $this->isHHVM ? parent::__get('y') : $this->y;
242250

243251
case 'months':
244-
return $this->m;
252+
return $this->isHHVM ? parent::__get('m') : $this->m;
245253

246254
case 'dayz':
247-
return $this->d;
255+
return $this->isHHVM ? parent::__get('d') : $this->d;
248256

249257
case 'hours':
250-
return $this->h;
258+
return $this->isHHVM ? parent::__get('h') : $this->h;
251259

252260
case 'minutes':
253-
return $this->i;
261+
return $this->isHHVM ? parent::__get('i') : $this->i;
254262

255263
case 'seconds':
256-
return $this->s;
264+
return $this->isHHVM ? parent::__get('s') : $this->s;
257265

258266
case 'weeks':
259-
return (int)floor($this->d / ChronosInterface::DAYS_PER_WEEK);
267+
return (int)floor(($this->isHHVM ? parent::__get('d') : $this->d) / ChronosInterface::DAYS_PER_WEEK);
260268

261269
case 'daysExcludeWeeks':
262270
case 'dayzExcludeWeeks':
263-
return $this->d % ChronosInterface::DAYS_PER_WEEK;
264-
271+
return $this->dayz % ChronosInterface::DAYS_PER_WEEK;
272+
case 'days':
273+
return $this->isHHVM ? parent::__get('days') : $this->days;
274+
case 'y':
275+
case 'm':
276+
case 'd':
277+
case 'h':
278+
case 'i':
279+
case 's':
280+
case 'invert':
281+
return parent::__get($name);
265282
default:
266283
throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name));
267284
}
@@ -279,32 +296,41 @@ public function __set($name, $val)
279296
{
280297
switch ($name) {
281298
case 'years':
282-
$this->y = $val;
299+
$this->isHHVM ? parent::__set('y', $val) : $this->y = $val;
283300
break;
284301

285302
case 'months':
286-
$this->m = $val;
303+
$this->isHHVM ? parent::__set('m', $val) : $this->m = $val;
287304
break;
288305

289306
case 'weeks':
290-
$this->d = $val * ChronosInterface::DAYS_PER_WEEK;
307+
$val = $val * ChronosInterface::DAYS_PER_WEEK;
308+
$this->isHHVM ? parent::__set('d', $val) : $this->d = $val;
291309
break;
292310

293311
case 'dayz':
294-
$this->d = $val;
312+
$this->isHHVM ? parent::__set('d', $val) : $this->d = $val;
295313
break;
296314

297315
case 'hours':
298-
$this->h = $val;
316+
$this->isHHVM ? parent::__set('h', $val) : $this->h = $val;
299317
break;
300318

301319
case 'minutes':
302-
$this->i = $val;
320+
$this->isHHVM ? parent::__set('i', $val) : $this->i = $val;
303321
break;
304322

305323
case 'seconds':
306-
$this->s = $val;
324+
$this->isHHVM ? parent::__set('s', $val) : $this->s = $val;
325+
break;
326+
327+
case 'invert':
328+
$this->isHHVM ? parent::__set('invert', $val) : $this->invert = $val;
307329
break;
330+
default:
331+
if ($this->isHHVM) {
332+
parent::__set($name, $val);
333+
}
308334
}
309335
}
310336

src/Date.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Date extends DateTimeImmutable implements ChronosInterface
5353
* @param string|null $time Fixed or relative time
5454
* @param DateTimeZone|string|null $tz The timezone for the instance
5555
*/
56-
public function __construct($time = null, $tz = null)
56+
public function __construct($time = 'now', $tz = null)
5757
{
5858
$tz = new DateTimeZone('UTC');
5959
if (static::$testNow === null) {
@@ -203,7 +203,7 @@ public function setTimezone($value)
203203
public function setTimestamp($value)
204204
{
205205
$date = date('Y-m-d 00:00:00', $value);
206-
return parent::setTimestamp(strtotime($value));
206+
return parent::setTimestamp(strtotime($date));
207207
}
208208

209209
/**

src/MutableDate.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class MutableDate extends DateTime implements ChronosInterface
5454
* @param string|null $time Fixed or relative time
5555
* @param DateTimeZone|string|null $tz The timezone for the instance
5656
*/
57-
public function __construct($time = null, $tz = null)
57+
public function __construct($time = 'now', $tz = null)
5858
{
5959
$tz = new DateTimeZone('UTC');
6060
if (static::$testNow === null) {

src/MutableDateTime.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class MutableDateTime extends DateTime implements ChronosInterface
5050
* @param string|null $time Fixed or relative time
5151
* @param DateTimeZone|string|null $tz The timezone for the instance
5252
*/
53-
public function __construct($time = null, $tz = null)
53+
public function __construct($time = 'now', $tz = null)
5454
{
5555
if ($tz !== null) {
5656
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
5757
}
5858

5959
if (static::$testNow === null) {
60-
return parent::__construct($time, $tz);
60+
return parent::__construct($time === null ? 'now' : $time, $tz);
6161
}
6262

6363
$relative = static::hasRelativeKeywords($time);

src/Traits/DifferenceTrait.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\Chronos\ChronosInterval;
1717
use Cake\Chronos\DifferenceFormatter;
1818
use DatePeriod;
19+
use DateTimeImmutable;
1920
use DateTimeInterface;
2021

2122
/**
@@ -128,6 +129,11 @@ public function diffFiltered(ChronosInterval $ci, callable $callback, ChronosInt
128129
$end = $dt === null ? static::now($this->tz) : $dt;
129130
$inverse = false;
130131

132+
if (defined('HHVM_VERSION')) {
133+
$start = new DateTimeImmutable($this->toIso8601String());
134+
$end = new DateTimeImmutable($end->toIso8601String());
135+
}
136+
131137
if ($end < $start) {
132138
$start = $end;
133139
$end = $this;

src/Traits/FactoryTrait.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public static function instance(DateTimeInterface $dt)
4141
* ChronosInterface::parse('Monday next week')->fn() rather than
4242
* (new Chronos('Monday next week'))->fn()
4343
*
44-
* @param string|null $time The strtotime compatible string to parse
44+
* @param string $time The strtotime compatible string to parse
4545
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name.
4646
* @return $this
4747
*/
48-
public static function parse($time = null, $tz = null)
48+
public static function parse($time = 'now', $tz = null)
4949
{
5050
return new static($time, $tz);
5151
}
@@ -58,7 +58,7 @@ public static function parse($time = null, $tz = null)
5858
*/
5959
public static function now($tz = null)
6060
{
61-
return new static(null, $tz);
61+
return new static('now', $tz);
6262
}
6363

6464
/**

src/Traits/ModifierTrait.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ public function nthOfMonth($nth, $dayOfWeek)
906906
$check = $dt->format('Y-m');
907907
$dt = $dt->modify("+$nth " . static::$days[$dayOfWeek]);
908908

909-
return ($dt->format('Y-m') === $check) ? $this->modify($dt) : false;
909+
return ($dt->format('Y-m') === $check) ? $dt : false;
910910
}
911911

912912
/**
@@ -954,7 +954,7 @@ public function nthOfQuarter($nth, $dayOfWeek)
954954
$year = $dt->year;
955955
$dt = $dt->firstOfQuarter()->modify("+$nth" . static::$days[$dayOfWeek]);
956956

957-
return ($lastMonth < $dt->month || $year !== $dt->year) ? false : $this->modify($dt);
957+
return ($lastMonth < $dt->month || $year !== $dt->year) ? false : $dt;
958958
}
959959

960960
/**
@@ -1000,7 +1000,7 @@ public function lastOfYear($dayOfWeek = null)
10001000
public function nthOfYear($nth, $dayOfWeek)
10011001
{
10021002
$dt = $this->copy()->firstOfYear()->modify("+$nth " . static::$days[$dayOfWeek]);
1003-
return $this->year === $dt->year ? $this->modify($dt) : false;
1003+
return $this->year === $dt->year ? $dt : false;
10041004
}
10051005

10061006
/**

tests/Date/TimezoneTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testNoopOnTimezoneChange($method)
3737
$date = new Date('2015-01-01');
3838
$new = $date->{$method}($tz);
3939
$this->assertSame($new, $date);
40-
$this->assertNotEquals($tz, $date->timezone);
40+
$this->assertNotSame($tz, $new->timezone);
4141
}
4242

4343
/**
@@ -51,6 +51,6 @@ public function testNoopOnTimezoneChangeMutableDate($method)
5151
$date = new MutableDate('2015-01-01');
5252
$new = $date->{$method}($tz);
5353
$this->assertSame($new, $date);
54-
$this->assertNotEquals($tz, $date->timezone);
54+
$this->assertNotSame($tz, $date->timezone);
5555
}
5656
}

tests/Interval/IntervalConstructTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function testInstance()
220220
$ci = ChronosInterval::instance(new DateInterval('P2Y1M5DT22H33M44S'));
221221
$this->assertInstanceOf(ChronosInterval::class, $ci);
222222
$this->assertDateTimeInterval($ci, 2, 1, 5, 22, 33, 44);
223-
$this->assertTrue($ci->days === false || $ci->days === ChronosInterval::PHP_DAYS_FALSE);
223+
$this->assertTrue($ci->days === false || $ci->days === 0 || $ci->days === ChronosInterval::PHP_DAYS_FALSE);
224224
}
225225

226226
public function testInstanceWithNegativeDateInterval()
@@ -230,7 +230,7 @@ public function testInstanceWithNegativeDateInterval()
230230
$ci = ChronosInterval::instance($di);
231231
$this->assertInstanceOf(ChronosInterval::class, $ci);
232232
$this->assertDateTimeInterval($ci, 2, 1, 5, 22, 33, 44);
233-
$this->assertTrue($ci->days === false || $ci->days === ChronosInterval::PHP_DAYS_FALSE);
233+
$this->assertTrue($ci->days === false || $ci->days === 0 || $ci->days === ChronosInterval::PHP_DAYS_FALSE);
234234
$this->assertSame(1, $ci->invert);
235235
}
236236

0 commit comments

Comments
 (0)