Skip to content

Commit

Permalink
Remove typehinting from new classes
Browse files Browse the repository at this point in the history
- Updated some methods so that they still throw exceptions when incorrect
  types are provided
  • Loading branch information
cpenny committed Jan 29, 2020
1 parent a0df09a commit 6a5ef30
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
37 changes: 24 additions & 13 deletions src/Elements/ElementBlogOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,7 +178,7 @@ class ElementBlogOverview extends BaseElement
* @codeCoverageIgnore
* @return string
*/
public function getType(): string
public function getType()
{
return static::config()->get('default_title');
}
Expand All @@ -186,7 +187,7 @@ public function getType(): string
* @codeCoverageIgnore
* @return FieldList
*/
public function getCMSFields(): FieldList
public function getCMSFields()
{
$fields = parent::getCMSFields();

Expand Down Expand Up @@ -259,7 +260,7 @@ public function getCMSFields(): FieldList
return $fields;
}

public function populateDefaults(): void
public function populateDefaults()
{
parent::populateDefaults();

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand Down
23 changes: 13 additions & 10 deletions tests/Elements/ElementBlogOverviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ElementBlogOverviewTest extends SapphireTest
],
];

public function testPopulateDefaults(): void
public function testPopulateDefaults()
{
$block = ElementBlogOverview::create();
$this->assertEmpty($block->Title);
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions tests/Fake/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class Page extends BasePage implements TestOnly
/**
* @return WidgetArea
*/
public function SideBarView(): WidgetArea
public function SideBarView()
{
return WidgetArea::create();
}

/**
* @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]);
Expand Down

0 comments on commit 6a5ef30

Please sign in to comment.