Skip to content

Commit

Permalink
Merge pull request #338 from creative-commoners/pulls/3/php81
Browse files Browse the repository at this point in the history
ENH PHP 8.1 compatibility
  • Loading branch information
emteknetnz authored Apr 22, 2022
2 parents ec93c99 + 93e4379 commit 71d60b0
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Extensions/GridFieldDetailFormItemRequestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function getHTMLFragment(GridFieldAddNewMultiClass $component)

$classes = $component->getClasses($grid);

if (!count($classes)) {
if (!count($classes ?? [])) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions src/GridFieldAddNewInlineButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getHTMLFragments($grid)
private function getRowTemplate(GridField $grid, GridFieldEditableColumns $editable)
{
$columns = ArrayList::create();
$handled = array_keys($editable->getDisplayFields($grid));
$handled = array_keys($editable->getDisplayFields($grid) ?? []);

if ($grid->getList()) {
$record = Injector::inst()->create($grid->getModelClass());
Expand All @@ -129,7 +129,7 @@ private function getRowTemplate(GridField $grid, GridFieldEditableColumns $edita
$fields = $editable->getFields($grid, $record);

foreach ($grid->getColumns() as $column) {
if (in_array($column, $handled)) {
if (in_array($column, $handled ?? [])) {
$field = $fields->dataFieldByName($column);
$field->setName(sprintf(
'%s[%s][{%%=o.num%%}][%s]',
Expand All @@ -149,7 +149,7 @@ private function getRowTemplate(GridField $grid, GridFieldEditableColumns $edita
$content = str_replace(
sprintf('[%s][0]', GridFieldEditableColumns::POST_KEY),
sprintf('[%s][{%%=o.num%%}]', self::POST_KEY),
$content
$content ?? ''
);
}

Expand Down Expand Up @@ -215,7 +215,7 @@ public function handleSave(GridField $grid, DataObjectInterface $record)
}

if ($list instanceof ManyManyList) {
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
$extra = array_intersect_key($form->getData() ?? [], (array) $list->getExtraFields());
}

$item->write(false, false, false, true);
Expand Down
12 changes: 6 additions & 6 deletions src/GridFieldAddNewMultiClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function getClasses(GridField $grid)
$result = array();

if (is_null($this->classes)) {
$classes = array_values(ClassInfo::subclassesFor($grid->getModelClass()));
$classes = array_values(ClassInfo::subclassesFor($grid->getModelClass()) ?? []);
sort($classes);
} else {
$classes = $this->classes;
Expand All @@ -135,7 +135,7 @@ public function getClasses(GridField $grid)
if (!is_string($class)) {
$class = $title;
}
if (!class_exists($class)) {
if (!class_exists($class ?? '')) {
continue;
}
$is_abstract = (($reflection = new ReflectionClass($class)) && $reflection->isAbstract());
Expand Down Expand Up @@ -216,7 +216,7 @@ public function handleAdd($grid, $request)
throw new Exception('The add new multi class component requires the detail form component.');
}

if (!$class || !array_key_exists($class, $classes)) {
if (!$class || !array_key_exists($class, $classes ?? [])) {
throw new HTTPResponse_Exception(400);
}

Expand All @@ -241,7 +241,7 @@ public function getHTMLFragments($grid)
{
$classes = $this->getClasses($grid);

if (!count($classes)) {
if (!count($classes ?? [])) {
return array();
}

Expand Down Expand Up @@ -293,7 +293,7 @@ public function setItemRequestClass($class)
*/
protected function sanitiseClassName($class)
{
return str_replace('\\', '-', $class);
return str_replace('\\', '-', $class ?? '');
}

/**
Expand All @@ -304,6 +304,6 @@ protected function sanitiseClassName($class)
*/
protected function unsanitiseClassName($class)
{
return str_replace('-', '\\', $class);
return str_replace('-', '\\', $class ?? '');
}
}
2 changes: 1 addition & 1 deletion src/GridFieldAddNewMultiClassHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function Link($action = null)
*/
protected function sanitiseClassName($class)
{
return str_replace('\\', '-', $class);
return str_replace('\\', '-', $class ?? '');
}
}
4 changes: 2 additions & 2 deletions src/GridFieldConfigurablePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function setPageSizes(array $pageSizes)
$this->pageSizes = $pageSizes;

// Reset items per page
$this->setItemsPerPage(current($pageSizes));
$this->setItemsPerPage(current($pageSizes ?? []));

return $this;
}
Expand Down Expand Up @@ -356,7 +356,7 @@ public function getHTMLFragments($gridField)
return array(
'footer' => $forTemplate->renderWith(
__CLASS__,
array('Colspan' => count($gridField->getColumns()))
array('Colspan' => count($gridField->getColumns() ?? []))
)
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/GridFieldEditableColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getColumnContent($grid, $record, $col)
if (!$this->displayFields) {
// If setDisplayFields() not used, utilize $summary_fields
// in a way similar to base class
$colRelation = explode('.', $col);
$colRelation = explode('.', $col ?? '');
$value = $grid->getDataFieldValue($record, $colRelation[0]);
$field = $fields->fieldByName($colRelation[0]);
if (!$field || $field->isReadonly() || $field->isDisabled()) {
Expand All @@ -82,7 +82,7 @@ public function getColumnContent($grid, $record, $col)

// Fall back to previous logic
if (!$field) {
$rel = (strpos($col, '.') === false); // field references a relation value
$rel = (strpos($col ?? '', '.') === false); // field references a relation value
$field = ($rel) ? clone $fields->fieldByName($col) : ReadonlyField::create($col);
}

Expand All @@ -91,7 +91,7 @@ public function getColumnContent($grid, $record, $col)
}
}

if (array_key_exists($col, $this->fieldCasting)) {
if (array_key_exists($col, $this->fieldCasting ?? [])) {
$value = $grid->getCastedValue($value, $this->fieldCasting[$col]);
}

Expand Down Expand Up @@ -157,7 +157,7 @@ public function handleSave(GridField $grid, DataObjectInterface $record)
}

if ($list instanceof ManyManyList) {
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
$extra = array_intersect_key($form->getData() ?? [], (array) $list->getExtraFields());
}

$item->write(false, false, false, true);
Expand Down Expand Up @@ -244,7 +244,7 @@ public function getFields(GridField $grid, DataObjectInterface $record)
if (!$field && $list instanceof ManyManyList) {
$extra = $list->getExtraFields();

if ($extra && array_key_exists($col, $extra)) {
if ($extra && array_key_exists($col, $extra ?? [])) {
$field = Injector::inst()->create($extra[$col], $col)->scaffoldFormField();
}
}
Expand All @@ -259,7 +259,7 @@ public function getFields(GridField $grid, DataObjectInterface $record)
// revert to looking good in cases where the field isn't
// available or is readonly
//
$colRelation = explode('.', $col);
$colRelation = explode('.', $col ?? '');
if ($class && $obj = DataObject::singleton($class)->dbObject($colRelation[0])) {
$field = $obj->scaffoldFormField();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/GridFieldExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GridFieldExternalLink extends GridFieldDataColumns
*/
public function augmentColumns($gridField, &$columns)
{
if (!in_array('Actions', $columns)) {
if (!in_array('Actions', $columns ?? [])) {
$columns[] = 'Actions';
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function validateSortField(SS_List $list)
if ($list instanceof ManyManyList) {
$extra = $list->getExtraFields();

if ($extra && array_key_exists($field, $extra)) {
if ($extra && array_key_exists($field, $extra ?? [])) {
return;
}
} elseif ($list instanceof ManyManyThroughList) {
Expand Down Expand Up @@ -239,7 +239,7 @@ public function getSortTable(SS_List $list)
if ($list instanceof ManyManyList) {
$extra = $list->getExtraFields();
$table = $list->getJoinTable();
if ($extra && array_key_exists($field, $extra)) {
if ($extra && array_key_exists($field, $extra ?? [])) {
return $table;
}
} elseif ($list instanceof ManyManyThroughList) {
Expand Down Expand Up @@ -277,8 +277,8 @@ public function getHTMLFragments($field)

public function augmentColumns($grid, &$cols)
{
if (!in_array('Reorder', $cols) && $grid->getState()->GridFieldOrderableRows->enabled) {
array_splice($cols, $this->reorderColumnNumber, 0, 'Reorder');
if (!in_array('Reorder', $cols ?? []) && $grid->getState()->GridFieldOrderableRows->enabled) {
array_splice($cols, $this->reorderColumnNumber ?? 0, 0, 'Reorder');
}
}

Expand Down Expand Up @@ -308,7 +308,7 @@ public function getColumnContent($grid, $record, $col)
// if it exists, not directly from the record
$throughListSorts = $this->getSortValuesFromManyManyThroughList($list, $this->getSortField());

if (array_key_exists($record->ID, $throughListSorts)) {
if (array_key_exists($record->ID, $throughListSorts ?? [])) {
$currentSortValue = $throughListSorts[$record->ID];
}
}
Expand Down Expand Up @@ -408,8 +408,8 @@ public function handleReorder($grid, $request)

// Get records from the `GridFieldEditableColumns` column
$gridFieldName = $grid->getName();
if (strpos($gridFieldName, '.') !== false) {
$gridFieldName = str_replace('.', '_', $gridFieldName);
if (strpos($gridFieldName ?? '', '.') !== false) {
$gridFieldName = str_replace('.', '_', $gridFieldName ?? '');
}

$data = $request->postVar($gridFieldName);
Expand Down Expand Up @@ -549,7 +549,7 @@ protected function executeReorder(GridField $grid, $sortedIDs)
$items = $list->filter('ID', $sortedIDs)->sort($sortterm);

// Ensure that each provided ID corresponded to an actual object.
if (count($items) != count($sortedIDs)) {
if (count($items ?? []) != count($sortedIDs ?? [])) {
return false;
}

Expand Down Expand Up @@ -720,7 +720,7 @@ protected function populateSortValues(DataList $list)
protected function getSortTableClauseForIds(DataList $list, $ids)
{
if (is_array($ids)) {
$value = 'IN (' . implode(', ', array_map('intval', $ids)) . ')';
$value = 'IN (' . implode(', ', array_map('intval', $ids ?? [])) . ')';
} else {
$value = '= ' . (int) $ids;
}
Expand All @@ -734,7 +734,7 @@ protected function getSortTableClauseForIds(DataList $list, $ids)
$foreignKey = $this->getManyManyInspectorForeignKey($introspector);
$foreignID = (int) $list->getForeignID();

if ($extra && array_key_exists($this->getSortField(), $extra)) {
if ($extra && array_key_exists($this->getSortField(), $extra ?? [])) {
return sprintf(
'"%s" %s AND "%s" = %d',
$key,
Expand Down
8 changes: 4 additions & 4 deletions tests/GridFieldOrderableRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testReorderItems($fixtureID, $relationName, $sortName)
$config = new GridFieldConfig_RelationEditor();
$config->addComponent($orderable);

list($parentClass, $parentInstanceID) = explode('.', $fixtureID);
list($parentClass, $parentInstanceID) = explode('.', $fixtureID ?? '');
$parent = $this->objFromFixture($parentClass, $parentInstanceID);

$grid = new GridField(
Expand All @@ -83,7 +83,7 @@ public function testReorderItems($fixtureID, $relationName, $sortName)
$desiredOrder = [];

// Make order non-contiguous, and 1-based
foreach (array_reverse($originalOrder) as $index => $id) {
foreach (array_reverse($originalOrder ?? []) as $index => $id) {
$desiredOrder[$index * 2 + 1] = $id;
}

Expand Down Expand Up @@ -183,7 +183,7 @@ public function testSortableChildClass()
);

$originalOrder = $parent->Children()->column('ID');
$desiredOrder = array_reverse($originalOrder);
$desiredOrder = array_reverse($originalOrder ?? []);

$this->assertNotEquals($originalOrder, $desiredOrder);

Expand Down Expand Up @@ -270,7 +270,7 @@ public function testReorderItemsSubclassVersioned()
$desiredOrder = [];

// Make order non-contiguous, and 1-based
foreach (array_reverse($originalOrder) as $index => $id) {
foreach (array_reverse($originalOrder ?? []) as $index => $id) {
$desiredOrder[$index * 2 + 1] = $id;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/OrderableRowsThroughVersionedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public function testReorderingSavesAndPublishes()
$originalOrder = $parent->$relationName()->sort($sortName)->column('ID');
// Ring (un)shift by one, e.g. 3,2,1 becomes 1,3,2.
// then string key our new order starting at 1
$desiredOrder = array_values($originalOrder);
$desiredOrder = array_values($originalOrder ?? []);
array_unshift($desiredOrder, array_pop($desiredOrder));
$desiredOrder = array_combine(
range('1', count($desiredOrder)),
$desiredOrder
range('1', count($desiredOrder ?? [])),
$desiredOrder ?? []
);
$this->assertNotEquals($originalOrder, $desiredOrder);

Expand Down
2 changes: 1 addition & 1 deletion tests/Stub/StubA.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StubA implements TestOnly
public function i18n_singular_name()
{
$class = get_class($this);
return substr($class, -1);
return substr($class ?? '', -1);
}

public function canCreate()
Expand Down

0 comments on commit 71d60b0

Please sign in to comment.