Skip to content

Commit

Permalink
ENH Add generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jan 17, 2024
1 parent a5d2b3b commit 2e20bd7
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 53 deletions.
14 changes: 0 additions & 14 deletions code/Controllers/CMSMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,10 +1038,6 @@ public function getBreadcrumbsBackLink()
->Link;
}

/**
* @param bool $unlinked
* @return ArrayList
*/
public function Breadcrumbs($unlinked = false)
{
$items = new ArrayList();
Expand Down Expand Up @@ -1293,7 +1289,6 @@ public function getEditForm($id = null, $fields = null)
if (!$id) {
$id = $this->currentPageID();
}
/** @var SiteTree $record */
$record = $this->getRecord($id);

// Check parent form can be generated
Expand Down Expand Up @@ -1639,7 +1634,6 @@ public function ListViewForm()
}
$gridField = GridField::create('Page', 'Pages', $list, $gridFieldConfig);
$gridField->setAttribute('cms-loading-ignore-url-params', true);
/** @var GridFieldDataColumns $columns */
$columns = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);

// Don't allow navigating into children nodes on filtered lists
Expand All @@ -1648,7 +1642,6 @@ public function ListViewForm()
'i18n_singular_name' => _t('SilverStripe\\CMS\\Model\\SiteTree.PAGETYPE', 'Page Type'),
'LastEdited' => _t('SilverStripe\\CMS\\Model\\SiteTree.LASTUPDATED', 'Last Updated'),
];
/** @var GridFieldSortableHeader $sortableHeader */
$sortableHeader = $gridField->getConfig()->getComponentByType(GridFieldSortableHeader::class);
$sortableHeader->setFieldSorting(['getTreeTitle' => 'Title']);
$gridField->getState()->ParentID = $parentID;
Expand Down Expand Up @@ -1891,7 +1884,6 @@ public function revert(array $data, Form $form): HTTPResponse
throw new HTTPResponse_Exception("SiteTree #$id not found", 400);
}

