Skip to content

Commit 19395f3

Browse files
hgoldsteinfacebook-github-bot
authored andcommitted
Typecheck ext_datetime (with datetimeinterface.php)
Summary: TSIA: nothing particularly novel here, though I am starting to notice more native functions that are typed as returning `array` and secretly return `null`. There are some functions in this diff that suffer from that, and their typing reflects such. Reviewed By: aorenste Differential Revision: D37928382 fbshipit-source-id: b3b88a1f26632d4fb60e608570a9e10d771c95d6
1 parent 9ef3fc1 commit 19395f3

File tree

3 files changed

+83
-24
lines changed

3 files changed

+83
-24
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?hh
2+
// Copyright (c) 2022, Meta, Inc.
3+
// All rights reserved.
4+
//
5+
// This source code is licensed under the MIT license found in the
6+
// LICENSE file in the "hack" directory of this source tree.
7+
8+
// TODO(T119870250)
9+
// Used as part of typechecking `hphp/runtime/ext/` without builtins; these
10+
// constant values are not representative of the true runtime value.
11+
12+
const int SUNFUNCS_RET_STRING = 0;

hphp/runtime/ext/datetime/ext_datetime.php

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ public function format(mixed $format)[]: string;
7474
*
7575
*/
7676
<<__Native>>
77-
public static function getLastErrors()[read_globals]: darray;
77+
public static function getLastErrors()[read_globals]: shape(
78+
'warning_count' => int,
79+
'warnings' => dict<int, string>,
80+
'error_count' => int,
81+
'errors' => dict<int, string>
82+
);
7883

7984
/**
8085
* Returns the timezone offset.
@@ -201,13 +206,13 @@ public function setTimezone(DateTimeZone $timezone)[write_props]: mixed;
201206
public function sub(DateInterval $interval)[write_props]: mixed;
202207

203208
<<__Native>>
204-
public function __sleep()[write_props]: varray;
209+
public function __sleep()[write_props]: varray<string>;
205210

206211
<<__Native>>
207212
public function __wakeup()[]: void;
208213

209214
<<__Native>>
210-
public function __debugInfo()[]: darray;
215+
public function __debugInfo()[]: darray<string, mixed>;
211216

212217
}
213218

@@ -232,7 +237,12 @@ public function __construct(string $timezone)[]: void;
232237
*
233238
*/
234239
<<__Native>>
235-
public function getLocation()[]: darray;
240+
public function getLocation()[]: shape(
241+
'country_code' => string,
242+
'latitude' => float,
243+
'longitude' => float,
244+
'comments' => string,
245+
);
236246

237247
/**
238248
* Returns the name of the timezone.
@@ -267,12 +277,19 @@ public function getOffset(DateTimeInterface $datetime)[]: mixed;
267277
public function getTransitions(int $timestamp_begin = PHP_INT_MIN,
268278
int $timestamp_end = PHP_INT_MAX)[]: mixed;
269279

280+
281+
const type TAbbrev = shape(
282+
'dst' => bool,
283+
'offset' => float,
284+
'timezone_id' => ?string,
285+
);
270286
/**
271287
* @return array - Returns array on success or FALSE on failure.
272288
*
273289
*/
274290
<<__Native>>
275-
public static function listAbbreviations()[]: darray;
291+
public static function listAbbreviations(
292+
)[]: dict<string, vec<this::TAbbrev>>;
276293

