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 #319

Merged
merged 1 commit into from
Apr 26, 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
2 changes: 1 addition & 1 deletion src/Search/Criteria/SearchCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ protected function getConjunctions()
protected function getConjunction($key)
{
$conjunctions = $this->getConjunctions();
if (!array_key_exists($key, $conjunctions)) {
if (!array_key_exists($key, $conjunctions ?? [])) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Search/FullTextSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function get_indexes($class = null, $rebuild = false)
}

if ($hidden) {
$candidates = array_diff($candidates, $hidden);
$candidates = array_diff($candidates ?? [], $hidden);
}

// Create all indexes
Expand All @@ -86,7 +86,7 @@ public static function get_indexes($class = null, $rebuild = false)

$valid = array();
foreach ($all as $indexclass => $instance) {
if (is_subclass_of($indexclass, $class)) {
if (is_subclass_of($indexclass, $class ?? '')) {
$valid[$indexclass] = $instance;
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/Search/Indexes/SearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ abstract class SearchIndex extends ViewableData
*/
protected function getSourceName($source)
{
$source = explode(self::config()->get('class_delimiter'), $source);
$source = explode(self::config()->get('class_delimiter') ?? '', $source ?? '');

return $source[0];
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public function __toString()
*/
public function fieldData($field, $forceType = null, $extraOptions = [])
{
$fullfield = str_replace(".", "_", $field);
$fullfield = str_replace(".", "_", $field ?? '');
$sources = $this->getClasses();

foreach ($sources as $source => $options) {
Expand All @@ -112,8 +112,8 @@ public function fieldData($field, $forceType = null, $extraOptions = [])

$found = [];

if (strpos($field, '.') !== false) {
$lookups = explode(".", $field);
if (strpos($field ?? '', '.') !== false) {
$lookups = explode(".", $field ?? '');
$field = array_pop($lookups);

foreach ($lookups as $lookup) {
Expand All @@ -133,7 +133,7 @@ public function fieldData($field, $forceType = null, $extraOptions = [])
// we only want to include base class for relation, omit classes that inherited the relation
$relationList = Config::inst()->get($dataclass, 'has_one', Config::UNINHERITED);
$relationList = (!is_null($relationList)) ? $relationList : [];
if (!array_key_exists($lookup, $relationList)) {
if (!array_key_exists($lookup, $relationList ?? [])) {
continue;
}

Expand All @@ -146,7 +146,7 @@ public function fieldData($field, $forceType = null, $extraOptions = [])
// we only want to include base class for relation, omit classes that inherited the relation
$relationList = Config::inst()->get($dataclass, 'has_many', Config::UNINHERITED);
$relationList = (!is_null($relationList)) ? $relationList : [];
if (!array_key_exists($lookup, $relationList)) {
if (!array_key_exists($lookup, $relationList ?? [])) {
continue;
}

Expand All @@ -160,7 +160,7 @@ public function fieldData($field, $forceType = null, $extraOptions = [])
// we only want to include base class for relation, omit classes that inherited the relation
$relationList = Config::inst()->get($dataclass, 'many_many', Config::UNINHERITED);
$relationList = (!is_null($relationList)) ? $relationList : [];
if (!array_key_exists($lookup, $relationList)) {
if (!array_key_exists($lookup, $relationList ?? [])) {
continue;
}

Expand Down Expand Up @@ -199,7 +199,7 @@ public function fieldData($field, $forceType = null, $extraOptions = [])
$class = $this->getSourceName($class);
$dataclasses = SearchIntrospection::hierarchy($class, $options['include_children']);

while (count($dataclasses)) {
while (count($dataclasses ?? [])) {
$dataclass = array_shift($dataclasses);
$type = null;
$fieldoptions = $options;
Expand Down Expand Up @@ -228,9 +228,9 @@ public function fieldData($field, $forceType = null, $extraOptions = [])

if ($type) {
// Don't search through child classes of a class we matched on. TODO: Should we?
$dataclasses = array_diff($dataclasses, array_values(ClassInfo::subclassesFor($dataclass)));
$dataclasses = array_diff($dataclasses ?? [], array_values(ClassInfo::subclassesFor($dataclass) ?? []));
// Trim arguments off the type string
if (preg_match('/^(\w+)\(/', $type, $match)) {
if (preg_match('/^(\w+)\(/', $type ?? '', $match)) {
$type = $match[1];
}
// Get the origin
Expand Down Expand Up @@ -412,7 +412,7 @@ public function variantStateExcluded($state)

public function buildDependancyList()
{
$this->dependancyList = array_keys($this->getClasses());
$this->dependancyList = array_keys($this->getClasses() ?? []);

foreach ($this->getFieldsIterator() as $name => $field) {
if (!isset($field['class'])) {
Expand All @@ -434,7 +434,7 @@ public function getDerivedFields()
$this->derivedFields = array();

foreach ($this->getFieldsIterator() as $name => $field) {
if (count($field['lookup_chain']) < 2) {
if (count($field['lookup_chain'] ?? []) < 2) {
continue;
}

Expand All @@ -445,7 +445,7 @@ public function getDerivedFields()
$this->derivedFields[$key]['fields'][$fieldname] = $fieldname;
SearchIntrospection::add_unique_by_ancestor($this->derivedFields['classes'], $field['class']);
} else {
$chain = array_reverse($field['lookup_chain']);
$chain = array_reverse($field['lookup_chain'] ?? []);
array_shift($chain);

$this->derivedFields[$key] = array(
Expand Down Expand Up @@ -473,7 +473,7 @@ public function getDocumentIDForState($base, $id, $state)
{
ksort($state);
$parts = array('id' => $id, 'base' => $base, 'state' => json_encode($state));
return implode('-', array_values($parts));
return implode('-', array_values($parts ?? []));
}

/**
Expand Down Expand Up @@ -586,7 +586,7 @@ public function getDirtyIDs($class, $id, $statefulids, $fields)

// First, if this object is directly contained in the index, add it
foreach ($this->classes as $searchclass => $options) {
if ($searchclass == $class || ($options['include_children'] && is_subclass_of($class, $searchclass))) {
if ($searchclass == $class || ($options['include_children'] && is_subclass_of($class, $searchclass ?? ''))) {
$base = DataObject::getSchema()->baseDataClass($searchclass);
$dirty[$base] = array();
foreach ($statefulids as $statefulid) {
Expand All @@ -605,7 +605,7 @@ public function getDirtyIDs($class, $id, $statefulids, $fields)
if (!SearchIntrospection::is_subclass_of($class, $derivation['classes'])) {
continue;
}
if (!array_intersect_key($fields, $derivation['fields'])) {
if (!array_intersect_key($fields ?? [], $derivation['fields'])) {
continue;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Search/Processors/SearchUpdateBatchedProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function process()
}

// Don't re-process completed queue
if ($this->currentBatch >= count($this->batches)) {
if ($this->currentBatch >= count($this->batches ?? [])) {
return true;
}

Expand Down Expand Up @@ -138,14 +138,14 @@ protected function segmentBatches($source)
while ($ids) {
// Estimate maximum number of items to take for this iteration, allowing for the soft cap
$take = $batchSize - $currentSize;
if (count($ids) <= $take + $softCap) {
if (count($ids ?? []) <= $take + $softCap) {
$take += $softCap;
}
$items = array_slice($ids, 0, $take, true);
$ids = array_slice($ids, count($items), null, true);
$items = array_slice($ids ?? [], 0, $take, true);
$ids = array_slice($ids ?? [], count($items ?? []), null, true);

// Update batch
$currentSize += count($items);
$currentSize += count($items ?? []);
$merge = array(
$base => array(
$stateKey => array(
Expand Down
14 changes: 7 additions & 7 deletions src/Search/Processors/SearchUpdateCommitJobProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function queue($dirty = true, $startAfter = null)

if ($dirty) {
$indexes = FullTextSearch::get_indexes();
static::$dirty_indexes = array_keys($indexes);
static::$dirty_indexes = array_keys($indexes ?? []);
}
return $id;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public function getAllIndexes()
{
if (empty($this->indexes)) {
$indexes = FullTextSearch::get_indexes();
$this->indexes = array_keys($indexes);
$this->indexes = array_keys($indexes ?? []);
}
return $this->indexes;
}
Expand All @@ -135,7 +135,7 @@ public function jobFinished()
{
// If we've indexed exactly as many as we would like, we are done
return $this->skipped
|| (count($this->getAllIndexes()) <= count($this->completed));
|| (count($this->getAllIndexes() ?? []) <= count($this->completed ?? []));
}

public function prepareForRestart()
Expand Down Expand Up @@ -213,7 +213,7 @@ protected function commitIndex($index)
{
// Skip index if this is already complete
$name = get_class($index);
if (in_array($name, $this->completed)) {
if (in_array($name, $this->completed ?? [])) {
$this->addMessage("Skipping already comitted index {$name}");
return;
}
Expand All @@ -240,8 +240,8 @@ public function setup()
public function getJobData()
{
$data = new stdClass();
$data->totalSteps = count($this->getAllIndexes());
$data->currentStep = count($this->completed);
$data->totalSteps = count($this->getAllIndexes() ?? []);
$data->currentStep = count($this->completed ?? []);
$data->isComplete = $this->jobFinished();
$data->messages = $this->messages;

Expand All @@ -265,7 +265,7 @@ public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $me

public function addMessage($message, $severity = 'INFO')
{
$severity = strtoupper($severity);
$severity = strtoupper($severity ?? '');
$this->messages[] = '[' . date('Y-m-d H:i:s') . "][$severity] $message";
}

Expand Down
4 changes: 2 additions & 2 deletions src/Search/Processors/SearchUpdateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function addDirtyIDs($class, $statefulids, $index)
$forclass[$statekey] = array('state' => $state, 'ids' => array($id => array($index)));
} elseif (!isset($forclass[$statekey]['ids'][$id])) {
$forclass[$statekey]['ids'][$id] = array($index);
} elseif (array_search($index, $forclass[$statekey]['ids'][$id]) === false) {
} elseif (array_search($index, $forclass[$statekey]['ids'][$id] ?? []) === false) {
$forclass[$statekey]['ids'][$id][] = $index;
// dirty count stays the same
}
Expand Down Expand Up @@ -89,7 +89,7 @@ protected function prepareIndexes()
SearchVariant::activate_state($state);

// Ensure that indexes for all new / updated objects are included
$objs = DataObject::get($base)->byIDs(array_keys($ids));
$objs = DataObject::get($base)->byIDs(array_keys($ids ?? []));

/** @var DataObject $obj */
foreach ($objs as $obj) {
Expand Down
6 changes: 3 additions & 3 deletions src/Search/Processors/SearchUpdateQueuedJobProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getJobType()

public function jobFinished()
{
return $this->currentBatch >= count($this->batches);
return $this->currentBatch >= count($this->batches ?? []);
}

public function setup()
Expand All @@ -68,7 +68,7 @@ public function afterComplete()
public function getJobData()
{
$data = new stdClass();
$data->totalSteps = count($this->batches);
$data->totalSteps = count($this->batches ?? []);
$data->currentStep = $this->currentBatch;
$data->isComplete = $this->jobFinished();
$data->messages = $this->messages;
Expand All @@ -91,7 +91,7 @@ public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $me

public function addMessage($message, $severity = 'INFO')
{
$severity = strtoupper($severity);
$severity = strtoupper($severity ?? '');
$this->messages[] = '[' . date('Y-m-d H:i:s') . "][$severity] $message";
}

Expand Down
10 changes: 5 additions & 5 deletions src/Search/SearchIntrospection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SearchIntrospection
public static function is_subclass_of($class, $of)
{
$ancestry = isset(self::$ancestry[$class]) ? self::$ancestry[$class] : (self::$ancestry[$class] = ClassInfo::ancestry($class));
return is_array($of) ? (bool)array_intersect($of, $ancestry) : array_key_exists($of, $ancestry);
return is_array($of) ? (bool)array_intersect($of, $ancestry) : array_key_exists($of, $ancestry ?? []);
}

protected static $hierarchy = array();
Expand All @@ -40,12 +40,12 @@ public static function hierarchy($class, $includeSubclasses = true, $dataOnly =
$key = "$class!" . ($includeSubclasses ? 'sc' : 'an') . '!' . ($dataOnly ? 'do' : 'al');

if (!isset(self::$hierarchy[$key])) {
$classes = array_values(ClassInfo::ancestry($class));
$classes = array_values(ClassInfo::ancestry($class) ?? []);
if ($includeSubclasses) {
$classes = array_unique(array_merge($classes, array_values(ClassInfo::subclassesFor($class))));
$classes = array_unique(array_merge($classes, array_values(ClassInfo::subclassesFor($class) ?? [])));
}

$idx = array_search(DataObject::class, $classes);
$idx = array_search(DataObject::class, $classes ?? []);
if ($idx !== false) {
array_splice($classes, 0, $idx+1);
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public static function add_unique_by_ancestor(&$list, $class)

// Strip out any subclasses of $class already in the list
$children = ClassInfo::subclassesFor($class);
$list = array_diff($list, $children);
$list = array_diff($list ?? [], $children);

// Then add the class in
$list[] = $class;
Expand Down
4 changes: 2 additions & 2 deletions src/Search/Services/SearchableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ private function classSkipsCanViewCheck(string $class): bool
if (empty($skipClasses)) {
return false;
}
if (in_array($class, $skipClasses)) {
if (in_array($class, $skipClasses ?? [])) {
return true;
}
foreach ($skipClasses as $skipClass) {
if (in_array($skipClass, class_parents($class))) {
if (in_array($skipClass, class_parents($class) ?? [])) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Search/Updaters/SearchUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function handle_manipulation($manipulation)
'command' => $command,
'fields' => array()
);
} elseif (is_subclass_of($class, $writes[$key]['class'])) {
} elseif (is_subclass_of($class, $writes[$key]['class'] ?? '')) {
// Otherwise update the class label if it's more specific than the currently recorded one
$writes[$key]['class'] = $class;
}
Expand All @@ -118,7 +118,7 @@ public static function handle_manipulation($manipulation)
}

// Trim non-delete records without fields
foreach (array_keys($writes) as $key) {
foreach (array_keys($writes ?? []) as $key) {
if ($writes[$key]['command'] !== 'delete' && empty($writes[$key]['fields'])) {
unset($writes[$key]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Search/Variants/SearchVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static function withCommon(array $classes = [])
// Merge the variants applicable to the current class into the list of common variants, using
// the variant instance to replace any previous versions for the same class name (should be singleton
// anyway).
$commonVariants = array_replace($commonVariants, $variantsForClass);
$commonVariants = array_replace($commonVariants ?? [], $variantsForClass);
}

// Cache for future calls
Expand Down Expand Up @@ -322,7 +322,7 @@ protected function mergeClasses($left, $right)
$merged = array_values(array_unique(array_merge($left, $right)));

// If there is only one item, return it as a single string
if (count($merged) === 1) {
if (count($merged ?? []) === 1) {
return reset($merged);
}
return $merged;
Expand Down
2 changes: 1 addition & 1 deletion src/Search/Variants/SearchVariantVersioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function extractManipulationState(&$manipulation)
$class = $details['class'];
$stage = Versioned::DRAFT;

if (preg_match('/^(.*)_' . Versioned::LIVE . '$/', $table, $matches)) {
if (preg_match('/^(.*)_' . Versioned::LIVE . '$/', $table ?? '', $matches)) {
$class = DataObject::getSchema()->tableClass($matches[1]);
$stage = Versioned::LIVE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Search/Variants/SearchVariant_Caller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function call($method, &...$args)
$values = array();

foreach ($this->variants as $variant) {
if (method_exists($variant, $method)) {
if (method_exists($variant, $method ?? '')) {
$value = $variant->$method(...$args);
if ($value !== null) {
$values[] = $value;
Expand Down
Loading