From f1678781a20b0ae5b6b5ed4821b5dd6b8c38d5dc Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 11 Apr 2022 17:22:22 +1200 Subject: [PATCH] ENH Various fixes for PHP 8.1 compatibility --- src/Control/Email/Email.php | 6 +++++- src/ORM/FieldType/DBText.php | 2 +- src/ORM/Queries/SQLConditionalExpression.php | 2 +- tests/php/Forms/DateFieldTest.php | 8 ++++---- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Control/Email/Email.php b/src/Control/Email/Email.php index f71db1b7aa3..504937a3d8c 100644 --- a/src/Control/Email/Email.php +++ b/src/Control/Email/Email.php @@ -19,6 +19,7 @@ use SilverStripe\View\ThemeResourceLoader; use SilverStripe\View\ViewableData; use Swift_Message; +use Swift_Mime_SimpleMessage; use Swift_MimePart; /** @@ -260,7 +261,10 @@ public function __construct( public function getSwiftMessage() { if (!$this->swiftMessage) { - $this->setSwiftMessage(new Swift_Message(null, null, 'text/html', 'utf-8')); + $message = new Swift_Message(null, null, 'text/html', 'utf-8'); + // Set priority to fix PHP 8.1 SimpleMessage::getPriority() sscanf() null parameter + $message->setPriority(Swift_Mime_SimpleMessage::PRIORITY_NORMAL); + $this->setSwiftMessage($message); } return $this->swiftMessage; diff --git a/src/ORM/FieldType/DBText.php b/src/ORM/FieldType/DBText.php index a0df9b7936e..2ded132db36 100644 --- a/src/ORM/FieldType/DBText.php +++ b/src/ORM/FieldType/DBText.php @@ -213,7 +213,7 @@ public function ContextSummary( $position = empty($keywords) ? 0 : (int) mb_stripos($text, $keywords); // We want to search string to be in the middle of our block to give it some context - $position = max(0, $position - ($characters / 2)); + $position = floor(max(0, $position - ($characters / 2))); if ($position > 0) { // We don't want to start mid-word diff --git a/src/ORM/Queries/SQLConditionalExpression.php b/src/ORM/Queries/SQLConditionalExpression.php index b4f4bb2c0df..b470df3c408 100644 --- a/src/ORM/Queries/SQLConditionalExpression.php +++ b/src/ORM/Queries/SQLConditionalExpression.php @@ -384,7 +384,7 @@ protected function mergesort(&$array, $cmpFunction = 'strcmp') return; } // Split the array in half - $halfway = count($array) / 2; + $halfway = floor(count($array) / 2); $array1 = array_slice($array, 0, $halfway); $array2 = array_slice($array, $halfway); // Recurse to sort the two halves diff --git a/tests/php/Forms/DateFieldTest.php b/tests/php/Forms/DateFieldTest.php index 55bbb15abd5..9b05d1edd47 100644 --- a/tests/php/Forms/DateFieldTest.php +++ b/tests/php/Forms/DateFieldTest.php @@ -46,12 +46,12 @@ public function testValidateMinDateStrtotime() { $f = new DateField('Date'); $f->setMinDate('-7 days'); - $f->setValue(strftime('%Y-%m-%d', strtotime('-8 days', DBDatetime::now()->getTimestamp()))); + $f->setValue(date('Y-m-d', strtotime('-8 days', DBDatetime::now()->getTimestamp()))); $this->assertFalse($f->validate(new RequiredFields()), 'Date below min date, with strtotime'); $f = new DateField('Date'); $f->setMinDate('-7 days'); - $f->setValue(strftime('%Y-%m-%d', strtotime('-7 days', DBDatetime::now()->getTimestamp()))); + $f->setValue(date('Y-m-d', strtotime('-7 days', DBDatetime::now()->getTimestamp()))); $this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date, with strtotime'); } @@ -59,12 +59,12 @@ public function testValidateMaxDateStrtotime() { $f = new DateField('Date'); $f->setMaxDate('7 days'); - $f->setValue(strftime('%Y-%m-%d', strtotime('8 days', DBDatetime::now()->getTimestamp()))); + $f->setValue(date('Y-m-d', strtotime('8 days', DBDatetime::now()->getTimestamp()))); $this->assertFalse($f->validate(new RequiredFields()), 'Date above max date, with strtotime'); $f = new DateField('Date'); $f->setMaxDate('7 days'); - $f->setValue(strftime('%Y-%m-%d', strtotime('7 days', DBDatetime::now()->getTimestamp()))); + $f->setValue(date('Y-m-d', strtotime('7 days', DBDatetime::now()->getTimestamp()))); $this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date, with strtotime'); }