Timezone: make PHPStan 1.0 happy

This commit is contained in:
Dylan K. Taylor 2021-11-01 15:24:16 +00:00
parent 27ae959e89
commit d5f13d8be2
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -31,6 +31,7 @@ use function file_get_contents;
use function implode;
use function ini_get;
use function ini_set;
use function is_array;
use function is_string;
use function json_decode;
use function parse_ini_file;
@ -58,50 +59,49 @@ abstract class Timezone{
*/
public static function init() : array{
$messages = [];
do{
$timezone = self::get();
if($timezone !== ""){
/*
* This is here so that people don't come to us complaining and fill up the issue tracker when they put
* an incorrect timezone abbreviation in php.ini apparently.
*/
if(strpos($timezone, "/") === false){
$default_timezone = timezone_name_from_abbr($timezone);
if($default_timezone !== false){
ini_set("date.timezone", $default_timezone);
date_default_timezone_set($default_timezone);
break;
}else{
//Bad php.ini value, try another method to detect timezone
$messages[] = "Timezone \"$timezone\" could not be parsed as a valid timezone from php.ini, falling back to auto-detection";
}
}else{
date_default_timezone_set($timezone);
break;
$timezone = self::get();
if($timezone !== ""){
/*
* This is here so that people don't come to us complaining and fill up the issue tracker when they put
* an incorrect timezone abbreviation in php.ini apparently.
*/
if(strpos($timezone, "/") === false){
$default_timezone = timezone_name_from_abbr($timezone);
if($default_timezone !== false){
ini_set("date.timezone", $default_timezone);
date_default_timezone_set($default_timezone);
return $messages;
}
//Bad php.ini value, try another method to detect timezone
$messages[] = "Timezone \"$timezone\" could not be parsed as a valid timezone from php.ini, falling back to auto-detection";
}else{
date_default_timezone_set($timezone);
return $messages;
}
}
if(($timezone = self::detectSystemTimezone()) and date_default_timezone_set($timezone)){
//Success! Timezone has already been set and validated in the if statement.
//This here is just for redundancy just in case some program wants to read timezone data from the ini.
ini_set("date.timezone", $timezone);
break;
}
if(($timezone = self::detectSystemTimezone()) !== false and date_default_timezone_set($timezone)){
//Success! Timezone has already been set and validated in the if statement.
//This here is just for redundancy just in case some program wants to read timezone data from the ini.
ini_set("date.timezone", $timezone);
return $messages;
}
if(($response = Internet::getURL("http://ip-api.com/json")) !== false //If system timezone detection fails or timezone is an invalid value.
and $ip_geolocation_data = json_decode($response, true)
and $ip_geolocation_data['status'] !== 'fail'
and date_default_timezone_set($ip_geolocation_data['timezone'])
){
//Again, for redundancy.
ini_set("date.timezone", $ip_geolocation_data['timezone']);
break;
}
if(($response = Internet::getURL("http://ip-api.com/json")) !== false //If system timezone detection fails or timezone is an invalid value.
and is_array($ip_geolocation_data = json_decode($response, true))
and isset($ip_geolocation_data['status'])
and $ip_geolocation_data['status'] !== 'fail'
and is_string($ip_geolocation_data['timezone'])
and date_default_timezone_set($ip_geolocation_data['timezone'])
){
//Again, for redundancy.
ini_set("date.timezone", $ip_geolocation_data['timezone']);
return $messages;
}
ini_set("date.timezone", "UTC");
date_default_timezone_set("UTC");
$messages[] = "Timezone could not be automatically determined or was set to an invalid value. An incorrect timezone will result in incorrect timestamps on console logs. It has been set to \"UTC\" by default. You can change it on the php.ini file.";
}while(false);
ini_set("date.timezone", "UTC");
date_default_timezone_set("UTC");
$messages[] = "Timezone could not be automatically determined or was set to an invalid value. An incorrect timezone will result in incorrect timestamps on console logs. It has been set to \"UTC\" by default. You can change it on the php.ini file.";
return $messages;
}