Skip to content

Commit

Permalink
ENH PHP 8.1 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Apr 4, 2022
1 parent cb05e52 commit 539ddee
Show file tree
Hide file tree
Showing 363 changed files with 2,034 additions and 1,984 deletions.
6 changes: 3 additions & 3 deletions src/Control/CLIRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public static function cleanEnvironment(array $variables)
if (isset($variables['_SERVER']['argv'][2])) {
$args = array_slice($variables['_SERVER']['argv'], 2);
foreach ($args as $arg) {
if (strpos($arg, '=') == false) {
if (strpos((string) $arg, '=') == false) {
$variables['_GET']['args'][] = $arg;
} else {
$newItems = [];
parse_str((substr($arg, 0, 2) == '--') ? substr($arg, 2) : $arg, $newItems);
parse_str((substr((string) $arg, 0, 2) == '--') ? substr($arg, 2) : $arg, $newItems);
$variables['_GET'] = array_merge($variables['_GET'], $newItems);
}
}
Expand All @@ -79,7 +79,7 @@ public static function createFromVariables(array $variables, $input, $url = null
{
$request = parent::createFromVariables($variables, $input, $url);
// unset scheme so that SS_BASE_URL can provide `is_https` information if required
$scheme = parse_url(Environment::getEnv('SS_BASE_URL'), PHP_URL_SCHEME);
$scheme = parse_url((string) Environment::getEnv('SS_BASE_URL'), PHP_URL_SCHEME);
if ($scheme) {
$request->setScheme($scheme);
}
Expand Down
40 changes: 20 additions & 20 deletions src/Control/ContentNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ public static function enabled_for($response)

// Disable content negotiation for other content types
if ($contentType
&& substr($contentType, 0, 9) != 'text/html'
&& substr($contentType, 0, 21) != 'application/xhtml+xml'
&& substr((string) $contentType, 0, 9) != 'text/html'
&& substr((string) $contentType, 0, 21) != 'application/xhtml+xml'
) {
return false;
}

if (ContentNegotiator::getEnabled()) {
return true;
} else {
return (substr($response->getBody(), 0, 5) == '<' . '?xml');
return (substr((string) $response->getBody(), 0, 5) == '<' . '?xml');
}
}

Expand Down Expand Up @@ -140,8 +140,8 @@ public static function process(HTTPResponse $response)
$chosenFormat = "xhtml";
} else {
foreach ($mimes as $format => $mime) {
$regExp = '/' . str_replace(['+', '/'], ['\+', '\/'], $mime) . '(;q=(\d+\.\d+))?/i';
if (isset($_SERVER['HTTP_ACCEPT']) && preg_match($regExp, $_SERVER['HTTP_ACCEPT'], $matches)) {
$regExp = '/' . str_replace(['+', '/'], ['\+', '\/'], $mime ?: '') . '(;q=(\d+\.\d+))?/i';
if (isset($_SERVER['HTTP_ACCEPT']) && preg_match((string) $regExp, $_SERVER['HTTP_ACCEPT'], $matches)) {
$preference = isset($matches[2]) ? $matches[2] : 1;
if (!isset($q[$preference])) {
$q[$preference] = $format;
Expand Down Expand Up @@ -189,17 +189,17 @@ public function xhtml(HTTPResponse $response)
$content = preg_replace(
'/<base href="([^"]*)"><!--\[if[[^\]*]\] \/><!\[endif\]-->/',
'<base href="$1" />',
$content
$content ?: ''
);

$content = str_replace('&nbsp;', '&#160;', $content);
$content = str_replace('<br>', '<br />', $content);
$content = str_replace('<hr>', '<hr />', $content);
$content = preg_replace('#(<img[^>]*[^/>])>#i', '\\1/>', $content);
$content = preg_replace('#(<input[^>]*[^/>])>#i', '\\1/>', $content);
$content = preg_replace('#(<param[^>]*[^/>])>#i', '\\1/>', $content);
$content = preg_replace("#(\<option[^>]*[\s]+selected)(?!\s*\=)#si", "$1=\"selected\"$2", $content);
$content = preg_replace("#(\<input[^>]*[\s]+checked)(?!\s*\=)#si", "$1=\"checked\"$2", $content);
$content = str_replace('&nbsp;', '&#160;', $content ?: '');
$content = str_replace('<br>', '<br />', $content ?: '');
$content = str_replace('<hr>', '<hr />', $content ?: '');
$content = preg_replace('#(<img[^>]*[^/>])>#i', '\\1/>', $content ?: '');
$content = preg_replace('#(<input[^>]*[^/>])>#i', '\\1/>', $content ?: '');
$content = preg_replace('#(<param[^>]*[^/>])>#i', '\\1/>', $content ?: '');
$content = preg_replace("#(\<option[^>]*[\s]+selected)(?!\s*\=)#si", "$1=\"selected\"$2", $content ?: '');
$content = preg_replace("#(\<input[^>]*[\s]+checked)(?!\s*\=)#si", "$1=\"checked\"$2", $content ?: '');

$response->setBody($content);
}
Expand All @@ -226,31 +226,31 @@ public function html(HTTPResponse $response)
$response->addHeader("Vary", "Accept");

$content = $response->getBody();
$hasXMLHeader = (substr($content, 0, 5) == '<' . '?xml');
$hasXMLHeader = (substr((string) $content, 0, 5) == '<' . '?xml');

// Fix base tag
$content = preg_replace(
'/<base href="([^"]*)" \/>/',
'<base href="$1"><!--[if lte IE 6]></base><![endif]-->',
$content
$content ?: ''
);

$content = preg_replace("#<\\?xml[^>]+\\?>\n?#", '', $content);
$content = preg_replace("#<\\?xml[^>]+\\?>\n?#", '', $content ?: '');
$content = str_replace(
['/>', 'xml:lang', 'application/xhtml+xml'],
['>', 'lang', 'text/html'],
$content
$content ?: ''
);

// Only replace the doctype in templates with the xml header
if ($hasXMLHeader) {
$content = preg_replace(
'/<!DOCTYPE[^>]+>/',
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
$content
$content ?: ''
);
}
$content = preg_replace('/<html xmlns="[^"]+"/', '<html ', $content);
$content = preg_replace('/<html xmlns="[^"]+"/', '<html ', $content ?: '');

$response->setBody($content);
}
Expand Down
36 changes: 18 additions & 18 deletions src/Control/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,11 @@ public function getViewer($action)
while ($parentClass !== parent::class) {
// _action templates have higher priority
if ($action && $action != 'index') {
$actionTemplates[] = strtok($parentClass, '_') . '_' . $action;
$actionTemplates[] = strtok((string) $parentClass, '_') . '_' . $action;
}
// class templates have lower priority
$classTemplates[] = strtok($parentClass, '_');
$parentClass = get_parent_class($parentClass);
$classTemplates[] = strtok((string) $parentClass, '_');
$parentClass = get_parent_class($parentClass ?: '');
}

// Add controller templates for inheritance chain
Expand Down Expand Up @@ -447,8 +447,8 @@ public function removeAction($fullURL, $action = null)
}
$returnURL = $fullURL;

if (($pos = strpos($fullURL, $action)) !== false) {
$returnURL = substr($fullURL, 0, $pos);
if (($pos = strpos((string) $fullURL, (string) $action)) !== false) {
$returnURL = substr((string) $fullURL, 0, $pos);
}

return $returnURL;
Expand All @@ -471,12 +471,12 @@ protected function definingClassForAction($action)

$class = static::class;
while ($class != 'SilverStripe\\Control\\RequestHandler') {
$templateName = strtok($class, '_') . '_' . $action;
$templateName = strtok((string) $class, '_') . '_' . $action;
if (SSViewer::hasTemplate($templateName)) {
return $class;
}

$class = get_parent_class($class);
$class = get_parent_class($class ?: '');
}

return null;
Expand All @@ -500,8 +500,8 @@ public function hasActionTemplate($action)
$templates = [];

while ($parentClass != __CLASS__) {
$templates[] = strtok($parentClass, '_') . '_' . $action;
$parentClass = get_parent_class($parentClass);
$templates[] = strtok((string) $parentClass, '_') . '_' . $action;
$parentClass = get_parent_class($parentClass ?: '');
}

return SSViewer::hasTemplate($templates);
Expand Down Expand Up @@ -585,7 +585,7 @@ public function can($perm, $member = null)
$member = Security::getCurrentUser();
}
if (is_array($perm)) {
$perm = array_map([$this, 'can'], $perm, array_fill(0, count($perm), $member));
$perm = array_map([$this, 'can'], $perm ?: [], array_fill(0, count($perm ?: []), $member));
return min($perm);
}
if ($this->hasMethod($methodName = 'can' . $perm)) {
Expand Down Expand Up @@ -679,27 +679,27 @@ public static function join_links($arg = null)

foreach ($args as $arg) {
// Find fragment identifier - keep the last one
if (strpos($arg, '#') !== false) {
list($arg, $fragmentIdentifier) = explode('#', $arg, 2);
if (strpos((string) $arg, '#') !== false) {
list($arg, $fragmentIdentifier) = explode('#', (string) $arg, 2);
}
// Find querystrings
if (strpos($arg, '?') !== false) {
list($arg, $suffix) = explode('?', $arg, 2);
parse_str($suffix, $localargs);
if (strpos((string) $arg, '?') !== false) {
list($arg, $suffix) = explode('?', (string) $arg, 2);
parse_str((string) $suffix, $localargs);
$queryargs = array_merge($queryargs, $localargs);
}
if ((is_string($arg) && $arg) || is_numeric($arg)) {
$arg = (string) $arg;
if ($result && substr($result, -1) != '/' && $arg[0] != '/') {
if ($result && substr((string) $result, -1) != '/' && $arg[0] != '/') {
$result .= "/$arg";
} else {
$result .= (substr($result, -1) == '/' && $arg[0] == '/') ? ltrim($arg, '/') : $arg;
$result .= (substr((string) $result, -1) == '/' && $arg[0] == '/') ? ltrim($arg, '/') : $arg;
}
}
}

if ($queryargs) {
$result .= '?' . http_build_query($queryargs);
$result .= '?' . http_build_query($queryargs ?: []);
}

if ($fragmentIdentifier) {
Expand Down
4 changes: 2 additions & 2 deletions src/Control/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function get($name, $includeUnsent = true)
}

//Normalise cookie names by replacing '.' with '_'
$safeName = str_replace('.', '_', $name);
$safeName = str_replace('.', '_', $name ?: '');
if (isset($cookies[$safeName])) {
return $cookies[$safeName];
}
Expand Down Expand Up @@ -170,7 +170,7 @@ protected function outputCookie(
) {
// if headers aren't sent, we can set the cookie
if (!headers_sent($file, $line)) {
return setcookie($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
return setcookie((string) $name, (string) $value, $expiry ?: 0, (string) $path, (string) $domain, (bool) $secure, (bool) $httpOnly);
}

if (Cookie::config()->uninherited('report_errors')) {
Expand Down
Loading

0 comments on commit 539ddee

Please sign in to comment.