ENH PHP 8.1 compatibility

This commit is contained in:
Steve Boyd 2022-04-13 10:29:17 +12:00
parent cb13f4ec12
commit b7554a5fc9
12 changed files with 39 additions and 39 deletions

View File

@ -109,9 +109,9 @@ class CacheHeadersCheck implements EnvironmentCheck
// Filter good messages // Filter good messages
$goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO];
$good = array_filter( $good = array_filter(
$this->result->getMessages(), $this->result->getMessages() ?? [],
function ($val, $key) use ($goodTypes) { function ($val, $key) use ($goodTypes) {
if (in_array($val['messageType'], $goodTypes)) { if (in_array($val['messageType'], $goodTypes ?? [])) {
return true; return true;
} }
return false; return false;
@ -119,15 +119,15 @@ class CacheHeadersCheck implements EnvironmentCheck
ARRAY_FILTER_USE_BOTH ARRAY_FILTER_USE_BOTH
); );
if (!empty($good)) { if (!empty($good)) {
$ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; $ret .= "GOOD: " . implode('; ', array_column($good ?? [], 'message')) . " ";
} }
// Filter bad messages // Filter bad messages
$badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING];
$bad = array_filter( $bad = array_filter(
$this->result->getMessages(), $this->result->getMessages() ?? [],
function ($val, $key) use ($badTypes) { function ($val, $key) use ($badTypes) {
if (in_array($val['messageType'], $badTypes)) { if (in_array($val['messageType'], $badTypes ?? [])) {
return true; return true;
} }
return false; return false;
@ -135,7 +135,7 @@ class CacheHeadersCheck implements EnvironmentCheck
ARRAY_FILTER_USE_BOTH ARRAY_FILTER_USE_BOTH
); );
if (!empty($bad)) { if (!empty($bad)) {
$ret .= "BAD: " . implode('; ', array_column($bad, 'message')); $ret .= "BAD: " . implode('; ', array_column($bad ?? [], 'message'));
} }
return $ret; return $ret;
} }
@ -173,32 +173,32 @@ class CacheHeadersCheck implements EnvironmentCheck
private function checkCacheControl(ResponseInterface $response) private function checkCacheControl(ResponseInterface $response)
{ {
$cacheControl = $response->getHeaderLine('Cache-Control'); $cacheControl = $response->getHeaderLine('Cache-Control');
$vals = array_map('trim', explode(',', $cacheControl)); $vals = array_map('trim', explode(',', $cacheControl ?? ''));
$fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url);
// All entries from must contain should be present // All entries from must contain should be present
if ($this->mustInclude == array_intersect($this->mustInclude, $vals)) { if ($this->mustInclude == array_intersect($this->mustInclude ?? [], $vals)) {
$matched = implode(",", $this->mustInclude); $matched = implode(",", $this->mustInclude);
$this->result->addMessage( $this->result->addMessage(
"$fullURL includes all settings: {$matched}", "$fullURL includes all settings: {$matched}",
ValidationResult::TYPE_GOOD ValidationResult::TYPE_GOOD
); );
} else { } else {
$missing = implode(",", array_diff($this->mustInclude, $vals)); $missing = implode(",", array_diff($this->mustInclude ?? [], $vals));
$this->result->addError( $this->result->addError(
"$fullURL is excluding some settings: {$missing}" "$fullURL is excluding some settings: {$missing}"
); );
} }
// All entries from must exclude should not be present // All entries from must exclude should not be present
if (empty(array_intersect($this->mustExclude, $vals))) { if (empty(array_intersect($this->mustExclude ?? [], $vals))) {
$missing = implode(",", $this->mustExclude); $missing = implode(",", $this->mustExclude);
$this->result->addMessage( $this->result->addMessage(
"$fullURL excludes all settings: {$missing}", "$fullURL excludes all settings: {$missing}",
ValidationResult::TYPE_GOOD ValidationResult::TYPE_GOOD
); );
} else { } else {
$matched = implode(",", array_intersect($this->mustExclude, $vals)); $matched = implode(",", array_intersect($this->mustExclude ?? [], $vals));
$this->result->addError( $this->result->addError(
"$fullURL is including some settings: {$matched}" "$fullURL is including some settings: {$matched}"
); );

View File

@ -37,7 +37,7 @@ class ExternalURLCheck implements EnvironmentCheck
public function __construct($urls, $timeout = 15) public function __construct($urls, $timeout = 15)
{ {
if ($urls) { if ($urls) {
$this->urls = explode(' ', $urls); $this->urls = explode(' ', $urls ?? '');
} }
$this->timeout = $timeout; $this->timeout = $timeout;
} }
@ -55,7 +55,7 @@ class ExternalURLCheck implements EnvironmentCheck
foreach ($urls as $url) { foreach ($urls as $url) {
$ch = curl_init(); $ch = curl_init();
$chs[] = $ch; $chs[] = $ch;
curl_setopt_array($ch, $this->getCurlOpts($url)); curl_setopt_array($ch, $this->getCurlOpts($url) ?? []);
} }
// Parallel execution for faster performance // Parallel execution for faster performance
$mh = curl_multi_init(); $mh = curl_multi_init();

View File

@ -96,7 +96,7 @@ class FileAccessibilityAndValidationCheck implements EnvironmentCheck
$files = $this->getFiles(); $files = $this->getFiles();
if ($files) { if ($files) {
$fileTypeValidateFunc = $this->fileTypeValidateFunc; $fileTypeValidateFunc = $this->fileTypeValidateFunc;
if (method_exists($this, $fileTypeValidateFunc)) { if (method_exists($this, $fileTypeValidateFunc ?? '')) {
$invalidFiles = []; $invalidFiles = [];
$validFiles = []; $validFiles = [];
@ -109,7 +109,7 @@ class FileAccessibilityAndValidationCheck implements EnvironmentCheck
} }
// If at least one file was valid, count as passed // If at least one file was valid, count as passed
if ($this->checkType == self::CHECK_SINGLE && count($invalidFiles) < count($files)) { if ($this->checkType == self::CHECK_SINGLE && count($invalidFiles ?? []) < count($files ?? [])) {
$validFileList = PHP_EOL; $validFileList = PHP_EOL;
foreach ($validFiles as $vf) { foreach ($validFiles as $vf) {
$validFileList .= $vf . PHP_EOL; $validFileList .= $vf . PHP_EOL;
@ -130,7 +130,7 @@ class FileAccessibilityAndValidationCheck implements EnvironmentCheck
]; ];
} }
} else { } else {
if (count($invalidFiles) == 0) { if (count($invalidFiles ?? []) == 0) {
$checkReturn = [EnvironmentCheck::OK, 'All files valideted']; $checkReturn = [EnvironmentCheck::OK, 'All files valideted'];
} else { } else {
$invalidFileList = PHP_EOL; $invalidFileList = PHP_EOL;
@ -180,7 +180,7 @@ class FileAccessibilityAndValidationCheck implements EnvironmentCheck
*/ */
private function jsonValidate($file) private function jsonValidate($file)
{ {
$json = json_decode(file_get_contents($file)); $json = json_decode(file_get_contents($file ?? '') ?? '');
if (!$json) { if (!$json) {
return false; return false;
} }
@ -204,6 +204,6 @@ class FileAccessibilityAndValidationCheck implements EnvironmentCheck
*/ */
protected function getFiles() protected function getFiles()
{ {
return glob($this->path); return glob($this->path ?? '');
} }
} }

View File

@ -96,7 +96,7 @@ class FileAgeCheck implements EnvironmentCheck
*/ */
public function check() public function check()
{ {
$cutoffTime = strtotime($this->relativeAge, DBDatetime::now()->Format('U')); $cutoffTime = strtotime($this->relativeAge ?? '', DBDatetime::now()->Format('U'));
$files = $this->getFiles(); $files = $this->getFiles();
$invalidFiles = []; $invalidFiles = [];
$validFiles = []; $validFiles = [];
@ -127,10 +127,10 @@ class FileAgeCheck implements EnvironmentCheck
} }
// If at least one file was valid, count as passed // If at least one file was valid, count as passed
if ($this->checkType == self::CHECK_SINGLE && count($invalidFiles) < count($files)) { if ($this->checkType == self::CHECK_SINGLE && count($invalidFiles ?? []) < count($files ?? [])) {
return [EnvironmentCheck::OK, '']; return [EnvironmentCheck::OK, ''];
} }
if (count($invalidFiles) == 0) { if (count($invalidFiles ?? []) == 0) {
return [EnvironmentCheck::OK, '']; return [EnvironmentCheck::OK, ''];
} }
return [ return [
@ -146,6 +146,6 @@ class FileAgeCheck implements EnvironmentCheck
*/ */
protected function getFiles() protected function getFiles()
{ {
return glob($this->path); return glob($this->path ?? '');
} }
} }

View File

@ -34,22 +34,22 @@ class FileWriteableCheck implements EnvironmentCheck
if ($this->path[0] == '/') { if ($this->path[0] == '/') {
$filename = $this->path; $filename = $this->path;
} else { } else {
$filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path); $filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path ?? '');
} }
if (file_exists($filename)) { if (file_exists($filename ?? '')) {
$isWriteable = is_writeable($filename); $isWriteable = is_writeable($filename ?? '');
} else { } else {
$isWriteable = is_writeable(dirname($filename)); $isWriteable = is_writeable(dirname($filename ?? ''));
} }
if (!$isWriteable) { if (!$isWriteable) {
if (function_exists('posix_getgroups')) { if (function_exists('posix_getgroups')) {
$userID = posix_geteuid(); $userID = posix_geteuid();
$user = posix_getpwuid($userID); $user = posix_getpwuid($userID ?? 0);
$currentOwnerID = fileowner(file_exists($filename) ? $filename : dirname($filename)); $currentOwnerID = fileowner(file_exists($filename ?? '') ? $filename : dirname($filename ?? ''));
$currentOwner = posix_getpwuid($currentOwnerID); $currentOwner = posix_getpwuid($currentOwnerID ?? 0);
$message = "User '$user[name]' needs to be able to write to this file:\n$filename\n\nThe file is " $message = "User '$user[name]' needs to be able to write to this file:\n$filename\n\nThe file is "
. "currently owned by '$currentOwner[name]'. "; . "currently owned by '$currentOwner[name]'. ";
@ -60,8 +60,8 @@ class FileWriteableCheck implements EnvironmentCheck
$groups = posix_getgroups(); $groups = posix_getgroups();
$groupList = []; $groupList = [];
foreach ($groups as $group) { foreach ($groups as $group) {
$groupInfo = posix_getgrgid($group); $groupInfo = posix_getgrgid($group ?? 0);
if (in_array($currentOwner['name'], $groupInfo['members'])) { if (in_array($currentOwner['name'], $groupInfo['members'] ?? [])) {
$groupList[] = $groupInfo['name']; $groupList[] = $groupInfo['name'];
} }
} }

View File

@ -31,7 +31,7 @@ class HasClassCheck implements EnvironmentCheck
*/ */
public function check() public function check()
{ {
if (class_exists($this->className)) { if (class_exists($this->className ?? '')) {
return [EnvironmentCheck::OK, 'Class ' . $this->className.' exists']; return [EnvironmentCheck::OK, 'Class ' . $this->className.' exists'];
} }
return [EnvironmentCheck::ERROR, 'Class ' . $this->className.' doesn\'t exist']; return [EnvironmentCheck::ERROR, 'Class ' . $this->className.' doesn\'t exist'];

View File

@ -31,7 +31,7 @@ class HasFunctionCheck implements EnvironmentCheck
*/ */
public function check() public function check()
{ {
if (function_exists($this->functionName)) { if (function_exists($this->functionName ?? '')) {
return [EnvironmentCheck::OK, $this->functionName . '() exists']; return [EnvironmentCheck::OK, $this->functionName . '() exists'];
} }
return [EnvironmentCheck::ERROR, $this->functionName . '() doesn\'t exist']; return [EnvironmentCheck::ERROR, $this->functionName . '() doesn\'t exist'];

View File

@ -57,7 +57,7 @@ class SMTPConnectCheck implements EnvironmentCheck
*/ */
public function check() public function check()
{ {
$f = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); $f = @fsockopen($this->host ?? '', $this->port ?? 0, $errno, $errstr, $this->timeout);
if (!$f) { if (!$f) {
return [ return [
EnvironmentCheck::ERROR, EnvironmentCheck::ERROR,
@ -67,7 +67,7 @@ class SMTPConnectCheck implements EnvironmentCheck
fwrite($f, "HELO its_me\r\n"); fwrite($f, "HELO its_me\r\n");
$response = fread($f, 26); $response = fread($f, 26);
if (substr($response, 0, 3) != '220') { if (substr($response ?? '', 0, 3) != '220') {
return [ return [
EnvironmentCheck::ERROR, EnvironmentCheck::ERROR,
sprintf('Invalid mail server response: %s', $response) sprintf('Invalid mail server response: %s', $response)

View File

@ -62,7 +62,7 @@ class SessionCheck implements EnvironmentCheck
$cookies = $response->getHeader('Set-Cookie'); $cookies = $response->getHeader('Set-Cookie');
foreach ($cookies as $cookie) { foreach ($cookies as $cookie) {
if (strpos($cookie, 'SESSID') !== false) { if (strpos($cookie ?? '', 'SESSID') !== false) {
$result = $cookie; $result = $cookie;
} }
} }

View File

@ -43,7 +43,7 @@ class SolrIndexCheck implements EnvironmentCheck
if (!empty($brokenCores)) { if (!empty($brokenCores)) {
return [ return [
EnvironmentCheck::ERROR, EnvironmentCheck::ERROR,
'The following indexes are unavailable: ' . implode($brokenCores, ', ') 'The following indexes are unavailable: ' . implode($brokenCores ?? '', ', ')
]; ];
} }

View File

@ -49,7 +49,7 @@ class URLCheck implements EnvironmentCheck
EnvironmentCheck::ERROR, EnvironmentCheck::ERROR,
sprintf('Error retrieving "%s" (Code: %d)', $this->url, $response->getStatusCode()) sprintf('Error retrieving "%s" (Code: %d)', $this->url, $response->getStatusCode())
]; ];
} elseif ($this->testString && (strpos($response->getBody(), $this->testString) === false)) { } elseif ($this->testString && (strpos($response->getBody() ?? '', $this->testString ?? '') === false)) {
return [ return [
EnvironmentCheck::WARNING, EnvironmentCheck::WARNING,
sprintf('Success retrieving "%s", but string "%s" not found', $this->url, $this->testString) sprintf('Success retrieving "%s", but string "%s" not found', $this->url, $this->testString)

View File

@ -206,7 +206,7 @@ class EnvironmentChecker extends RequestHandler
// output the result as JSON if requested // output the result as JSON if requested
if ($this->getRequest()->getExtension() == 'json' if ($this->getRequest()->getExtension() == 'json'
|| strpos($this->getRequest()->getHeader('Accept'), 'application/json') !== false || strpos($this->getRequest()->getHeader('Accept') ?? '', 'application/json') !== false
) { ) {
$response->setBody($result->toJSON()); $response->setBody($result->toJSON());
$response->addHeader('Content-Type', 'application/json'); $response->addHeader('Content-Type', 'application/json');