Skip to content

Commit

Permalink
Merge pull request #2730 from creative-commoners/pulls/4/php81
Browse files Browse the repository at this point in the history
ENH PHP 8.1 compatibility
  • Loading branch information
GuySartorelli authored Apr 22, 2022
2 parents 5840285 + 1b0b215 commit bb158e4
Show file tree
Hide file tree
Showing 40 changed files with 203 additions and 203 deletions.
2 changes: 1 addition & 1 deletion code/BatchActions/CMSBatchAction_Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public function applicablePages($ids)

// Get pages that exist in stage and remove them from the restore-able set
$stageIDs = Versioned::get_by_stage($this->managedClass, Versioned::DRAFT)->column('ID');
return array_values(array_diff($ids, $stageIDs));
return array_values(array_diff($ids ?? [], $stageIDs));
}
}
66 changes: 33 additions & 33 deletions code/Controllers/CMSMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function init()
{
// set reading lang
if (SiteTree::has_extension('Translatable') && !$this->getRequest()->isAjax()) {
Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages(SiteTree::class)));
Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages(SiteTree::class) ?? []));
}

parent::init();
Expand Down Expand Up @@ -454,7 +454,7 @@ public function LinkWithSearch($link)
];
$link = Controller::join_links(
$link,
array_filter(array_values($params)) ? '?' . http_build_query($params) : null
array_filter(array_values($params ?? [])) ? '?' . http_build_query($params) : null
);
$this->extend('updateLinkWithSearch', $link);
return $link;
Expand All @@ -470,7 +470,7 @@ public function LinkPageAdd($extra = null, $placeholders = null)
}

if ($placeholders) {
$link .= (strpos($link, '?') === false ? "?$placeholders" : "&$placeholders");
$link .= (strpos($link ?? '', '?') === false ? "?$placeholders" : "&$placeholders");
}

return $link;
Expand Down Expand Up @@ -627,7 +627,7 @@ public function getTreeNodeClasses(SiteTree $node)
// Get status flag classes
$flags = $node->getStatusFlags();
if ($flags) {
$statuses = array_keys($flags);
$statuses = array_keys($flags ?? []);
foreach ($statuses as $s) {
$classes .= ' status-' . $s;
}
Expand All @@ -642,7 +642,7 @@ public function getTreeNodeClasses(SiteTree $node)
$classes .= ' ' . $filterClasses;
}

return trim($classes);
return trim($classes ?? '');
}

