Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH PHP 8.1 compatibility #356

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Caching/ProxyCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ public function getMultiple($keys, $default = null)
// Enforce $poolResult is same length / order as $keyIDs prior to combining back
$items = array_map(function ($keyID) use ($default, $itemsByID) {
return isset($itemsByID[$keyID]) ? $itemsByID[$keyID] : $default;
}, $keyIDs);
}, $keyIDs ?? []);

// Combine back with original keys
return array_combine($keys, $items);
return array_combine($keys ?? [], $items ?? []);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Caching/VersionedCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function getKeyID($key)
{
$state = Versioned::get_reading_mode();
if ($state) {
return $key . '_' . md5($state);
return $key . '_' . md5($state ?? '');
}
return $key;
}
Expand Down
6 changes: 3 additions & 3 deletions src/ChangeSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ protected function calculateImplicit()
$all = array_merge($referenced, $explicit);

/** @var string[][] $implicit Anything that is in $all, but not in $explicit, is an implicit inclusion */
$implicit = array_diff_key($all, $explicit);
$implicit = array_diff_key($all ?? [], $explicit);

foreach ($implicit as $key => $object) {
$implicit[$key]['ReferencedBy'] = $references[$key];
Expand Down Expand Up @@ -315,7 +315,7 @@ public function sync()
$objectKey = $this->implicitKey($item);

// If a ChangeSetItem exists, but isn't in $implicit, it's no longer required, so delete it
if (!array_key_exists($objectKey, $implicit)) {
if (!array_key_exists($objectKey, $implicit ?? [])) {
$item->delete();
} else {
// Otherwise it is required, so update ReferencedBy and remove from $implicit
Expand Down Expand Up @@ -352,7 +352,7 @@ public function isSynced()
$objectKey = $this->implicitKey($item);

// If a ChangeSetItem exists, but isn't in $implicit -> validation failure
if (!array_key_exists($objectKey, $implicit)) {
if (!array_key_exists($objectKey, $implicit ?? [])) {
return false;
}
// Exists, remove from $implicit
Expand Down
10 changes: 5 additions & 5 deletions src/ChangeSetItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function ThumbnailURL($width, $height)
*/
public function getChangeType()
{
if (!class_exists($this->ObjectClass)) {
if (!class_exists($this->ObjectClass ?? '')) {
throw new UnexpectedDataException("Invalid Class '{$this->ObjectClass}' in ChangeSetItem #{$this->ID}");
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public function getChangeType()
*/
protected function getObjectInStage($stage)
{
if (!class_exists($this->ObjectClass)) {
if (!class_exists($this->ObjectClass ?? '')) {
throw new UnexpectedDataException("Invalid Class '{$this->ObjectClass}' in ChangeSetItem #{$this->ID}");
}

Expand All @@ -195,7 +195,7 @@ protected function getObjectInStage($stage)
*/
protected function getObjectLatestVersion()
{
if (!class_exists($this->ObjectClass)) {
if (!class_exists($this->ObjectClass ?? '')) {
throw new UnexpectedDataException("Invalid Class '{$this->ObjectClass}' in ChangeSetItem #{$this->ID}");
}

Expand Down Expand Up @@ -257,7 +257,7 @@ public function findReferenced()
*/
public function publish()
{
if (!class_exists($this->ObjectClass)) {
if (!class_exists($this->ObjectClass ?? '')) {
throw new UnexpectedDataException("Invalid Class '{$this->ObjectClass}' in ChangeSetItem #{$this->ID}");
}

Expand Down Expand Up @@ -554,7 +554,7 @@ public function CMSEditLink()
*/
public function isVersioned()
{
if (!$this->ObjectClass || !class_exists($this->ObjectClass)) {
if (!$this->ObjectClass || !class_exists($this->ObjectClass ?? '')) {
return false;
}
/** @var Versioned|DataObject $singleton */
Expand Down
12 changes: 6 additions & 6 deletions src/DataDifferencer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ public function diffedData()
$fields = array_keys($diffed->toMap() + $this->toRecord->toMap());
} else {
$diffed = clone $this->toRecord;
$fields = array_keys($this->toRecord->toMap());
$fields = array_keys($this->toRecord->toMap() ?? []);
}

$hasOnes = array_merge($this->fromRecord->hasOne(), $this->toRecord->hasOne());

// Loop through properties
foreach ($fields as $field) {
if (in_array($field, $this->ignoredFields)) {
if (in_array($field, $this->ignoredFields ?? [])) {
continue;
}
if (in_array($field, array_keys($hasOnes))) {
if (in_array($field, array_keys($hasOnes ?? []))) {
continue;
}

Expand Down Expand Up @@ -134,7 +134,7 @@ public function diffedData()

// Loop through has_one
foreach ($hasOnes as $relName => $relSpec) {
if (in_array($relName, $this->ignoredFields)) {
if (in_array($relName, $this->ignoredFields ?? [])) {
continue;
}

Expand Down Expand Up @@ -246,11 +246,11 @@ public function ChangedFields()
public function changedFieldNames()
{
$base = $this->fromRecord ?: $this->toRecord;
$fields = array_keys($base->toMap());
$fields = array_keys($base->toMap() ?? []);

$changedFields = [];
foreach ($fields as $field) {
if (in_array($field, $this->ignoredFields)) {
if (in_array($field, $this->ignoredFields ?? [])) {
continue;
}
if (!$this->fromRecord || $this->fromRecord->$field != $this->toRecord->$field) {
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/Operations/CopyToStageCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function createOperation(
$plugins = $config['plugins'] ?? [];
$mutationName = $config['name'] ?? null;
if (!$mutationName) {
$mutationName = 'copy' . ucfirst($typeName) . 'ToStage';
$mutationName = 'copy' . ucfirst($typeName ?? '') . 'ToStage';
}

return ModelMutation::create($model, $mutationName)
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/Operations/PublishCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PublishCreator extends AbstractPublishOperationCreator
*/
protected function createOperationName(string $typeName): string
{
return 'publish' . ucfirst($typeName);
return 'publish' . ucfirst($typeName ?? '');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/Operations/RollbackCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function createOperation(
$defaultPlugins = $this->config()->get('default_plugins');
$configPlugins = $config['plugins'] ?? [];
$plugins = array_merge($defaultPlugins, $configPlugins);
$mutationName = 'rollback' . ucfirst($typeName);
$mutationName = 'rollback' . ucfirst($typeName ?? '');
return ModelMutation::create($model, $mutationName)
->setPlugins($plugins)
->setType($typeName)
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/Operations/UnpublishCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class UnpublishCreator extends AbstractPublishOperationCreator
*/
protected function createOperationName(string $typeName): string
{
return 'unpublish' . ucfirst($typeName);
return 'unpublish' . ucfirst($typeName ?? '');
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/GraphQL/Resolvers/VersionFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function applyToList(DataList $list, array $versioningArgs): DataList
$statuses = $versioningArgs['status'];

// If we need to search archived records, we need to manually join draft table
if (in_array('archived', $statuses)) {
if (in_array('archived', $statuses ?? [])) {
$list = $list
->setDataQueryParam('Versioned.mode', 'latest_versions');
// Join a temporary alias BaseTable_Draft, renaming this on execution to BaseTable
Expand Down Expand Up @@ -120,32 +120,32 @@ public function applyToList(DataList $list, array $versioningArgs): DataList
$conditions = [];

// Modified exist on both stages, but differ
if (in_array('modified', $statuses)) {
if (in_array('modified', $statuses ?? [])) {
$conditions[] = "\"{$liveTable}\".\"ID\" IS NOT NULL AND \"{$draftTable}\".\"ID\" IS NOT NULL"
. " AND \"{$draftTable}\".\"Version\" <> \"{$liveTable}\".\"Version\"";
}

// Is deleted and sent to archive
if (in_array('archived', $statuses)) {
if (in_array('archived', $statuses ?? [])) {
// Note: Include items staged for deletion for the time being, as these are effectively archived
// we could split this out into "staged for deletion" in the future
$conditions[] = "\"{$draftTable}\".\"ID\" IS NULL";
}

// Is on draft only
if (in_array('draft', $statuses)) {
if (in_array('draft', $statuses ?? [])) {
$conditions[] = "\"{$liveTable}\".\"ID\" IS NULL AND \"{$draftTable}\".\"ID\" IS NOT NULL";
}

if (in_array('published', $statuses)) {
if (in_array('published', $statuses ?? [])) {
$conditions[] = "\"{$liveTable}\".\"ID\" IS NOT NULL";
}

// Validate that all statuses have been handled
if (empty($conditions) || count($statuses) !== count($conditions)) {
if (empty($conditions) || count($statuses ?? []) !== count($conditions ?? [])) {
throw new InvalidArgumentException("Invalid statuses provided");
}
$list = $list->whereAny(array_filter($conditions));
$list = $list->whereAny(array_filter($conditions ?? []));
break;
case 'version':
// Note: Only valid for ReadOne
Expand Down Expand Up @@ -222,6 +222,6 @@ protected function isValidDate($date)
{
$dt = DateTime::createFromFormat('Y-m-d', $date);

return ($dt !== false && !array_sum($dt->getLastErrors()));
return ($dt !== false && !array_sum($dt->getLastErrors() ?? []));
}
}
2 changes: 1 addition & 1 deletion src/GraphQL/Resolvers/VersionedResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static function resolvePublishOperation(array $context)
AbstractPublishOperationCreator::ACTION_PUBLISH,
AbstractPublishOperationCreator::ACTION_UNPUBLISH,
];
if (!in_array($action, $allowedActions)) {
if (!in_array($action, $allowedActions ?? [])) {
throw new InvalidArgumentException(sprintf(
'Invalid publish action: %s',
$action
Expand Down
2 changes: 1 addition & 1 deletion src/GridFieldArchiveAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function augmentColumns($gridField, &$columns)
$config->removeComponent($deleteComponent);
}
}
if (!in_array('Actions', $columns)) {
if (!in_array('Actions', $columns ?? [])) {
$columns[] = 'Actions';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/GridFieldRestoreAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getExtraData($gridField, $record, $columnName)
*/
public function augmentColumns($gridField, &$columns)
{
if (!in_array('Actions', $columns)) {
if (!in_array('Actions', $columns ?? [])) {
$columns[] = 'Actions';
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/ReadingMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function toDataQueryParams($mode)
if (!is_string($mode)) {
throw new InvalidArgumentException("mode must be a string");
}
$parts = explode('.', $mode);
$parts = explode('.', $mode ?? '');
switch ($parts[0]) {
case 'Archive':
$archiveStage = isset($parts[2]) ? $parts[2] : Versioned::DRAFT;
Expand Down Expand Up @@ -80,21 +80,21 @@ public static function fromDataQueryParams($params)
public static function fromQueryString($query)
{
if (is_string($query)) {
parse_str($query, $query);
parse_str($query ?? '', $query);
}
if (empty($query)) {
return null;
}
// Check date
$archiveDate = isset($query['archiveDate']) && strtotime($query['archiveDate'])
$archiveDate = isset($query['archiveDate']) && strtotime($query['archiveDate'] ?? '')
? $query['archiveDate']
: null;

// Check stage (ignore invalid stages)
$stage = null;
if (isset($query['stage']) && strcasecmp($query['stage'], Versioned::DRAFT) === 0) {
if (isset($query['stage']) && strcasecmp($query['stage'] ?? '', Versioned::DRAFT ?? '') === 0) {
$stage = Versioned::DRAFT;
} elseif (isset($query['stage']) && strcasecmp($query['stage'], Versioned::LIVE) === 0) {
} elseif (isset($query['stage']) && strcasecmp($query['stage'] ?? '', Versioned::LIVE ?? '') === 0) {
$stage = Versioned::LIVE;
}

Expand Down Expand Up @@ -129,7 +129,7 @@ public static function toQueryString($mode)
if (!is_string($mode)) {
throw new InvalidArgumentException("mode must be a string");
}
$parts = explode('.', $mode);
$parts = explode('.', $mode ?? '');
switch ($parts[0]) {
case 'Archive':
$archiveStage = isset($parts[2]) ? $parts[2] : Versioned::DRAFT;
Expand Down
12 changes: 6 additions & 6 deletions src/RecursivePublishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function findOwnersRecursive($recursive, $list, $lookup)
if ($owner->isInDB()) {
foreach ($lookup as $ownedClass => $classLookups) {
// Skip owners of other objects
if (!is_a($owner, $ownedClass)) {
if (!is_a($owner, $ownedClass ?? '')) {
continue;
}
foreach ($classLookups as $classLookup) {
Expand Down Expand Up @@ -323,7 +323,7 @@ public function unlinkDisownedObjects($source, $targetStage)
// dis-connected from this object (set ForeignKeyID = 0)
$owns = $owner->config()->get('owns');
$hasMany = $owner->config()->get('has_many');
$ownedHasMany = array_intersect($owns, array_keys($hasMany));
$ownedHasMany = array_intersect($owns ?? [], array_keys($hasMany ?? []));
if (empty($ownedHasMany)) {
return;
}
Expand Down Expand Up @@ -438,14 +438,14 @@ public function onBeforeDuplicate($original, &$doWrite, &$relations)
// Only duplicate owned relationships that are either exclusively owned,
// or require additional writes. Also exclude any custom non-relation ownerships.
$allowed = array_merge(
array_keys($this->owner->manyMany()), // Require mapping table duplications
array_keys($this->owner->belongsTo()), // Exclusive record must be duplicated
array_keys($this->owner->hasMany()) // Exclusive records should be duplicated
array_keys($this->owner->manyMany() ?? []), // Require mapping table duplications
array_keys($this->owner->belongsTo() ?? []), // Exclusive record must be duplicated
array_keys($this->owner->hasMany() ?? []) // Exclusive records should be duplicated
);
// Note: don't assume that owned has_one needs duplication, as these can be
// shared non-exclusively by both clone and original.
// Get candidates from ownership and intersect
$owns = $this->owner->config()->get('owns');
$relations = array_intersect($allowed, $owns);
$relations = array_intersect($allowed ?? [], $owns);
}
}
2 changes: 1 addition & 1 deletion src/RestoreAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function restore($item)
public static function getRestoreMessage($originalItem, $restoredItem, $changedLocation = false)
{
$restoredID = $restoredItem->Title ?: $restoredItem->ID;
$restoredType = strtolower($restoredItem->i18n_singular_name());
$restoredType = strtolower($restoredItem->i18n_singular_name() ?? '');

if (method_exists($restoredItem, 'CMSEditLink') &&
$restoredItem->CMSEditLink()) {
Expand Down
Loading