277294
/**
278295
* @param int $what - One of DateTimeZone class constants.
@@ -287,7 +304,7 @@ public static function listIdentifiers(int $what = 2047,
287304
string $country = "")[]: mixed;
288305

289306
<<__Native>>
290-
public function __debugInfo(): darray;
307+
public function __debugInfo(): darray<string, mixed>;
291308
}
292309

293310
/**
@@ -406,7 +423,12 @@ function date_default_timezone_get(): string;
406423
<<__Native>>
407424
function date_default_timezone_set(string $name): bool;
408425

409-
function date_get_last_errors(): darray {
426+
function date_get_last_errors()[read_globals]: shape(
427+
'warning_count' => int,
428+
'warnings' => dict<int, string>,
429+
'error_count' => int,
430+
'errors' => dict<int, string>
431+
) {
410432
return DateTime::getLastErrors();
411433
}
412434

@@ -422,7 +444,7 @@ function date_interval_format(DateInterval $interval,
422444
function date_isodate_set(DateTime $datetime,
423445
int $year,
424446
int $week,
425-
int $day = 1): void {
447+
int $day = 1): DateTime {
426448
return $datetime->setISODate($year, $week, $day);
427449
}
428450

@@ -451,12 +473,19 @@ function date_sub(DateTime $datetime, DateInterval $interval): mixed {
451473
* @param int $ts - Timestamp.
452474
* @param float $latitude - Latitude in degrees.
453475
* @param float $longitude - Longitude in degrees.
454-
*
455-
* @return array - Returns array on success or FALSE on failure.
456-
*
457476
*/
458477
<<__Native>>
459-
function date_sun_info(int $ts, float $latitude, float $longitude): darray;
478+
function date_sun_info(int $ts, float $latitude, float $longitude): shape(
479+
'sunrise' => mixed,
480+
'sunset' => mixed,
481+
'transit' => int,
482+
'civil_twilight_begin' => mixed,
483+
'civil_twilight_end' => mixed,
484+
'nautical_twilight_begin' => mixed,
485+
'nautical_twilight_end' => mixed,
486+
'astronomical_twilight_begin' => mixed,
487+
'astronomical_twilight_end' => mixed,
488+
);
460489

461490
/**
462491
* date_sunrise() returns the sunrise time for a given day (specified as a
@@ -556,7 +585,19 @@ function date(string $format, ?int $timestamp = null): mixed;
556585
*
557586
*/
558587
<<__Native>>
559-
function getdate(?int $timestamp = null): darray;
588+
function getdate(?int $timestamp = null): shape(
589+
'seconds' => int,
590+
'minutes' => int,
591+
'hours' => int,
592+
'mday' => int,
593+
'wday' => int,
594+
'mon' => int,
595+
'year' => int,
596+
'yday' => int,
597+
'weekday' => string,
598+
'month' => string,
599+
...
600+
);
560601

561602
/**
562603
* This is an interface to gettimeofday(2). It returns an associative array
@@ -682,7 +723,7 @@ function idate(string $format, ?int $timestamp = null): mixed;
682723
*/
683724
<<__Native>>
684725
function localtime(?int $timestamp = null,
685-
bool $is_associative = false): varray_or_darray;
726+
bool $is_associative = false): varray_or_darray<mixed>;
686727

687728
/**
688729
* microtime() returns the current Unix timestamp with microseconds. This
@@ -799,7 +840,8 @@ function strtotime(string $input, ?int $timestamp = null)[leak_safe]: mixed;
799840
<<__Native>>
800841
function time()[leak_safe]: int;
801842

802-
function timezone_abbreviations_list(): darray {
843+
function timezone_abbreviations_list(
844+
): ?dict<string, vec<DateTimeZone::TAbbrev>> {
803845
return DateTimeZone::listAbbreviations();
804846
}
805847

@@ -816,7 +858,12 @@ function timezone_identifiers_list(int $what = 2047,
816858
return DateTimeZone::listIdentifiers($what, $country);
817859
}
818860

819-
function timezone_location_get(DateTimeZone $timezone): darray {
861+
function timezone_location_get(DateTimeZone $timezone): ?shape(
862+
'country_code' => string,
863+
'latitude' => float,
864+
'longitude' => float,
865+
'comments' => string,
866+
) {
820867
return $timezone->getLocation();
821868
}
822869

@@ -853,7 +900,7 @@ function timezone_open(string $timezone): mixed {
853900
catch (Exception $e) {
854901
$msg = str_replace("DateTimeZone::__construct", "timezone_open",
855902
$e->getMessage());
856-
trigger_error($msg, E_WARNING);
903+
trigger_error(HH\FIXME\UNSAFE_CAST<mixed, string>($msg), E_WARNING);
857904
return false;
858905
}
859906
}

hphp/system/php/date/datetimeinterface.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
*/
77
interface DateTimeInterface {
88
public function diff(
9-
/* DateTimeInterface */ $datetime2,
10-
/* bool */ $absolute /*= false */
11-
);
12-
public function format(/* string */ $format);
13-
public function getOffset();
14-
public function getTimestamp()[];
15-
public function getTimezone();
9+
DateTimeInterface $datetime2,
10+
bool $absolute = false,
11+
): mixed;
12+
public function format(string $format): string;
13+
public function getOffset(): mixed;
14+
public function getTimestamp()[]: int;
15+
public function getTimezone(): mixed;
1616
}

0 commit comments

Comments
 (0)