From ec60328a0c584b1ca3494b163a1ae354bdcd40ee Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 24 Sep 2018 18:16:03 +0200 Subject: [PATCH] Use short brackets, injection where possible and type hint injector created objects --- src/Admin/CommentAdmin.php | 29 ++++---- src/Admin/CommentsGridField.php | 1 - src/Admin/CommentsGridFieldAction.php | 20 +++--- src/Admin/CommentsGridFieldConfig.php | 7 +- src/Controllers/CommentingController.php | 92 +++++++++++++----------- 5 files changed, 78 insertions(+), 71 deletions(-) diff --git a/src/Admin/CommentAdmin.php b/src/Admin/CommentAdmin.php index 257dd1e..7cbd599 100644 --- a/src/Admin/CommentAdmin.php +++ b/src/Admin/CommentAdmin.php @@ -3,22 +3,19 @@ namespace SilverStripe\Comments\Admin; use SilverStripe\Admin\LeftAndMain; -use SilverStripe\Comments\Admin\CommentsGridField; use SilverStripe\Comments\Model\Comment; -use SilverStripe\Forms\Tab; -use SilverStripe\Forms\TabSet; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; -use SilverStripe\Security\PermissionProvider; +use SilverStripe\Forms\Tab; +use SilverStripe\Forms\TabSet; use SilverStripe\Security\Security; -use SilverStripe\View\SSViewer; /** * Comment administration system within the CMS * * @package comments */ -class CommentAdmin extends LeftAndMain implements PermissionProvider +class CommentAdmin extends LeftAndMain { private static $url_segment = 'comments'; @@ -28,7 +25,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider private static $menu_icon_class = 'font-icon-comment'; - private static $allowed_actions = array( + private static $allowed_actions = [ 'approvedmarked', 'deleteall', 'deletemarked', @@ -37,18 +34,18 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider 'spammarked', 'EditForm', 'unmoderated' - ); + ]; private static $required_permission_codes = 'CMS_ACCESS_CommentAdmin'; public function providePermissions() { - return array( - "CMS_ACCESS_CommentAdmin" => array( + return [ + 'CMS_ACCESS_CommentAdmin' => [ 'name' => _t(__CLASS__ . '.ADMIN_PERMISSION', "Access to 'Comments' section"), 'category' => _t('SilverStripe\\Security\\Permission.CMS_ACCESS_CATEGORY', 'CMS Access') - ) - ); + ], + ]; } /** @@ -69,7 +66,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider $newComments = Comment::get()->filter('Moderated', 0); - $newGrid = new CommentsGridField( + $newGrid = CommentsGridField::create( 'NewComments', '', $newComments, @@ -78,7 +75,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider $approvedComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 0); - $approvedGrid = new CommentsGridField( + $approvedGrid = CommentsGridField::create( 'ApprovedComments', '', $approvedComments, @@ -87,7 +84,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider $spamComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 1); - $spamGrid = new CommentsGridField( + $spamGrid = CommentsGridField::create( 'SpamComments', '', $spamComments, @@ -140,7 +137,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); if ($form->Fields()->hasTabset()) { - $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); + $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses()); } diff --git a/src/Admin/CommentsGridField.php b/src/Admin/CommentsGridField.php index 732c036..cde1eed 100644 --- a/src/Admin/CommentsGridField.php +++ b/src/Admin/CommentsGridField.php @@ -2,7 +2,6 @@ namespace SilverStripe\Comments\Admin; -use SilverStripe\Forms\FormField; use SilverStripe\Forms\GridField\GridField; use SilverStripe\View\HTML; diff --git a/src/Admin/CommentsGridFieldAction.php b/src/Admin/CommentsGridFieldAction.php index d602bf4..478472d 100644 --- a/src/Admin/CommentsGridFieldAction.php +++ b/src/Admin/CommentsGridFieldAction.php @@ -26,7 +26,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function getColumnAttributes($gridField, $record, $columnName) { - return array('class' => 'col-buttons'); + return ['class' => 'col-buttons']; } /** @@ -34,8 +34,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function getColumnMetadata($gridField, $columnName) { - if ($columnName == 'Actions') { - return array('title' => ''); + if ($columnName === 'Actions') { + return ['title' => '']; } } @@ -44,7 +44,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function getColumnsHandled($gridField) { - return array('Actions'); + return ['Actions']; } /** @@ -64,7 +64,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act 'CustomAction' . $record->ID . 'Spam', _t(__CLASS__ . '.SPAM', 'Spam'), 'spam', - array('RecordID' => $record->ID) + ['RecordID' => $record->ID] ) ->addExtraClass('btn btn-secondary grid-field__icon-action') ->Field(); @@ -76,7 +76,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act 'CustomAction' . $record->ID . 'Approve', _t(__CLASS__ . '.APPROVE', 'Approve'), 'approve', - array('RecordID' => $record->ID) + ['RecordID' => $record->ID] ) ->addExtraClass('btn btn-secondary grid-field__icon-action') ->Field(); @@ -90,7 +90,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function getActions($gridField) { - return array('spam', 'approve'); + return ['spam', 'approve']; } /** @@ -98,7 +98,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { - if ($actionName == 'spam') { + if ($actionName === 'spam') { + /** @var Comment $comment */ $comment = Comment::get()->byID($arguments['RecordID']); $comment->markSpam(); @@ -109,7 +110,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act ); } - if ($actionName == 'approve') { + if ($actionName === 'approve') { + /** @var Comment $comment */ $comment = Comment::get()->byID($arguments['RecordID']); $comment->markApproved(); diff --git a/src/Admin/CommentsGridFieldConfig.php b/src/Admin/CommentsGridFieldConfig.php index 4225fec..fb0c300 100644 --- a/src/Admin/CommentsGridFieldConfig.php +++ b/src/Admin/CommentsGridFieldConfig.php @@ -20,8 +20,9 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor $this->addComponent(new CommentsGridFieldAction()); // Format column + /** @var GridFieldDataColumns $columns */ $columns = $this->getComponentByType(GridFieldDataColumns::class); - $columns->setFieldFormatting(array( + $columns->setFieldFormatting([ 'ParentTitle' => function ($value, &$item) { return sprintf( '%s', @@ -29,10 +30,10 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor $item->obj('ParentTitle')->forTemplate() ); } - )); + ]); // Add bulk option - $manager = new BulkManager(null, false); + $manager = BulkManager::create(null, false); $spamAction = SpamHandler::create()->setLabel(_t(__CLASS__ . '.SPAM', 'Spam')); $approveAction = ApproveHandler::create()->setLabel(_t(__CLASS__ . '.APPROVE', 'Approve')); diff --git a/src/Controllers/CommentingController.php b/src/Controllers/CommentingController.php index f69afc7..50c5fcd 100644 --- a/src/Controllers/CommentingController.php +++ b/src/Controllers/CommentingController.php @@ -4,20 +4,21 @@ namespace SilverStripe\Comments\Controllers; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Comments\Extensions\CommentsExtension; +use SilverStripe\Comments\Forms\CommentForm; use SilverStripe\Comments\Model\Comment; -use SilverStripe\Control\Director; use SilverStripe\Control\Controller; -use SilverStripe\Control\Email\Email; +use SilverStripe\Control\Director; use SilverStripe\Control\HTTP; use SilverStripe\Control\HTTPRequest; +use SilverStripe\Control\HTTPResponse; +use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Control\RSS\RSSFeed; -use SilverStripe\Control\Session; -use SilverStripe\ORM\DataObject; -use SilverStripe\ORM\PaginatedList; -use SilverStripe\Security\Member; -use SilverStripe\Security\Security; use SilverStripe\Core\Injector\Injector; -use SilverStripe\Comments\Forms\CommentForm; +use SilverStripe\Forms\Form; +use SilverStripe\ORM\DataObject; +use SilverStripe\ORM\FieldType\DBHTMLText; +use SilverStripe\ORM\PaginatedList; +use SilverStripe\Security\Security; /** * @package comments @@ -27,7 +28,7 @@ class CommentingController extends Controller /** * {@inheritDoc} */ - private static $allowed_actions = array( + private static $allowed_actions = [ 'delete', 'spam', 'ham', @@ -36,15 +37,15 @@ class CommentingController extends Controller 'CommentsForm', 'reply', 'doPostComment', - 'doPreviewComment' - ); + 'doPreviewComment', + ]; /** * {@inheritDoc} */ - private static $url_handlers = array( + private static $url_handlers = [ 'reply/$ParentCommentID//$ID/$OtherID' => 'reply', - ); + ]; /** * Fields required for this form @@ -52,11 +53,11 @@ class CommentingController extends Controller * @var array * @config */ - private static $required_fields = array( + private static $required_fields = [ 'Name', 'Email', - 'Comment' - ); + 'Comment', + ]; /** * Parent class this commenting form is for @@ -70,21 +71,21 @@ class CommentingController extends Controller * * @var DataObject */ - private $ownerRecord = null; + private $ownerRecord; /** * Parent controller record * * @var Controller */ - private $ownerController = null; + private $ownerController; /** * Backup url to return to * * @var string */ - protected $fallbackReturnURL = null; + protected $fallbackReturnURL; /** * Set the parent class name to use @@ -178,6 +179,7 @@ class CommentingController extends Controller { // If possible use the current record if ($record = $this->getOwnerRecord()) { + /** @var DataObject|CommentsExtension $record */ return $record->getCommentsOption($key); } @@ -198,6 +200,7 @@ class CommentingController extends Controller public function getOptions() { if ($record = $this->getOwnerRecord()) { + /** @var DataObject|CommentsExtension $record */ return $record->getCommentsOptions(); } @@ -226,7 +229,7 @@ class CommentingController extends Controller /** * Outputs the RSS feed of comments * - * @return HTMLText + * @return DBHTMLText */ public function rss() { @@ -252,10 +255,10 @@ class CommentingController extends Controller $class = SiteTree::class; } - $comments = Comment::get()->filter(array( + $comments = Comment::get()->filter([ 'Moderated' => 1, 'IsSpam' => 0, - )); + ]); // Check if class filter if ($class) { @@ -275,10 +278,10 @@ class CommentingController extends Controller } $title = _t(__CLASS__ . '.RSSTITLE', "Comments RSS Feed"); - $comments = new PaginatedList($comments, $request); + $comments = PaginatedList::create($comments, $request); $comments->setPageLength($this->getOption('comments_per_page')); - return new RSSFeed( + return RSSFeed::create( $comments, $link, $title, @@ -375,6 +378,9 @@ class CommentingController extends Controller * Redirect back to referer if available, ensuring that only site URLs * are allowed to avoid phishing. If it's an AJAX request render the * comment in it's new state + * + * @param Comment $comment + * @return DBHTMLText|HTTPResponse|false */ private function renderChangedCommentState($comment) { @@ -383,21 +389,21 @@ class CommentingController extends Controller // Render comment using AJAX if ($this->request->isAjax()) { return $comment->renderWith('Includes/CommentsInterface_singlecomment'); - } else { - // Redirect to either the comment or start of the page - if (empty($referer)) { - return $this->redirectBack(); - } else { - // Redirect to the comment, but check for phishing - $url = $referer . '#comment-' . $comment->ID; - // absolute redirection URLs not located on this site may cause phishing - if (Director::is_site_url($url)) { - return $this->redirect($url); - } else { - return false; - } - } } + + // Redirect to either the comment or start of the page + if (empty($referer)) { + return $this->redirectBack(); + } + + // Redirect to the comment, but check for phishing + $url = $referer . '#comment-' . $comment->ID; + // absolute redirection URLs not located on this site may cause phishing + if (Director::is_site_url($url)) { + return $this->redirect($url); + } + + return false; } /** @@ -411,6 +417,7 @@ class CommentingController extends Controller $id = isset($this->urlParams['ID']) ? $this->urlParams['ID'] : false; if ($id) { + /** @var Comment $comment */ $comment = Comment::get()->byId($id); if ($comment) { $this->fallbackReturnURL = $comment->Link(); @@ -436,9 +443,9 @@ class CommentingController extends Controller $form->addExtraClass('reply-form'); // Load parent into reply form - $form->loadDataFrom(array( + $form->loadDataFrom([ 'ParentCommentID' => $comment->ID - )); + ]); // Customise action $form->setFormAction($this->Link('reply', $comment->ID)); @@ -461,6 +468,7 @@ class CommentingController extends Controller { // Extract parent comment from reply and build this way if ($parentID = $request->param('ParentCommentID')) { + /** @var Comment $comment */ $comment = DataObject::get_by_id(Comment::class, $parentID, true); if ($comment) { return $this->ReplyForm($comment); @@ -518,8 +526,8 @@ class CommentingController extends Controller // absolute redirection URLs not located on this site may cause phishing if (Director::is_site_url($url)) { return $this->redirect($url); - } else { - return false; } + + return false; } }