/**
Expand All @@ -664,8 +664,8 @@ public function getsubtree($request)
);

// Trim off the outer tag
$html = preg_replace('/^[\s\t\r\n]*<ul[^>]*>/', '', $html);
$html = preg_replace('/<\/ul[^>]*>[\s\t\r\n]*$/', '', $html);
$html = preg_replace('/^[\s\t\r\n]*<ul[^>]*>/', '', $html ?? '');
$html = preg_replace('/<\/ul[^>]*>[\s\t\r\n]*$/', '', $html ?? '');

return $html;
}
Expand All @@ -682,7 +682,7 @@ public function getsubtree($request)
public function updatetreenodes($request)
{
$data = [];
$ids = explode(',', $request->getVar('ids'));
$ids = explode(',', $request->getVar('ids') ?? '');
foreach ($ids as $id) {
if ($id === "") {
continue; // $id may be a blank string, which is invalid and should be skipped over
Expand Down Expand Up @@ -824,7 +824,7 @@ public function savetreenode($request)

$this->getResponse()->addHeader(
'X-Status',
rawurlencode(_t(__CLASS__.'.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.'))
rawurlencode(_t(__CLASS__.'.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.') ?? '')
);
}

Expand Down Expand Up @@ -852,7 +852,7 @@ public function savetreenode($request)

$this->getResponse()->addHeader(
'X-Status',
rawurlencode(_t(__CLASS__.'.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.'))
rawurlencode(_t(__CLASS__.'.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.') ?? '')
);
}

Expand Down Expand Up @@ -923,7 +923,7 @@ public function getSearchFieldSchema()

$searchParams = array_combine(array_map(function ($key) {
return 'Search__' . $key;
}, array_keys($searchParams)), $searchParams);
}, array_keys($searchParams ?? [])), $searchParams ?? []);

$schema = [
'formSchemaUrl' => $schemaUrl,
Expand Down Expand Up @@ -1082,7 +1082,7 @@ public function Breadcrumbs($unlinked = false)

// Add all ancestors
$ancestors = $record->getAncestors();
$ancestors = new ArrayList(array_reverse($ancestors->toArray()));
$ancestors = new ArrayList(array_reverse($ancestors->toArray() ?? []));
$ancestors->push($record);
/** @var SiteTree $ancestor */
foreach ($ancestors as $ancestor) {
Expand Down Expand Up @@ -1144,7 +1144,7 @@ public function SiteTreeHints()
// Check if can be created at the root
$needsPerm = $obj->config()->get('need_permission');
if (!$obj->config()->get('can_be_root')
|| (!array_key_exists($class, $canCreate) || !$canCreate[$class])
|| (!array_key_exists($class, $canCreate ?? []) || !$canCreate[$class])
|| ($needsPerm && !$this->can($needsPerm))
) {
$def['Root']['disallowedChildren'][] = $class;
Expand Down Expand Up @@ -1226,7 +1226,7 @@ public function getRecord($id, $versionID = null)
if ($id instanceof $treeClass) {
return $id;
}
if (substr($id, 0, 3) == 'new') {
if (substr($id ?? '', 0, 3) == 'new') {
return $this->getNewItem($id);
}
if (!is_numeric($id)) {
Expand Down Expand Up @@ -1459,18 +1459,18 @@ protected function getArchiveWarningMessage($record)

// Count number of affected change set
$affectedChangeSetCount = 0;
if (count($inChangeSetIDs) > 0) {
if (count($inChangeSetIDs ?? []) > 0) {
$affectedChangeSetCount = ChangeSet::get()
->filter(['ID' => $inChangeSetIDs, 'State' => ChangeSet::STATE_OPEN])
->count();
}

$numCampaigns = ChangeSet::singleton()->i18n_pluralise($affectedChangeSetCount);
$numCampaigns = mb_strtolower($numCampaigns);
$numCampaigns = mb_strtolower($numCampaigns ?? '');

if (count($descendants) > 0 && $affectedChangeSetCount > 0) {
if (count($descendants ?? []) > 0 && $affectedChangeSetCount > 0) {
$archiveWarningMsg = _t('SilverStripe\\CMS\\Controllers\\CMSMain.ArchiveWarningWithChildrenAndCampaigns', 'Warning: This page and all of its child pages will be unpublished and automatically removed from their associated {NumCampaigns} before being sent to the archive.\n\nAre you sure you want to proceed?', [ 'NumCampaigns' => $numCampaigns ]);
} elseif (count($descendants) > 0) {
} elseif (count($descendants ?? []) > 0) {
$archiveWarningMsg = $defaultMessage;
} elseif ($affectedChangeSetCount > 0) {
$archiveWarningMsg = _t('SilverStripe\\CMS\\Controllers\\CMSMain.ArchiveWarningWithCampaigns', 'Warning: This page will be unpublished and automatically removed from their associated {NumCampaigns} before being sent to the archive.\n\nAre you sure you want to proceed?', [ 'NumCampaigns' => $numCampaigns ]);
Expand Down Expand Up @@ -1693,9 +1693,9 @@ public function ListViewForm()
);
$breadcrumbs = $item->Breadcrumbs(20, true, false, true, '/');
// Remove item's tile
$breadcrumbs = preg_replace('/[^\/]+$/', '', trim($breadcrumbs));
$breadcrumbs = preg_replace('/[^\/]+$/', '', trim($breadcrumbs ?? ''));
// Trim spaces around delimiters
$breadcrumbs = preg_replace('/\s?\/\s?/', '/', trim($breadcrumbs));
$breadcrumbs = preg_replace('/\s?\/\s?/', '/', trim($breadcrumbs ?? ''));
return $title . sprintf('<p class="small cms-list__item-breadcrumbs">%s</p>', $breadcrumbs);
}
]);
Expand Down Expand Up @@ -1752,7 +1752,7 @@ public function save($data, $form)

// Existing or new record?
$id = $data['ID'];
if (substr($id, 0, 3) != 'new') {
if (substr($id ?? '', 0, 3) != 'new') {
/** @var SiteTree $record */
$record = DataObject::get_by_id($className, $id);
// Check edit permissions
Expand Down Expand Up @@ -1810,7 +1810,7 @@ public function save($data, $form)
);
}

$this->getResponse()->addHeader('X-Status', rawurlencode($message));
$this->getResponse()->addHeader('X-Status', rawurlencode($message ?? ''));
return $this->getResponseNegotiator()->respond($this->getRequest());
}

Expand All @@ -1825,9 +1825,9 @@ public function save($data, $form)
public function getNewItem($id, $setID = true)
{
$parentClass = $this->config()->get('tree_class');
list(, $className, $parentID) = array_pad(explode('-', $id), 3, null);
list(, $className, $parentID) = array_pad(explode('-', $id ?? ''), 3, null);

if (!is_a($className, $parentClass, true)) {
if (!is_a($className, $parentClass ?? '', true)) {
$response = Security::permissionFailure($this);
if (!$response) {
$response = $this->getResponse();
Expand Down Expand Up @@ -1931,7 +1931,7 @@ public function revert($data, $form)
"Restored '{title}' successfully",
'Param {title} is a title',
['title' => $record->Title]
))
) ?? '')
);

return $this->getResponseNegotiator()->respond($this->getRequest());
Expand Down Expand Up @@ -1967,7 +1967,7 @@ public function delete($data, $form)
__CLASS__ . '.REMOVEDPAGEFROMDRAFT',
"Removed '{title}' from the draft site",
['title' => $record->Title]
))
) ?? '')
);

