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 12, 2024
1 parent da4dac1 commit da12f4f
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 64 deletions.
9 changes: 0 additions & 9 deletions src/ChangeSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public function publish($isSynced = false)

DB::get_conn()->withTransaction(function () {
foreach ($this->Changes() as $change) {
/** @var ChangeSetItem $change */
$change->publish();
}

Expand All @@ -146,7 +145,6 @@ public function publish($isSynced = false)
// This is done as a safer alternative to deleting records on live that
// are deleted on stage.
foreach ($this->Changes() as $change) {
/** @var ChangeSetItem $change */
$change->unlinkDisownedObjects();
}

Expand Down Expand Up @@ -265,14 +263,12 @@ protected function calculateImplicit()
/** @var string[][] $references List of which explicit items reference each thing in referenced */
$references = [];

/** @var ChangeSetItem $item */
foreach ($this->Changes()->filter(['Added' => ChangeSetItem::EXPLICITLY]) as $item) {
$explicitKey = $this->implicitKey($item);
$explicit[$explicitKey] = true;

foreach ($item->findReferenced() as $referee) {
try {
/** @var DataObject $referee */
$key = $this->implicitKey($referee);

$referenced[$key] = [
Expand Down Expand Up @@ -324,7 +320,6 @@ public function sync()
$implicit = $this->calculateImplicit();

// Adjust the existing implicit ChangeSetItems for this ChangeSet
/** @var ChangeSetItem $item */
foreach ($this->Changes()->filter(['Added' => ChangeSetItem::IMPLICITLY]) as $item) {
$objectKey = $this->implicitKey($item);

Expand Down Expand Up @@ -410,7 +405,6 @@ public function canPublish($member = null)
}
// Check all explicitly added items
foreach ($this->Changes()->filter(['Added' => ChangeSetItem::EXPLICITLY]) as $change) {
/** @var ChangeSetItem $change */
if (!$change->canPublish($member)) {
return false;
}
Expand All @@ -427,7 +421,6 @@ public function canPublish($member = null)
public function hasChanges()
{
// All changes must be publishable
/** @var ChangeSetItem $change */
foreach ($this->Changes() as $change) {
if ($change->hasChange()) {
return true;
Expand All @@ -446,7 +439,6 @@ public function canRevert($member = null)
{
// All changes must be publishable
foreach ($this->Changes() as $change) {
/** @var ChangeSetItem $change */
if (!$change->canRevert($member)) {
return false;
}
Expand Down Expand Up @@ -528,7 +520,6 @@ public function getCMSFields()
public function getDetails()
{
// Check each change item
/** @var ChangeSetItem $change */
$total = 0;
$withChanges = 0;
foreach ($this->Changes() as $change) {
Expand Down
4 changes: 1 addition & 3 deletions src/ChangeSetItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function getObjectLatestVersion()
/**
* Get all implicit objects for this change
*
* @return SS_List
* @return SS_List<DataObject>
*/
public function findReferenced()
{
Expand All @@ -231,7 +231,6 @@ public function findReferenced()
}

// If changed on stage, include all owned objects for publish
/** @var DataObject|RecursivePublishable $draftRecord */
$draftRecord = $this->getObjectInStage(Versioned::DRAFT);
if (!$draftRecord) {
return ArrayList::create();
Expand Down Expand Up @@ -375,7 +374,6 @@ public function canRevert($member)
}

// Just get the best version as this object may not even exist on either stage anymore.
/** @var Versioned|DataObject $object */
$object = $this->getObjectLatestVersion();
if (!$object) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/DataDifferencer.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public function diffedData()
// Using relation name instead of database column name, because of FileField etc.
$setField = is_a($relSpec, Image::class, true) ? $relName : $relField;
$toTitle = '';
/** @var DataObject $relObjTo */
$relObjTo = null;
if ($this->toRecord->hasMethod($relName)) {
/** @var DataObject $relObjTo */
$relObjTo = $this->toRecord->$relName();
$toTitle = $this->getObjectDisplay($relObjTo);
}
Expand Down Expand Up @@ -204,6 +204,7 @@ protected function getObjectDisplay($object = null)
* - Diff: An HTML diff showing the changes
* - From: The older version of the field
* - To: The newer version of the field
* @return ArrayList<ArrayData>
*/
public function ChangedFields()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Dev/VersionedTestSessionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Decorates TestSession object to update get / post requests with versioned querystring arguments.
*
* @property TestSession $owner
* @extends VersionedStateExtension<TestSession>
*/
class VersionedTestSessionExtension extends VersionedStateExtension
{
Expand Down
5 changes: 3 additions & 2 deletions src/GraphQL/Resolvers/VersionFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public function applyToReadingState(array $versioningArgs)
}

/**
* @param DataList $list
* @template T of DataObject
* @param DataList<T> $list
* @param array $versioningArgs
* @throws InvalidArgumentException
* @return DataList
* @return DataList<T>
*/
public function applyToList(DataList $list, array $versioningArgs): DataList
{
Expand Down
5 changes: 3 additions & 2 deletions src/GraphQL/Resolvers/VersionedResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ private static function getVersionsList(DataObject $object)
}

/**
* @param DataList $list
* @template T of DataObject
* @param DataList<T> $list
* @param array $args
* @param array $context
* @param ResolveInfo $info
* @return DataList
* @return DataList<T>
* @see VersionedRead
*/
public static function resolveVersionedRead(DataList $list, array $args, array $context, ResolveInfo $info)
Expand Down
11 changes: 4 additions & 7 deletions src/RecursivePublishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Provides owns / owned_by and recursive publishing API for all objects.
* This extension is added to DataObject by default
*
* @property DataObject|RecursivePublishable $owner
* @extends DataExtension<DataObject&static>
*/
class RecursivePublishable extends DataExtension
{
Expand Down Expand Up @@ -133,15 +133,13 @@ public function deleteFromChangeSets()
$changeSetIDs = [];

// Remove all ChangeSetItems matching this record
/** @var ChangeSetItem $changeSetItem */
foreach (ChangeSetItem::get_for_object($this->owner) as $changeSetItem) {
$changeSetIDs[$changeSetItem->ChangeSetID] = $changeSetItem->ChangeSetID;
$changeSetItem->delete();
}

// Sync all affected changesets
if ($changeSetIDs) {
/** @var ChangeSet $changeSet */
foreach (ChangeSet::get()->byIDs($changeSetIDs) as $changeSet) {
$changeSet->sync();
}
Expand All @@ -155,7 +153,7 @@ public function deleteFromChangeSets()
*
* @param bool $recursive True if recursive
* @param ArrayList $list Optional list to add items to
* @return ArrayList list of objects
* @return ArrayList<DataObject&static> list of objects
*/
public function findOwned($recursive = true, $list = null)
{
Expand Down Expand Up @@ -191,7 +189,7 @@ public function hasOwned()
*
* @param bool $recursive True if recursive
* @param ArrayList $list Optional list to add items to
* @return ArrayList list of objects
* @return ArrayList<DataObject> list of objects
*/
public function findOwners($recursive = true, $list = null)
{
Expand All @@ -213,12 +211,11 @@ public function findOwners($recursive = true, $list = null)
* @param bool $recursive True if recursive
* @param ArrayList $list List to add items to
* @param array $lookup List of reverse lookup rules for owned objects
* @return ArrayList list of objects
* @return ArrayList<DataObject> list of objects
*/
public function findOwnersRecursive($recursive, $list, $lookup)
{
// First pass: find objects that are explicitly owned_by (e.g. custom relationships)
/** @var DataObject $owner */
$owner = $this->owner;
$owners = $owner->findRelatedObjects('owned_by', false);

Expand Down
3 changes: 3 additions & 0 deletions src/RecursivePublishableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace SilverStripe\Versioned;

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Core\Extension;
use SilverStripe\ORM\DataObject;

/**
* Provides recursive publishable behaviour for LeftAndMain and GridFieldDetailForm_ItemRequest
*
* @extends Extension<LeftAndMain>
*/
class RecursivePublishableHandler extends Extension
{
Expand Down
3 changes: 0 additions & 3 deletions src/RecursiveStagesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function flushCachedData(): void

public static function reset(): void
{
/** @var RecursiveStagesInterface $service */
$service = Injector::inst()->get(RecursiveStagesInterface::class);

if (!$service instanceof RecursiveStagesService) {
Expand Down Expand Up @@ -116,7 +115,6 @@ protected function getOwnedIdentifiers(DataObject $object, string $stage): array
$identifiers = Versioned::withVersionedMode(function () use ($object, $stage): array {
Versioned::set_stage($stage);

/** @var DataObject $stagedObject */
$stagedObject = DataObject::get_by_id($object->ClassName, $object->ID);

if ($stagedObject === null) {
Expand Down Expand Up @@ -154,7 +152,6 @@ protected function getOwnedIdentifiers(DataObject $object, string $stage): array
*/
protected function getOwnedObjects(DataObject $object): array
{
/** @var DataObject|Versioned $object */
if (!$object->hasExtension(RecursivePublishable::class)) {
return [];
}
Expand Down
Loading

0 comments on commit da12f4f

Please sign in to comment.