From 6a5ef30e1740b27c3190547cff0a7adf03b03ba0 Mon Sep 17 00:00:00 2001 From: cpenny Date: Thu, 30 Jan 2020 07:31:41 +1300 Subject: [PATCH] Remove typehinting from new classes - Updated some methods so that they still throw exceptions when incorrect types are provided --- src/Elements/ElementBlogOverview.php | 37 ++++++++++++++-------- tests/Elements/ElementBlogOverviewTest.php | 23 ++++++++------ tests/Fake/Page.php | 4 +-- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/Elements/ElementBlogOverview.php b/src/Elements/ElementBlogOverview.php index 9f8fc64..b7e2501 100644 --- a/src/Elements/ElementBlogOverview.php +++ b/src/Elements/ElementBlogOverview.php @@ -3,6 +3,7 @@ namespace Dynamic\Elements\Blog\Elements; use DNADesign\Elemental\Models\BaseElement; +use Exception; use SilverStripe\Blog\Model\Blog; use SilverStripe\Blog\Model\BlogController; use SilverStripe\Blog\Model\BlogPost; @@ -177,7 +178,7 @@ class ElementBlogOverview extends BaseElement * @codeCoverageIgnore * @return string */ - public function getType(): string + public function getType() { return static::config()->get('default_title'); } @@ -186,7 +187,7 @@ public function getType(): string * @codeCoverageIgnore * @return FieldList */ - public function getCMSFields(): FieldList + public function getCMSFields() { $fields = parent::getCMSFields(); @@ -259,7 +260,7 @@ public function getCMSFields(): FieldList return $fields; } - public function populateDefaults(): void + public function populateDefaults() { parent::populateDefaults(); @@ -275,9 +276,10 @@ public function populateDefaults(): void /** * @return PaginatedList|null + * @throws Exception * @throws ValidationException */ - public function getPaginatedList(): ?PaginatedList + public function getPaginatedList() { // Return our cached value, if one exists if ($this->paginatedList !== null) { @@ -313,6 +315,10 @@ public function getPaginatedList(): ?PaginatedList // If you know that you're going to be using this Block on another Page type, then you can implement the // getBlogPostPaginatedList() method there, and we'll use that $paginatedList = $controller->PaginatedList(); + + if ($paginatedList !== null && !$paginatedList instanceof PaginatedList) { + throw new Exception('Expected method PaginatestList() to return a PaginatedList or null'); + } } else { // Since you have specified that this Block *can* be used outside of the Blog, we'll create a PaginatedList // containing all BlogPosts in the DB, and you can then manipulate that List as you wish through the @@ -330,7 +336,7 @@ public function getPaginatedList(): ?PaginatedList * @return DataList * @throws ValidationException */ - public function getBlogPosts(): ?DataList + public function getBlogPosts() { // Return our cached value, if one exists if ($this->blogPosts !== null) { @@ -376,9 +382,10 @@ public function getBlogPosts(): ?DataList /** * @return WidgetArea|null + * @throws Exception * @throws ValidationException */ - public function SideBarView(): ?WidgetArea + public function SideBarView() { // Return our cached value, if one exists if ($this->widgetArea !== null) { @@ -404,9 +411,13 @@ public function SideBarView(): ?WidgetArea // You get one last chance to return a WidgetArea through some other means if ($page->hasMethod('SideBarView')) { - // setWidgetArea() requires a class of WidgetArea|null be provided, so, it'll throw exceptions for us if - // any other type is provided - $this->setWidgetArea($page->SideBarView()); + $widgetArea = $page->SideBarView(); + + if ($widgetArea !== null && !$widgetArea instanceof WidgetArea) { + throw new Exception('Expected SideBarView to return a WidgetArea or null'); + } + + $this->setWidgetArea($widgetArea); return $this->widgetArea; } @@ -442,7 +453,7 @@ public function SideBarView(): ?WidgetArea * @return string|null * @throws ValidationException */ - public function getCacheKey(): ?string + public function getCacheKey() { if ($this->cacheKey !== null) { return $this->cacheKey; @@ -469,7 +480,7 @@ public function getCacheKey(): ?string /** * @param PaginatedList|null $paginatedList */ - protected function setPaginatedList(?PaginatedList $paginatedList): void + protected function setPaginatedList($paginatedList) { // Provided is an opportunity for you to update this PaginatedList $this->invokeWithExtensions('updatePaginatedList', $paginatedList); @@ -481,7 +492,7 @@ protected function setPaginatedList(?PaginatedList $paginatedList): void /** * @param DataList|null $blogPosts */ - protected function setBlogPosts(?DataList $blogPosts): void + protected function setBlogPosts($blogPosts) { // Provided is an opportunity for you to update this DataList $this->invokeWithExtensions('updateBlogPosts', $blogPosts); @@ -493,7 +504,7 @@ protected function setBlogPosts(?DataList $blogPosts): void /** * @param WidgetArea|null $widgetArea */ - protected function setWidgetArea(?WidgetArea $widgetArea): void + protected function setWidgetArea($widgetArea) { $this->invokeWithExtensions('updateWidgetArea', $widgetArea); diff --git a/tests/Elements/ElementBlogOverviewTest.php b/tests/Elements/ElementBlogOverviewTest.php index 126197d..52bf265 100644 --- a/tests/Elements/ElementBlogOverviewTest.php +++ b/tests/Elements/ElementBlogOverviewTest.php @@ -28,7 +28,7 @@ class ElementBlogOverviewTest extends SapphireTest ], ]; - public function testPopulateDefaults(): void + public function testPopulateDefaults() { $block = ElementBlogOverview::create(); $this->assertEmpty($block->Title); @@ -46,7 +46,10 @@ public function testPopulateDefaults(): void $this->assertTrue((bool) $block->ShowWidgets); } - public function testGetBlogPosts(): void + /** + * @throws ValidationException + */ + public function testGetBlogPosts() { /** @var ElementBlogOverview $block */ $block = $this->objFromFixture(ElementBlogOverview::class, 'block1'); @@ -60,7 +63,7 @@ public function testGetBlogPosts(): void * * @throws ValidationException */ - public function testGetBlogPostsDenied(): void + public function testGetBlogPostsDenied() { /** @var ElementBlogOverview $block */ $block = $this->objFromFixture(ElementBlogOverview::class, 'block4'); @@ -74,7 +77,7 @@ public function testGetBlogPostsDenied(): void * * @throws ValidationException */ - public function testGetBlogPostsCustom(): void + public function testGetBlogPostsCustom() { // Update our config to allow this Block type to be used outside of the Blog ElementBlogOverview::config()->update('allow_use_outside_of_blog', true); @@ -92,7 +95,7 @@ public function testGetBlogPostsCustom(): void * * @throws ValidationException */ - public function testGetBlogPostsDefault(): void + public function testGetBlogPostsDefault() { // Update our config to allow this Block type to be used outside of the Blog ElementBlogOverview::config()->update('allow_use_outside_of_blog', true); @@ -110,7 +113,7 @@ public function testGetBlogPostsDefault(): void * * @throws ValidationException */ - public function testSideBarView(): void + public function testSideBarView() { /** @var ElementBlogOverview $block */ $block = $this->objFromFixture(ElementBlogOverview::class, 'block2'); @@ -139,7 +142,7 @@ public function testSideBarView(): void * * @throws ValidationException */ - public function testSideBarViewInheriting(): void + public function testSideBarViewInheriting() { /** @var ElementBlogOverview $block */ $block = $this->objFromFixture(ElementBlogOverview::class, 'block1'); @@ -168,7 +171,7 @@ public function testSideBarViewInheriting(): void * * @throws ValidationException */ - public function testSideBarDenied(): void + public function testSideBarDenied() { /** @var ElementBlogOverview $block */ $block = $this->objFromFixture(ElementBlogOverview::class, 'block3'); @@ -182,7 +185,7 @@ public function testSideBarDenied(): void * * @throws ValidationException */ - public function testSideBarCustom(): void + public function testSideBarCustom() { // Update our config to allow this Block type to be used outside of the Blog ElementBlogOverview::config()->update('allow_use_outside_of_blog', true); @@ -196,7 +199,7 @@ public function testSideBarCustom(): void /** * @throws ValidationException */ - public function testCacheKey(): void + public function testCacheKey() { /** @var ElementBlogOverview $block */ $block = $this->objFromFixture(ElementBlogOverview::class, 'block1'); diff --git a/tests/Fake/Page.php b/tests/Fake/Page.php index 5228ea6..0b45f5c 100644 --- a/tests/Fake/Page.php +++ b/tests/Fake/Page.php @@ -20,7 +20,7 @@ class Page extends BasePage implements TestOnly /** * @return WidgetArea */ - public function SideBarView(): WidgetArea + public function SideBarView() { return WidgetArea::create(); } @@ -28,7 +28,7 @@ public function SideBarView(): WidgetArea /** * @return DataList */ - public function getBlogPosts(): DataList + public function getBlogPosts() { // This is just a workaround way of returning an empty DataList of the correct type return BlogPost::get()->byIDs([0]);