// Even if the record has been deleted from stage and live, it can be viewed in "archive mode"
Expand Down Expand Up @@ -2003,7 +2003,7 @@ public function archive($data, $form)
__CLASS__ . '.ARCHIVEDPAGE',
"Archived page '{title}'",
['title' => $record->Title]
))
) ?? '')
);

// Even if the record has been deleted from stage and live, it can be viewed in "archive mode"
Expand Down Expand Up @@ -2038,7 +2038,7 @@ public function unpublish($data, $form)
__CLASS__ . '.REMOVEDPAGE',
"Removed '{title}' from the published site",
['title' => $record->Title]
))
) ?? '')
);

return $this->getResponseNegotiator()->respond($this->getRequest());
Expand Down Expand Up @@ -2091,7 +2091,7 @@ public function doRollback($data, $form)
);
}

$this->getResponse()->addHeader('X-Status', rawurlencode($message));
$this->getResponse()->addHeader('X-Status', rawurlencode($message ?? ''));

// Can be used in different contexts: In normal page edit view, in which case the redirect won't have any effect.
// Or in history view, in which case a revert causes the CMS to re-load the edit view.
Expand Down Expand Up @@ -2242,7 +2242,7 @@ public function restore($data, $form)
__CLASS__ . '.RESTORED',
"Restored '{title}' successfully",
['title' => $restoredPage->Title]
))
) ?? '')
);

return $this->getResponseNegotiator()->respond($this->getRequest());
Expand Down Expand Up @@ -2280,7 +2280,7 @@ public function duplicate($request)
__CLASS__ . '.DUPLICATED',
"Duplicated '{title}' successfully",
['title' => $newPage->Title]
))
) ?? '')
);
$url = $newPage->CMSEditLink();
$this->getResponse()->addHeader('X-ControllerURL', $url);
Expand Down Expand Up @@ -2318,7 +2318,7 @@ public function duplicatewithchildren($request)
__CLASS__ . '.DUPLICATEDWITHCHILDREN',
"Duplicated '{title}' and children successfully",
['title' => $newPage->Title]
))
) ?? '')
);
$url = $newPage->CMSEditLink();
$this->getResponse()->addHeader('X-ControllerURL', $url);
Expand Down Expand Up @@ -2370,7 +2370,7 @@ protected function generateHintsCacheKey($memberID)

$this->extend('updateHintsCacheKey', $baseKey);

