Skip to content

Commit

Permalink
ENH Add generic types
Browse files Browse the repository at this point in the history
- Add generic types where appropriate, e.g. for indicating the type of
records inside DataList or ArrayList.
- Replace @Property annotation for extension $owner property with a
generic extension. This works effectively the same way, with the bonus
benefit that the return type for getOwner() is also correct.
- Remove unnecessary @var comments.
  • Loading branch information
GuySartorelli committed Jan 12, 2024
1 parent 12a8d79 commit b55dcf4
Show file tree
Hide file tree
Showing 26 changed files with 37 additions and 52 deletions.
3 changes: 2 additions & 1 deletion src/Extension/FluentCMSMainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*
* @see FluentSiteTreeExtension::updateSavePublishActions()
* @see CopyLocaleAction::handleAction()
* @property CMSMain $owner
*
* @extends Extension<CMSMain>
*/
class FluentCMSMainExtension extends Extension
{
Expand Down
3 changes: 1 addition & 2 deletions src/Extension/FluentChangesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Adds locale-specific extensions to ChangeSet
*
* @property ChangeSetItem $owner
* @extends DataExtension<ChangeSetItem>
*/
class FluentChangesExtension extends DataExtension
{
Expand All @@ -26,7 +26,6 @@ public function updateChangeType(&$type, $draftVersion, $liveVersion)
}

// Mark any fluent object as modified if otherwise treated as null
/** @var ChangeSetItem $owner */
$owner = $this->owner;
foreach ($owner->Object()->getExtensionInstances() as $extension) {
if ($extension instanceof FluentExtension) {
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/FluentDateTimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use TractorCow\Fluent\Model\Locale;

/**
* @property DBDatetime $owner
* @extends Extension<DBDatetime>
*/
class FluentDateTimeExtension extends Extension
{
Expand Down
3 changes: 1 addition & 2 deletions src/Extension/FluentDirectorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Fluent extension for {@link \SilverStripe\Control\Director} to apply routing rules for locales
*
* @property Director $owner
* @extends Extension<Director>
*/
class FluentDirectorExtension extends Extension
{
Expand Down Expand Up @@ -138,7 +138,6 @@ protected function getExplicitRoutes($originalRules)
{
$queryParam = static::config()->get('query_param');
$rules = [];
/** @var Locale $localeObj */
foreach (Locale::getCached() as $localeObj) {
$locale = $localeObj->getLocale();
$url = $localeObj->getURLSegment();
Expand Down
6 changes: 3 additions & 3 deletions src/Extension/FluentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
* - data_exclude
* - data_include
*
* @property FluentExtension|DataObject $owner
* @template T of DataObject
* @extends DataExtension<T&static>
*/
class FluentExtension extends DataExtension
{
Expand Down Expand Up @@ -998,13 +999,12 @@ protected function getManipulationRecordID($updates)
/**
* Templatable list of all locale information for this record
*
* @return ArrayList|RecordLocale[]
* @return ArrayList<RecordLocale>
*/
public function Locales()
{
$data = [];
foreach (Locale::getCached() as $localeObj) {
/** @var Locale $localeObj */
$data[] = $this->owner->LocaleInformation($localeObj->getLocale());
}
return ArrayList::create($data);
Expand Down
3 changes: 2 additions & 1 deletion src/Extension/FluentFilteredExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
use TractorCow\Fluent\State\FluentState;

/**
* @property FluentFilteredExtension|DataObject $owner
* @method DataList|Locale[] FilteredLocales()
*
* @extends DataExtension<DataObject&static>
*/
class FluentFilteredExtension extends DataExtension
{
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/FluentGridFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Supports GridFieldDetailForm_ItemRequest with extra actions
*
* @property GridFieldDetailForm_ItemRequest $owner
* @extends Extension<GridFieldDetailForm_ItemRequest>
*/
class FluentGridFieldExtension extends Extension
{
Expand Down
3 changes: 2 additions & 1 deletion src/Extension/FluentIsolatedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* Note: You cannot use this extension on any object with the other fluent extensions
*
* @property int $LocaleID
* @property FluentIsolatedExtension|DataObject $owner
* @method Locale Locale()
*
* @extends DataExtension<DataObject&static>
*/
class FluentIsolatedExtension extends DataExtension
{
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/FluentLeftAndMainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use TractorCow\Fluent\Extension\Traits\FluentBadgeTrait;

/**
* @property LeftAndMain $owner
* @extends Extension<LeftAndMain>
*/
class FluentLeftAndMainExtension extends Extension
{
Expand Down
7 changes: 5 additions & 2 deletions src/Extension/FluentMemberExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member;
use SilverStripe\Security\Member_GroupSet;
use SilverStripe\Security\Permission;
use TractorCow\Fluent\Model\Locale;
use TractorCow\Fluent\State\FluentState;

/**
* @extends DataExtension<Member>
*/
class FluentMemberExtension extends DataExtension
{
/**
Expand Down Expand Up @@ -46,7 +50,7 @@ public function updateGroups(Member_GroupSet &$groups)
/**
* Get list of locales that the user has CMS access in
*
* @return Locale[]|ArrayList
* @return ArrayList<Locale>
*/
public function getCMSAccessLocales()
{
Expand Down Expand Up @@ -74,7 +78,6 @@ public function getCMSAccessLocales()
protected function getLocalePermissionsForGroup(Group $group)
{
$localePermissions = [];
/** @var Permission $permission */
foreach ($group->Permissions() as $permission) {
$prefix = Locale::CMS_ACCESS_FLUENT_LOCALE;
$begin = substr($permission->Code, 0, strlen($prefix));
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/FluentReadVersionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Available since SilverStripe 4.3.x
*
* @property ReadVersions $owner
* @extends Extension<ReadVersions>
*/
class FluentReadVersionsExtension extends Extension
{
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/FluentSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Fluent extension for SiteTree
*
* @property FluentSiteTreeExtension|SiteTree $owner
* @extends FluentVersionedExtension<SiteTree&static>
*/
class FluentSiteTreeExtension extends FluentVersionedExtension
{
Expand Down
8 changes: 2 additions & 6 deletions src/Extension/FluentVersionedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
* Important: If adding this to a custom object, this extension must be added AFTER the versioned extension.
* Use yaml `after` to enforce this
*
* @property DataObject|FluentVersionedExtension $owner
* @template T of DataObject&Versioned
* @extends FluentExtension<T&static>
*/
class FluentVersionedExtension extends FluentExtension implements Resettable
{
Expand Down Expand Up @@ -156,7 +157,6 @@ protected function augmentDatabaseRequireTable($localisedTable, $fields, $indexe
*/
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
{
/** @var Locale|null $locale */
$locale = $this->getDataQueryLocale($dataQuery);
if (!$locale) {
return;
Expand Down Expand Up @@ -228,7 +228,6 @@ protected function addLocaleFallbackChain(SQLSelect $query, $tableName, Locale $
$baseTable = $this->owner->baseTable();

foreach ($locale->getChain() as $joinLocale) {
/** @var Locale $joinLocale */
$joinAlias = $this->getLocalisedTable($tableName, $joinLocale->Locale);
$versionTable = $baseTable . self::SUFFIX_VERSIONS;

Expand Down Expand Up @@ -386,7 +385,6 @@ public function existsInLocale($locale = null)
*/
public function stagesDifferInLocale($locale = null): bool
{
/** @var DataObject|Versioned|FluentExtension|FluentVersionedExtension $record */
$record = $this->owner;
$id = $record->ID ?: $record->OldID;
$class = get_class($record);
Expand Down Expand Up @@ -518,7 +516,6 @@ public static function reset()
{
static::$idsInLocaleCache = [];

/** @var FluentVersionedExtension $singleton */
$singleton = singleton(static::class);
$singleton->flushVersionsCache();
}
Expand Down Expand Up @@ -575,7 +572,6 @@ public static function prepoulateIdsInLocale($locale, $dataObjectClass, $populat

// Populate both the draft and live stages
foreach ($tables as $table) {
/** @var SQLSelect $select */
$select = SQLSelect::create(
['"RecordID"'],
'"' . $table . '"',
Expand Down
1 change: 0 additions & 1 deletion src/Extension/Traits/FluentBadgeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ protected function addFluentBadge($badgeField, DataObject $record)
*/
public function getBadge(DataObject $record)
{
/** @var Locale $currentLocale */
$currentLocale = Locale::getCurrentLocale();
if (!$currentLocale) {
return null;
Expand Down
13 changes: 6 additions & 7 deletions src/Extension/Traits/FluentObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

/**
* Shared functionality between both FluentExtension and FluentFilteredExtension
*
* @property DataObject $owner
*/
trait FluentObjectTrait
{
Expand All @@ -41,7 +39,7 @@ abstract public function updateLocalisationTabConfig(
/**
* Gets list of all Locale dataobjects, linked to this record
*
* @return ArrayList|Locale[]
* @return ArrayList<Locale>
* @see Locale::RecordLocale()
*/
public function LinkedLocales()
Expand Down Expand Up @@ -90,7 +88,9 @@ public function augmentDataQueryCreation(
*/
protected function updateFluentCMSFields(FieldList $fields)
{
if (!$this->owner->ID) {
/** @var DataObject $owner */
$owner = $this->owner;
if (!$owner->ID) {
return;
}

Expand All @@ -102,19 +102,18 @@ protected function updateFluentCMSFields(FieldList $fields)
// Generate gridfield for handling localisations
$config = GridFieldConfig_Base::create();

/** @var GridFieldDataColumns $columns */
$columns = $config->getComponentByType(GridFieldDataColumns::class);
$summaryColumns = [
'Title' => 'Title',
'Locale' => 'Locale'
];

// Let extensions override columns
$this->owner->extend('updateLocalisationTabColumns', $summaryColumns);
$owner->extend('updateLocalisationTabColumns', $summaryColumns);
$columns->setDisplayFields($summaryColumns);

// Let extensions override components
$this->owner->extend('updateLocalisationTabConfig', $config);
$owner->extend('updateLocalisationTabConfig', $config);

// Add gridfield to tab / fields
$gridField = GridField::create(
Expand Down
1 change: 0 additions & 1 deletion src/Forms/CopyLocaleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ protected function appliesToRecord(DataObject $record, Locale $locale)
throw new LogicException("Error loading locale");
}

/** @var RecordLocale $fromRecordLocale */
$fromRecordLocale = RecordLocale::create($record, $fromLocale);
return $fromRecordLocale->IsDraft();
}
Expand Down
1 change: 0 additions & 1 deletion src/Forms/VisibleLocalesColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function getColumnContent($gridField, $record, $columnName)
}

$label = '';
/** @var Locale $locale */
foreach (Locale::getLocales() as $locale) {
$label .= $this->generateBadgeHTML($record, $locale);
}
Expand Down
3 changes: 0 additions & 3 deletions src/Middleware/DetectLocaleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ protected function getDomainLocale()
// If the current domain has exactly one locale, the locale is non-ambiguous
$locales = Locale::getCached()->filter('DomainID', $domainObj->ID);
if ($locales->count() == 1) {
/** @var Locale $localeObject */
$localeObject = $locales->first();
if ($localeObject) {
return $localeObject->getLocale();
Expand All @@ -313,7 +312,6 @@ protected function getDetectedLocale(HTTPRequest $request)
return null;
}

/** @var LocaleDetector $detector */
$detector = Injector::inst()->get(LocaleDetector::class);
$localeObj = $detector->detectLocale($request);
if ($localeObj) {
Expand Down Expand Up @@ -358,7 +356,6 @@ protected function validateAllowedLocale(FluentState $state)
// If limited to one or more locales, check that the current locale is in
// this list
$allowedLocales = $member->getCMSAccessLocales();
/** @var Locale $firstAllowedLocale */
$firstAllowedLocale = $allowedLocales->first();
if ($firstAllowedLocale && !$allowedLocales->find('Locale', $state->getLocale())) {
// Force state to the first allowed locale
Expand Down
3 changes: 1 addition & 2 deletions src/Model/CachableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
trait CachableModel
{

/**
* @return ArrayList|static[]
* @return ArrayList<static>
*/
public static function getCached()
{
Expand Down
1 change: 0 additions & 1 deletion src/Model/Delete/UsesDeletePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function updateDeleteTables(&$queriedTables)
}
$queriedTables = [];

/** @var DeletePolicy $policy */
$policy = Injector::inst()->create(DeletePolicy::class, $this->owner);
$policy->delete($this->owner);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Model/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function getCMSFields()

// Don't show "Is Default" column, as this is not locale-specific default
$localeConfig = GridFieldConfig_RelationEditor::create();
/** @var GridFieldDataColumns $detailRow */
$detailRow = $localeConfig->getComponentByType(GridFieldDataColumns::class);
$detailRow->setDisplayFields([
'Title' => 'Title',
Expand Down Expand Up @@ -184,7 +183,7 @@ public static function getByDomain($domain)
/**
* Get locales for this domain
*
* @return ArrayList|Locale[]
* @return ArrayList<Locale>
*/
public function getLocales()
{
Expand Down
Loading

0 comments on commit b55dcf4

Please sign in to comment.