diff --git a/src/Admin/CommentAdmin.php b/src/Admin/CommentAdmin.php index 6ab90f0..56b6e2b 100644 --- a/src/Admin/CommentAdmin.php +++ b/src/Admin/CommentAdmin.php @@ -100,7 +100,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider _t( __CLASS__.'.NewComments', 'New ({count})', - ['count' => count($newComments)] + ['count' => count($newComments ?? [])] ), $newGrid ), @@ -109,7 +109,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider _t( __CLASS__.'.ApprovedComments', 'Approved ({count})', - ['count' => count($approvedComments)] + ['count' => count($approvedComments ?? [])] ), $approvedGrid ), @@ -118,7 +118,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider _t( __CLASS__.'.SpamComments', 'Spam ({count})', - ['count' => count($spamComments)] + ['count' => count($spamComments ?? [])] ), $spamGrid ) diff --git a/src/Admin/CommentsGridFieldAction.php b/src/Admin/CommentsGridFieldAction.php index dc0d867..9e143dd 100644 --- a/src/Admin/CommentsGridFieldAction.php +++ b/src/Admin/CommentsGridFieldAction.php @@ -19,7 +19,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function augmentColumns($gridField, &$columns) { - if (!in_array('Actions', $columns)) { + if (!in_array('Actions', $columns ?? [])) { $columns[] = 'Actions'; } } diff --git a/src/Admin/CommentsGridFieldApproveAction.php b/src/Admin/CommentsGridFieldApproveAction.php index 1979109..5c175e1 100644 --- a/src/Admin/CommentsGridFieldApproveAction.php +++ b/src/Admin/CommentsGridFieldApproveAction.php @@ -20,7 +20,7 @@ class CommentsGridFieldApproveAction implements */ public function augmentColumns($gridField, &$columns) { - if (!in_array('Actions', $columns)) { + if (!in_array('Actions', $columns ?? [])) { $columns[] = 'Actions'; } } diff --git a/src/Admin/CommentsGridFieldSpamAction.php b/src/Admin/CommentsGridFieldSpamAction.php index cc8761c..e02c94a 100644 --- a/src/Admin/CommentsGridFieldSpamAction.php +++ b/src/Admin/CommentsGridFieldSpamAction.php @@ -20,7 +20,7 @@ class CommentsGridFieldSpamAction implements */ public function augmentColumns($gridField, &$columns) { - if (!in_array('Actions', $columns)) { + if (!in_array('Actions', $columns ?? [])) { $columns[] = 'Actions'; } } diff --git a/src/Controllers/CommentingController.php b/src/Controllers/CommentingController.php index 50c5fcd..a0023a5 100644 --- a/src/Controllers/CommentingController.php +++ b/src/Controllers/CommentingController.php @@ -115,7 +115,7 @@ class CommentingController extends Controller */ public function encodeClassName($input) { - return str_replace('\\', '-', $input); + return str_replace('\\', '-', $input ?? ''); } /** @@ -126,7 +126,7 @@ class CommentingController extends Controller */ public function decodeClassName($input) { - return str_replace('-', '\\', $input); + return str_replace('-', '\\', $input ?? ''); } /** diff --git a/src/Extensions/CommentsExtension.php b/src/Extensions/CommentsExtension.php index 4e75f00..2d18c26 100644 --- a/src/Extensions/CommentsExtension.php +++ b/src/Extensions/CommentsExtension.php @@ -583,9 +583,9 @@ class CommentsExtension extends DataExtension CommentsGridFieldConfig::create() ); - $newCount = '(' . count($newComments) . ')'; - $approvedCount = '(' . count($approvedComments) . ')'; - $spamCount = '(' . count($spamComments) . ')'; + $newCount = '(' . count($newComments ?? []) . ')'; + $approvedCount = '(' . count($approvedComments ?? []) . ')'; + $spamCount = '(' . count($spamComments ?? []) . ')'; if ($fields->hasTabSet()) { $tabs = TabSet::create( diff --git a/src/Forms/CommentForm.php b/src/Forms/CommentForm.php index c8639f5..5c046a1 100644 --- a/src/Forms/CommentForm.php +++ b/src/Forms/CommentForm.php @@ -145,8 +145,8 @@ class CommentForm extends Form } // load user data from previous form request back into form. - if (array_key_exists('UserData', $data)) { - $formData = json_decode($data['UserData'], true); + if (array_key_exists('UserData', $data ?? [])) { + $formData = json_decode($data['UserData'] ?? '', true); $this->loadDataFrom([ 'Name' => isset($formData['Name']) ? $formData['Name'] : '', @@ -156,7 +156,7 @@ class CommentForm extends Form } // allow previous value to fill if comment - if (array_key_exists('Comment', $data)) { + if (array_key_exists('Comment', $data ?? [])) { $prevComment = $data['Comment']; if ($prevComment && $prevComment != '') { diff --git a/src/Model/Comment.php b/src/Model/Comment.php index 8c47ac6..090f17b 100755 --- a/src/Model/Comment.php +++ b/src/Model/Comment.php @@ -608,7 +608,7 @@ class Comment extends DataObject */ public function getTitle() { - $title = sprintf(_t(__CLASS__ . '.COMMENTBY', 'Comment by %s', 'Name'), $this->getAuthorName()); + $title = sprintf(_t(__CLASS__ . '.COMMENTBY', 'Comment by %s', 'Name') ?? '', $this->getAuthorName()); if ($parent = $this->Parent()) { if ($parent->Title) { @@ -726,7 +726,7 @@ class Comment extends DataObject } // This injector cannot be set unless the 'p' element is allowed - if (in_array('p', $allowedElements)) { + if (in_array('p', $allowedElements ?? [])) { $config->set('AutoFormat.AutoParagraph', true); } @@ -747,7 +747,7 @@ class Comment extends DataObject $use_gravatar = $this->getOption('use_gravatar'); if ($use_gravatar) { - $gravatar = 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->Email))); + $gravatar = 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->Email ?? ''))); $gravatarsize = $this->getOption('gravatar_size'); $gravatardefault = $this->getOption('gravatar_default'); $gravatarrating = $this->getOption('gravatar_rating'); diff --git a/src/Model/Comment/SecurityToken.php b/src/Model/Comment/SecurityToken.php index e92fc76..e511ab4 100644 --- a/src/Model/Comment/SecurityToken.php +++ b/src/Model/Comment/SecurityToken.php @@ -37,7 +37,7 @@ class SecurityToken */ protected function getToken($salt) { - return hash_pbkdf2('sha256', $this->secret, $salt, 1000, 30); + return hash_pbkdf2('sha256', $this->secret ?? '', $salt ?? '', 1000, 30); } /** @@ -74,8 +74,8 @@ class SecurityToken $url, sprintf( '?t=%s&s=%s', - urlencode($token), - urlencode($salt) + urlencode($token ?? ''), + urlencode($salt ?? '') ) ); } @@ -113,7 +113,7 @@ class SecurityToken $generator = new RandomGenerator(); $result = $generator->randomToken('sha256'); if ($length !== null) { - return substr($result, 0, $length); + return substr($result ?? '', 0, $length); } return $result; } diff --git a/tests/CommentingControllerTest.php b/tests/CommentingControllerTest.php index ec48e37..e1e8f0d 100644 --- a/tests/CommentingControllerTest.php +++ b/tests/CommentingControllerTest.php @@ -179,21 +179,29 @@ class CommentingControllerTest extends FunctionalTest // comments sitewide $response = $this->get('comments/rss'); $comment = "10 approved, non spam comments on page 1"; - $this->assertEquals(10, substr_count($response->getBody(), ""), $comment); + $this->assertEquals(10, substr_count($response->getBody() ?? '', ""), $comment); $response = $this->get('comments/rss?start=10'); - $this->assertEquals(4, substr_count($response->getBody(), ""), "3 approved, non spam comments on page 2"); + $this->assertEquals( + 4, + substr_count($response->getBody() ?? '', ""), + "3 approved, non spam comments on page 2" + ); // all comments on a type $response = $this->get('comments/rss/SilverStripe-Comments-Tests-Stubs-CommentableItem'); - $this->assertEquals(10, substr_count($response->getBody(), "")); + $this->assertEquals(10, substr_count($response->getBody() ?? '', "")); $response = $this->get('comments/rss/SilverStripe-Comments-Tests-Stubs-CommentableItem?start=10'); - $this->assertEquals(4, substr_count($response->getBody(), ""), "3 approved, non spam comments on page 2"); + $this->assertEquals( + 4, + substr_count($response->getBody() ?? '', ""), + "3 approved, non spam comments on page 2" + ); // specific page $response = $this->get('comments/rss/SilverStripe-Comments-Tests-Stubs-CommentableItem/'.$item->ID); - $this->assertEquals(1, substr_count($response->getBody(), "")); + $this->assertEquals(1, substr_count($response->getBody() ?? '', "")); $this->assertStringContainsString('FA', $response->getBody()); // test accessing comments on a type that doesn't exist diff --git a/tests/CommentsExtensionTest.php b/tests/CommentsExtensionTest.php index dbee3cd..c07afd8 100644 --- a/tests/CommentsExtensionTest.php +++ b/tests/CommentsExtensionTest.php @@ -343,7 +343,7 @@ class CommentsExtensionTest extends FunctionalTest $results[0]->Comment ); - $this->assertEquals(4, sizeof($results)); + $this->assertEquals(4, sizeof($results ?? [])); } public function testUpdateModerationFields()