From 8f3778f77f314ebb01156a4dc9965c7c4516ffa1 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 14 Apr 2022 15:16:37 +1200 Subject: [PATCH] ENH PHP 8.1 compatibility --- src/Admin/GridFieldMergeAction.php | 2 +- src/Forms/GridField/GridFieldBlogPostState.php | 4 ++-- src/Model/Blog.php | 2 +- src/Model/BlogController.php | 16 ++++++++-------- src/Model/BlogFilter.php | 2 +- src/Model/BlogMemberExtension.php | 2 +- src/Model/BlogPost.php | 6 +++--- src/Widgets/BlogArchiveWidget.php | 4 ++-- tests/behat/src/FeatureContext.php | 2 +- tests/php/BlogTest.php | 2 +- 10 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Admin/GridFieldMergeAction.php b/src/Admin/GridFieldMergeAction.php index d1666a6db..22622ee3a 100644 --- a/src/Admin/GridFieldMergeAction.php +++ b/src/Admin/GridFieldMergeAction.php @@ -61,7 +61,7 @@ public function __construct($records, $parentType, $parentMethod, $childMethod) */ public function augmentColumns($gridField, &$columns) { - if (!in_array('MergeAction', $columns)) { + if (!in_array('MergeAction', $columns ?? [])) { $columns[] = 'MergeAction'; } diff --git a/src/Forms/GridField/GridFieldBlogPostState.php b/src/Forms/GridField/GridFieldBlogPostState.php index 38c0a90a2..9478bd530 100644 --- a/src/Forms/GridField/GridFieldBlogPostState.php +++ b/src/Forms/GridField/GridFieldBlogPostState.php @@ -47,7 +47,7 @@ public function getColumnContent($gridField, $record, $columnName) */ $publishDate = $record->dbObject('PublishDate'); - if (strtotime($record->PublishDate) > time()) { + if (strtotime($record->PublishDate ?? '') > time()) { return ' ' . _t( __CLASS__ . '.Timer', 'Publish at {date}', @@ -83,7 +83,7 @@ public function getColumnAttributes($gridField, $record, $columnName) if (!$published) { $class = 'gridfield-icon draft'; - } elseif (strtotime($record->PublishDate) > time()) { + } elseif (strtotime($record->PublishDate ?? '') > time()) { $class = 'gridfield-icon timer'; } else { $class = 'gridfield-icon published'; diff --git a/src/Model/Blog.php b/src/Model/Blog.php index ab8bdb102..c848c5cd4 100644 --- a/src/Model/Blog.php +++ b/src/Model/Blog.php @@ -263,7 +263,7 @@ protected function isMemberOf($member, $relation) } if ($relation instanceof UnsavedRelationList) { - return in_array($member->ID, $relation->getIDList()); + return in_array($member->ID, $relation->getIDList() ?? []); } return $relation->byID($member->ID) !== null; diff --git a/src/Model/BlogController.php b/src/Model/BlogController.php index 5bcfd1351..a30ee6510 100644 --- a/src/Model/BlogController.php +++ b/src/Model/BlogController.php @@ -112,7 +112,7 @@ public function getCurrentProfile() // url encode unless it's multibyte (already pre-encoded in the database) // see https://github.com/silverstripe/silverstripe-cms/pull/2384 if (!$filter->getAllowMultibyte()) { - $urlSegment = rawurlencode($urlSegment); + $urlSegment = rawurlencode($urlSegment ?? ''); } return Member::get() @@ -201,9 +201,9 @@ public function getArchiveMonth() { $month = $this->request->param('Month'); - if (preg_match('/^[0-9]{1,2}$/', $month)) { + if (preg_match('/^[0-9]{1,2}$/', $month ?? '')) { if ($month > 0 && $month < 13) { - if (checkdate($month, 01, $this->getArchiveYear())) { + if (checkdate($month ?? 0, 01, $this->getArchiveYear() ?? 0)) { return (int) $month; } } @@ -221,8 +221,8 @@ public function getArchiveDay() { $day = $this->request->param('Day'); - if (preg_match('/^[0-9]{1,2}$/', $day)) { - if (checkdate($this->getArchiveMonth(), $day, $this->getArchiveYear())) { + if (preg_match('/^[0-9]{1,2}$/', $day ?? '')) { + if (checkdate($this->getArchiveMonth() ?? 0, $day ?? 0, $this->getArchiveYear() ?? 0)) { return (int) $day; } } @@ -271,7 +271,7 @@ public function getCurrentTag() // url encode unless it's multibyte (already pre-encoded in the database) // see https://github.com/silverstripe/silverstripe-cms/pull/2384 if (!$filter->getAllowMultibyte()) { - $tag = rawurlencode($tag); + $tag = rawurlencode($tag ?? ''); } return $dataRecord->Tags() @@ -321,7 +321,7 @@ public function getCurrentCategory() // url encode unless it's multibyte (already pre-encoded in the database) // see https://github.com/silverstripe/silverstripe-cms/pull/2384 if (!$filter->getAllowMultibyte()) { - $category = rawurlencode($category); + $category = rawurlencode($category ?? ''); } return $dataRecord->Categories() @@ -582,6 +582,6 @@ protected function rssFeed($blogPosts, $link) protected function isRSS() { $rss = $this->request->param('Rss'); - return (is_string($rss) && strcasecmp($rss, 'rss') == 0); + return (is_string($rss) && strcasecmp($rss ?? '', 'rss') == 0); } } diff --git a/src/Model/BlogFilter.php b/src/Model/BlogFilter.php index ff771a576..89d348eb1 100644 --- a/src/Model/BlogFilter.php +++ b/src/Model/BlogFilter.php @@ -54,7 +54,7 @@ public function stageChildren($showAll = false) */ protected function subclassForBlog() { - return in_array(get_class($this->owner), ClassInfo::subclassesFor(Blog::class)); + return in_array(get_class($this->owner), ClassInfo::subclassesFor(Blog::class) ?? []); } /** diff --git a/src/Model/BlogMemberExtension.php b/src/Model/BlogMemberExtension.php index b13c53281..9ea032c21 100644 --- a/src/Model/BlogMemberExtension.php +++ b/src/Model/BlogMemberExtension.php @@ -56,7 +56,7 @@ public function onBeforeWrite() $this->owner->URLSegment = $this->generateURLSegment(); while (!$this->validURLSegment()) { - $this->owner->URLSegment = preg_replace('/-[0-9]+$/', '', $this->owner->URLSegment) . '-' . $count; + $this->owner->URLSegment = preg_replace('/-[0-9]+$/', '', $this->owner->URLSegment ?? '') . '-' . $count; $count++; } diff --git a/src/Model/BlogPost.php b/src/Model/BlogPost.php index 6c092d10a..fd9416002 100644 --- a/src/Model/BlogPost.php +++ b/src/Model/BlogPost.php @@ -239,7 +239,7 @@ public function isAuthor($member = null) $list = $this->Authors(); if ($list instanceof UnsavedRelationList) { - return in_array($member->ID, $list->getIDList()); + return in_array($member->ID, $list->getIDList() ?? []); } return $list->byID($member->ID) !== null; @@ -740,7 +740,7 @@ protected function getStaticCredits() { $items = ArrayList::create(); - $authors = array_filter(preg_split('/\s*,\s*/', $this->AuthorNames)); + $authors = array_filter(preg_split('/\s*,\s*/', $this->AuthorNames ?? '') ?? []); foreach ($authors as $author) { $item = ArrayData::create([ @@ -809,7 +809,7 @@ public function MinutesToRead($wpm = null) throw new \InvalidArgumentException(sprintf("Expecting integer but got %s instead", gettype($wpm))); } - $wordCount = str_word_count(strip_tags($this->Content)); + $wordCount = str_word_count(strip_tags($this->Content ?? '')); if ($wordCount < $wpm) { return 0; diff --git a/src/Widgets/BlogArchiveWidget.php b/src/Widgets/BlogArchiveWidget.php index 135faaf6b..4ee27bbb7 100644 --- a/src/Widgets/BlogArchiveWidget.php +++ b/src/Widgets/BlogArchiveWidget.php @@ -85,7 +85,7 @@ public function getCMSFields() $type = $archiveType->enumValues(); foreach ($type as $k => $v) { - $type[$k] = _t(__CLASS__ .'.' . ucfirst(strtolower($v)), $v); + $type[$k] = _t(__CLASS__ .'.' . ucfirst(strtolower($v ?? '')), $v); } /** @@ -139,7 +139,7 @@ public function getArchive() $title = $year; } else { $date = DBDate::create(); - $date->setValue(strtotime($post['PublishDate'])); + $date->setValue(strtotime($post['PublishDate'] ?? '')); $year = $date->Format('y'); $month = $date->Format('MM'); diff --git a/tests/behat/src/FeatureContext.php b/tests/behat/src/FeatureContext.php index e656ff238..f41979841 100644 --- a/tests/behat/src/FeatureContext.php +++ b/tests/behat/src/FeatureContext.php @@ -58,7 +58,7 @@ public function stepIFillInTheHtmlFieldWith($widgetTitle, $fieldTitle, $value) $this->getSession()->evaluateScript(sprintf( "jQuery('#%s').entwine('ss').getEditor().setContent('%s')", $field->getAttribute('id'), - addcslashes($value, "'") + addcslashes($value ?? '', "'") )); $this->getSession()->evaluateScript(sprintf( "jQuery('#%s').entwine('ss').getEditor().save()", diff --git a/tests/php/BlogTest.php b/tests/php/BlogTest.php index a02f65e12..32ed789c7 100755 --- a/tests/php/BlogTest.php +++ b/tests/php/BlogTest.php @@ -371,6 +371,6 @@ protected function assertIDsEquals($left, $right) } asort($left); asort($right); - $this->assertEquals(array_values($left), array_values($right)); + $this->assertEquals(array_values($left ?? []), array_values($right ?? [])); } }