return md5($baseKey);
return md5($baseKey ?? '');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions code/Controllers/CMSPageAddController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function AddForm()
"PageType",
DBField::create_field(
'HTMLFragment',
sprintf($numericLabelTmpl, 2, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageType', 'Choose page type'))
sprintf($numericLabelTmpl ?? '', 2, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageType', 'Choose page type'))
),
$pageTypes,
'Page'
Expand All @@ -121,7 +121,7 @@ public function AddForm()

$parentModeField->setTitle(DBField::create_field(
'HTMLFragment',
sprintf($numericLabelTmpl, 1, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageParentMode', 'Choose where to create this page'))
sprintf($numericLabelTmpl ?? '', 1, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageParentMode', 'Choose where to create this page'))
));

$parentField->setSearchFunction(function ($sourceObject, $labelField, $search) {
Expand Down
2 changes: 1 addition & 1 deletion code/Controllers/CMSPagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function Breadcrumbs($unlinked = false)
$params['ParentID'] = $page->ID;
$item = new stdClass();
$item->Title = $page->Title;
$item->Link = Controller::join_links($this->Link(), '?' . http_build_query($params));
$item->Link = Controller::join_links($this->Link(), '?' . http_build_query($params ?? []));
$items->push(new ArrayData($item));
}
}
Expand Down
4 changes: 2 additions & 2 deletions code/Controllers/CMSSiteTreeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function get_all_filters()
uasort($filterMap, function ($a, $b) {
return ($a === CMSSiteTreeFilter_Search::title())
? -1
: strcasecmp($a, $b);
: strcasecmp($a ?? '', $b ?? '');
});

return $filterMap;
Expand Down Expand Up @@ -164,7 +164,7 @@ protected function populateIDs()

while (!empty($parents)) {
$q = Versioned::get_including_deleted(SiteTree::class)
->byIDs(array_keys($parents));
->byIDs(array_keys($parents ?? []));
$list = $q->map('ID', 'ParentID');
$parents = [];
foreach ($list as $id => $parentID) {
Expand Down
10 changes: 5 additions & 5 deletions code/Controllers/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected function init()
$getVars = $_GET;
unset($getVars['url']);
if ($getVars) {
$url = "?" . http_build_query($getVars);
$url = "?" . http_build_query($getVars ?? []);
} else {
$url = "";
}
Expand Down Expand Up @@ -494,7 +494,7 @@ public function successfullyinstalled()
// TODO Allow this to work when allow_url_fopen=0
if (isset($_SESSION['StatsID']) && $_SESSION['StatsID']) {
$url = 'http://ss2stat.silverstripe.com/Installation/installed?ID=' . $_SESSION['StatsID'];
@file_get_contents($url);
@file_get_contents($url ?? '');
}

global $project;
Expand Down Expand Up @@ -532,11 +532,11 @@ public function deleteinstallfiles()
$unsuccessful = new ArrayList();
foreach ($installfiles as $installfile) {
$installfilepath = PUBLIC_PATH . '/' . $installfile;
if (file_exists($installfilepath)) {
@unlink($installfilepath);
if (file_exists($installfilepath ?? '')) {
@unlink($installfilepath ?? '');
}

if (file_exists($installfilepath)) {
if (file_exists($installfilepath ?? '')) {
$unsuccessful->push(new ArrayData(['File' => $installfile]));
}
}
Expand Down
6 changes: 3 additions & 3 deletions code/Controllers/ModelAsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static function controller_for(SiteTree $sitetree, $action = null)
{
$controller = $sitetree->getControllerName();

if ($action && class_exists($controller . '_' . ucfirst($action))) {
$controller = $controller . '_' . ucfirst($action);
if ($action && class_exists($controller . '_' . ucfirst($action ?? ''))) {
$controller = $controller . '_' . ucfirst($action ?? '');
}

return Injector::inst()->create($controller, $sitetree);
Expand Down Expand Up @@ -131,7 +131,7 @@ public function getNestedController()
// url encode unless it's multibyte (already pre-encoded in the database)
$filter = URLSegmentFilter::create();
if (!$filter->getAllowMultibyte()) {
$URLSegment = rawurlencode($URLSegment);
$URLSegment = rawurlencode($URLSegment ?? '');
}

// Select child page
Expand Down
Loading

0 comments on commit bb158e4

Please sign in to comment.