/** @var SiteTree $record */
$table = DataObject::singleton(SiteTree::class)->baseTable();
$liveTable = DataObject::singleton(SiteTree::class)->stageTable($table, Versioned::LIVE);
$record = Versioned::get_one_by_stage(SiteTree::class, Versioned::LIVE, [
Expand Down Expand Up @@ -1964,7 +1956,6 @@ public function delete(array $data, Form $form): HTTPResponse
public function archive(array $data, Form $form): HTTPResponse
{
$id = $data['ID'];
/** @var SiteTree $record */
$record = SiteTree::get()->byID($id);
if (!$record || !$record->exists()) {
throw new HTTPResponse_Exception("Bad record ID #$id", 404);
Expand Down Expand Up @@ -2141,7 +2132,6 @@ public function restore(array $data, Form $form): HTTPResponse
}

$id = (int)$data['ID'];
/** @var SiteTree $restoredPage */
$restoredPage = Versioned::get_latest_version(SiteTree::class, $id);
if (!$restoredPage) {
return new HTTPResponse("SiteTree #$id not found", 400);
Expand Down Expand Up @@ -2169,7 +2159,6 @@ public function duplicate(HTTPRequest $request): HTTPResponse
}

if (($id = $this->urlParams['ID']) && is_numeric($id)) {
/** @var SiteTree $page */
$page = SiteTree::get()->byID($id);
if ($page && !$page->canCreate(null, ['Parent' => $page->Parent()])) {
return Security::permissionFailure($this);
Expand All @@ -2178,7 +2167,6 @@ public function duplicate(HTTPRequest $request): HTTPResponse
throw new HTTPResponse_Exception("Bad record ID #$id", 404);
}

/** @var SiteTree $newPage */
$newPage = $page->duplicate();

// ParentID can be hard-set in the URL. This is useful for pages with multiple parents
Expand Down Expand Up @@ -2213,7 +2201,6 @@ public function duplicatewithchildren(HTTPRequest $request): HTTPResponse
}
Environment::increaseTimeLimitTo();
if (($id = $this->urlParams['ID']) && is_numeric($id)) {
/** @var SiteTree $page */
$page = SiteTree::get()->byID($id);
if ($page && !$page->canCreate(null, ['Parent' => $page->Parent()])) {
return Security::permissionFailure($this);
Expand All @@ -2222,7 +2209,6 @@ public function duplicatewithchildren(HTTPRequest $request): HTTPResponse
throw new HTTPResponse_Exception("Bad record ID #$id", 404);
}

/** @var SiteTree $newPage */
$newPage = $page->duplicateWithChildren();

$this->getResponse()->addHeader(
Expand Down
15 changes: 9 additions & 6 deletions code/Controllers/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@
* Subclasses of ContentController are generally instantiated by ModelAsController; this will create
* a controller based on the URLSegment action variable, by looking in the SiteTree table.
*
* @template T of SiteTree
*/
class ContentController extends Controller
{
/**
* @var SiteTree
* @var T
*/
protected $dataRecord;

Expand All @@ -71,7 +72,7 @@ class ContentController extends Controller
* The ContentController will take the URLSegment parameter from the URL and use that to look
* up a SiteTree record.
*
* @param SiteTree $dataRecord
* @param T|null $dataRecord
*/
public function __construct($dataRecord = null)
{
Expand Down Expand Up @@ -110,7 +111,7 @@ public function Link($action = null)
* Return the children of a given page. The parent reference can either be a page link or an ID.
*
* @param string|int $parentRef
* @return SS_List
* @return SS_List<SiteTree>
*/
public function ChildrenOf($parentRef)
{
Expand Down Expand Up @@ -188,7 +189,6 @@ protected function init()
*/
public function handleRequest(HTTPRequest $request): HTTPResponse
{
/** @var SiteTree $child */
$child = null;
$action = $request->param('Action');

Expand Down Expand Up @@ -243,6 +243,7 @@ public function project()

/**
* Returns the associated database record
* @return T
*/
public function data()
{
Expand All @@ -254,7 +255,7 @@ public function data()
/**
* Returns a fixed navigation menu of the given level.
* @param int $level Menu level to return.
* @return ArrayList
* @return ArrayList<SiteTree>
*/
public function getMenu($level = 1)
{
Expand Down Expand Up @@ -284,7 +285,6 @@ public function getMenu($level = 1)
// We might need to create a show in menu permission
if (isset($result)) {
foreach ($result as $page) {
/** @var SiteTree $page */
if ($page->canView()) {
$visible[] = $page;
}
Expand All @@ -294,6 +294,9 @@ public function getMenu($level = 1)
return new ArrayList($visible);
}

/**
* @return ArrayList<SiteTree>
*/
public function Menu($level)
{
return $this->getMenu($level);
Expand Down
4 changes: 4 additions & 0 deletions code/Controllers/LeftAndMainBatchActionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace SilverStripe\CMS\Controllers;

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\Core\Extension;

/**
* @extends Extension<LeftAndMain>
*/
class LeftAndMainBatchActionsExtension extends Extension
{
public function updateBatchActionsForm(&$form)
Expand Down
4 changes: 3 additions & 1 deletion code/Controllers/LeftAndMainPageIconsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;
use ReflectionException;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
Expand All @@ -16,10 +17,11 @@

/**
* Extension to include custom page icons
*
* @extends Extension<LeftAndMain>
*/
class LeftAndMainPageIconsExtension extends Extension implements Flushable
{

/**
* @throws InvalidArgumentException
* @throws ReflectionException
Expand Down
1 change: 0 additions & 1 deletion code/Controllers/ModelAsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public function getNestedController(): ContentController
if (SiteTree::config()->get('nested_urls')) {
$conditions[] = [sprintf('"%s"."ParentID"', $tableName) => 0];
}
/** @var SiteTree $sitetree */
$sitetree = DataObject::get_one(SiteTree::class, $conditions);

if (!$sitetree) {
Expand Down
5 changes: 3 additions & 2 deletions code/Controllers/OldPageRedirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Extension;

/**
* @extends Extension<ContentController|ModelAsController>
*/
class OldPageRedirector extends Extension
{

/**
* On every URL that generates a 404, we'll capture it here and see if we can
* find an old URL that it should be redirecting to.
Expand Down Expand Up @@ -75,7 +77,6 @@ public static function find_old_page($params, $parent = null, $redirect = false)
'ParentID' => is_numeric($parent) ? $parent : $parent->ID,
]);
}
/** @var SiteTree $page */
$page = $pages->first();
if (!$page) {
// If we haven't found a candidate, lets resort to finding an old page with this URL segment
Expand Down
14 changes: 2 additions & 12 deletions code/Forms/InternalLinkModalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

/**
* Decorates ModalController with insert internal link
* @see ModalController
*
* @extends Extension<ModalController>
*/
class InternalLinkModalExtension extends Extension
{
Expand All @@ -22,17 +23,6 @@ class InternalLinkModalExtension extends Extension
'editorAnchorLink',
];

/**
* @return ModalController
*/
public function getOwner()
{
/** @var ModalController $owner */
$owner = $this->owner;
return $owner;
}


/**
* Form for inserting internal link pages
*
Expand Down
1 change: 0 additions & 1 deletion code/Model/RedirectorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function redirectionLink()
}

// Check internal redirect
/** @var SiteTree $linkTo */
$linkTo = $this->LinkToID ? SiteTree::get()->byID($this->LinkToID) : null;

if (empty($linkTo)) {
Expand Down
3 changes: 2 additions & 1 deletion code/Model/RedirectorPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* Controller for the {@link RedirectorPage}.
*
* @extends PageController<RedirectorPage>
*/
class RedirectorPageController extends PageController
{
Expand All @@ -26,7 +28,6 @@ class RedirectorPageController extends PageController
*/
public function index(HTTPRequest $request)
{
/** @var RedirectorPage $page */
$page = $this->data();

// Redirect if we can
Expand Down
16 changes: 4 additions & 12 deletions code/Model/SiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,6 @@ public static function link_shortcode_handler($arguments, $content = null, $pars
return null; // There were no suitable matches at all.
}

/** @var SiteTree $page */
$link = Convert::raw2att($page->Link());

if ($content) {
Expand Down Expand Up @@ -886,12 +885,10 @@ public function onBeforeDuplicate($original, $doWrite)
*/
public function duplicateWithChildren()
{
/** @var SiteTree $clone */
$clone = $this->duplicate();
$children = $this->AllChildren();

if ($children) {
/** @var SiteTree $child */
$sort = 0;
foreach ($children as $child) {
$childClone = method_exists($child, 'duplicateWithChildren')
Expand All @@ -914,7 +911,6 @@ public function duplicateWithChildren()
*/
public function duplicateAsChild($id)
{
/** @var SiteTree $newSiteTree */
$newSiteTree = $this->duplicate();
$newSiteTree->ParentID = $id;
$newSiteTree->Sort = 0;
Expand Down Expand Up @@ -950,7 +946,7 @@ public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType =
* @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal.
* @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0
*
* @return ArrayList
* @return ArrayList<SiteTree>
*/
public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $showHidden = false)
{
Expand Down Expand Up @@ -1416,7 +1412,6 @@ public function collateDescendants($condition, &$collator)
if (eval("return $condition;")) {
$collator[] = $item;
}
/** @var SiteTree $item */
$item->collateDescendants($condition, $collator);
}
return true;
Expand Down Expand Up @@ -1768,7 +1763,6 @@ public function onBeforeDelete()
// If deleting this page, delete all its children.
if ($this->isInDB() && SiteTree::config()->get('enforce_strict_hierarchy')) {
foreach ($this->AllChildren() as $child) {
/** @var SiteTree $child */
$child->delete();
}
}
Expand Down Expand Up @@ -1966,7 +1960,7 @@ public function getLiveURLSegment()
/**
* Get the back-link tracking objects that link to this page
*
* @return ArrayList|DataObject[]
* @return ArrayList<DataObject>
*/
public function BackLinkTracking()
{
Expand Down Expand Up @@ -2007,7 +2001,7 @@ public function BackLinkTracking()
* Returns the pages that depend on this page. This includes virtual pages, pages that link to it, etc.
*
* @param bool $includeVirtuals Set to false to exlcude virtual pages.
* @return ArrayList|SiteTree[]
* @return ArrayList<SiteTree>
*/
public function DependentPages($includeVirtuals = true)
{
Expand Down Expand Up @@ -2057,7 +2051,7 @@ public function DependentPages($includeVirtuals = true)
/**
* Return all virtual pages that link to this page.
*
* @return DataList
* @return DataList<SiteTree>
*/
public function VirtualPages()
{
Expand Down Expand Up @@ -2104,7 +2098,6 @@ public function getCMSFields()
false,
$dependentPages
);
/** @var GridFieldDataColumns $dataColumns */
$dataColumns = $dependentTable->getConfig()->getComponentByType(GridFieldDataColumns::class);
$dataColumns
->setDisplayFields($dependentColumns)
Expand Down Expand Up @@ -3333,7 +3326,6 @@ protected function updateDependentPages()
$this->flushCache();

// Need to mark pages depending to this one as broken
/** @var Page $page */
foreach ($this->DependentPages() as $page) {
// Update sync link tracking
$page->syncLinkTracking();
Expand Down
3 changes: 3 additions & 0 deletions code/Model/SiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

/**
* Plug-ins for additional functionality in your SiteTree classes.
*
* @template T of SiteTree
* @extends DataExtension<T>
*/
abstract class SiteTreeExtension extends DataExtension
{
Expand Down
2 changes: 2 additions & 0 deletions code/Model/SiteTreeLinkTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*
* @property DataObject|SiteTreeLinkTracking $owner
* @method ManyManyThroughList<SiteTree> LinkTracking()
*
* @extends DataExtension<DataObject>
*/
class SiteTreeLinkTracking extends DataExtension
{
Expand Down
Loading

0 comments on commit 2e20bd7

Please sign in to comment.