Compare commits

..

2 Commits

Author SHA1 Message Date
Steve Boyd
71d60b0734
Merge pull request #338 from creative-commoners/pulls/3/php81
ENH PHP 8.1 compatibility
2022-04-22 16:48:56 +12:00
Steve Boyd
93e4379c3d ENH PHP 8.1 compatibility 2022-04-13 17:44:44 +12:00
11 changed files with 39 additions and 39 deletions

View File

@ -47,7 +47,7 @@ class GridFieldDetailFormItemRequestExtension extends Extension
$classes = $component->getClasses($grid); $classes = $component->getClasses($grid);
if (!count($classes)) { if (!count($classes ?? [])) {
return false; return false;
} }

View File

@ -118,7 +118,7 @@ class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements
private function getRowTemplate(GridField $grid, GridFieldEditableColumns $editable) private function getRowTemplate(GridField $grid, GridFieldEditableColumns $editable)
{ {
$columns = ArrayList::create(); $columns = ArrayList::create();
$handled = array_keys($editable->getDisplayFields($grid)); $handled = array_keys($editable->getDisplayFields($grid) ?? []);
if ($grid->getList()) { if ($grid->getList()) {
$record = Injector::inst()->create($grid->getModelClass()); $record = Injector::inst()->create($grid->getModelClass());
@ -129,7 +129,7 @@ class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements
$fields = $editable->getFields($grid, $record); $fields = $editable->getFields($grid, $record);
foreach ($grid->getColumns() as $column) { foreach ($grid->getColumns() as $column) {
if (in_array($column, $handled)) { if (in_array($column, $handled ?? [])) {
$field = $fields->dataFieldByName($column); $field = $fields->dataFieldByName($column);
$field->setName(sprintf( $field->setName(sprintf(
'%s[%s][{%%=o.num%%}][%s]', '%s[%s][{%%=o.num%%}][%s]',
@ -149,7 +149,7 @@ class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements
$content = str_replace( $content = str_replace(
sprintf('[%s][0]', GridFieldEditableColumns::POST_KEY), sprintf('[%s][0]', GridFieldEditableColumns::POST_KEY),
sprintf('[%s][{%%=o.num%%}]', self::POST_KEY), sprintf('[%s][{%%=o.num%%}]', self::POST_KEY),
$content $content ?? ''
); );
} }
@ -215,7 +215,7 @@ class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements
} }
if ($list instanceof ManyManyList) { 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); $item->write(false, false, false, true);

View File

@ -124,7 +124,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements
$result = array(); $result = array();
if (is_null($this->classes)) { if (is_null($this->classes)) {
$classes = array_values(ClassInfo::subclassesFor($grid->getModelClass())); $classes = array_values(ClassInfo::subclassesFor($grid->getModelClass()) ?? []);
sort($classes); sort($classes);
} else { } else {
$classes = $this->classes; $classes = $this->classes;
@ -135,7 +135,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements
if (!is_string($class)) { if (!is_string($class)) {
$class = $title; $class = $title;
} }
if (!class_exists($class)) { if (!class_exists($class ?? '')) {
continue; continue;
} }
$is_abstract = (($reflection = new ReflectionClass($class)) && $reflection->isAbstract()); $is_abstract = (($reflection = new ReflectionClass($class)) && $reflection->isAbstract());
@ -216,7 +216,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements
throw new Exception('The add new multi class component requires the detail form component.'); 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); throw new HTTPResponse_Exception(400);
} }
@ -241,7 +241,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements
{ {
$classes = $this->getClasses($grid); $classes = $this->getClasses($grid);
if (!count($classes)) { if (!count($classes ?? [])) {
return array(); return array();
} }
@ -293,7 +293,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements
*/ */
protected function sanitiseClassName($class) protected function sanitiseClassName($class)
{ {
return str_replace('\\', '-', $class); return str_replace('\\', '-', $class ?? '');
} }
/** /**
@ -304,6 +304,6 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements
*/ */
protected function unsanitiseClassName($class) protected function unsanitiseClassName($class)
{ {
return str_replace('-', '\\', $class); return str_replace('-', '\\', $class ?? '');
} }
} }

View File

@ -31,6 +31,6 @@ class GridFieldAddNewMultiClassHandler extends GridFieldDetailForm_ItemRequest
*/ */
protected function sanitiseClassName($class) protected function sanitiseClassName($class)
{ {
return str_replace('\\', '-', $class); return str_replace('\\', '-', $class ?? '');
} }
} }

View File

@ -171,7 +171,7 @@ class GridFieldConfigurablePaginator extends GridFieldPaginator
$this->pageSizes = $pageSizes; $this->pageSizes = $pageSizes;
// Reset items per page // Reset items per page
$this->setItemsPerPage(current($pageSizes)); $this->setItemsPerPage(current($pageSizes ?? []));
return $this; return $this;
} }
@ -356,7 +356,7 @@ class GridFieldConfigurablePaginator extends GridFieldPaginator
return array( return array(
'footer' => $forTemplate->renderWith( 'footer' => $forTemplate->renderWith(
__CLASS__, __CLASS__,
array('Colspan' => count($gridField->getColumns())) array('Colspan' => count($gridField->getColumns() ?? []))
) )
); );
} }

View File

@ -61,7 +61,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
if (!$this->displayFields) { if (!$this->displayFields) {
// If setDisplayFields() not used, utilize $summary_fields // If setDisplayFields() not used, utilize $summary_fields
// in a way similar to base class // in a way similar to base class
$colRelation = explode('.', $col); $colRelation = explode('.', $col ?? '');
$value = $grid->getDataFieldValue($record, $colRelation[0]); $value = $grid->getDataFieldValue($record, $colRelation[0]);
$field = $fields->fieldByName($colRelation[0]); $field = $fields->fieldByName($colRelation[0]);
if (!$field || $field->isReadonly() || $field->isDisabled()) { if (!$field || $field->isReadonly() || $field->isDisabled()) {
@ -82,7 +82,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
// Fall back to previous logic // Fall back to previous logic
if (!$field) { 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); $field = ($rel) ? clone $fields->fieldByName($col) : ReadonlyField::create($col);
} }
@ -91,7 +91,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
} }
} }
if (array_key_exists($col, $this->fieldCasting)) { if (array_key_exists($col, $this->fieldCasting ?? [])) {
$value = $grid->getCastedValue($value, $this->fieldCasting[$col]); $value = $grid->getCastedValue($value, $this->fieldCasting[$col]);
} }
@ -157,7 +157,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
} }
if ($list instanceof ManyManyList) { 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); $item->write(false, false, false, true);
@ -244,7 +244,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
if (!$field && $list instanceof ManyManyList) { if (!$field && $list instanceof ManyManyList) {
$extra = $list->getExtraFields(); $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(); $field = Injector::inst()->create($extra[$col], $col)->scaffoldFormField();
} }
} }
@ -259,7 +259,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
// revert to looking good in cases where the field isn't // revert to looking good in cases where the field isn't
// available or is readonly // available or is readonly
// //
$colRelation = explode('.', $col); $colRelation = explode('.', $col ?? '');
if ($class && $obj = DataObject::singleton($class)->dbObject($colRelation[0])) { if ($class && $obj = DataObject::singleton($class)->dbObject($colRelation[0])) {
$field = $obj->scaffoldFormField(); $field = $obj->scaffoldFormField();
} else { } else {

View File

@ -19,7 +19,7 @@ class GridFieldExternalLink extends GridFieldDataColumns
*/ */
public function augmentColumns($gridField, &$columns) public function augmentColumns($gridField, &$columns)
{ {
if (!in_array('Actions', $columns)) { if (!in_array('Actions', $columns ?? [])) {
$columns[] = 'Actions'; $columns[] = 'Actions';
} }
} }

View File

@ -205,7 +205,7 @@ class GridFieldOrderableRows extends RequestHandler implements
if ($list instanceof ManyManyList) { if ($list instanceof ManyManyList) {
$extra = $list->getExtraFields(); $extra = $list->getExtraFields();
if ($extra && array_key_exists($field, $extra)) { if ($extra && array_key_exists($field, $extra ?? [])) {
return; return;
} }
} elseif ($list instanceof ManyManyThroughList) { } elseif ($list instanceof ManyManyThroughList) {
@ -239,7 +239,7 @@ class GridFieldOrderableRows extends RequestHandler implements
if ($list instanceof ManyManyList) { if ($list instanceof ManyManyList) {
$extra = $list->getExtraFields(); $extra = $list->getExtraFields();
$table = $list->getJoinTable(); $table = $list->getJoinTable();
if ($extra && array_key_exists($field, $extra)) { if ($extra && array_key_exists($field, $extra ?? [])) {
return $table; return $table;
} }
} elseif ($list instanceof ManyManyThroughList) { } elseif ($list instanceof ManyManyThroughList) {
@ -277,8 +277,8 @@ class GridFieldOrderableRows extends RequestHandler implements
public function augmentColumns($grid, &$cols) public function augmentColumns($grid, &$cols)
{ {
if (!in_array('Reorder', $cols) && $grid->getState()->GridFieldOrderableRows->enabled) { if (!in_array('Reorder', $cols ?? []) && $grid->getState()->GridFieldOrderableRows->enabled) {
array_splice($cols, $this->reorderColumnNumber, 0, 'Reorder'); array_splice($cols, $this->reorderColumnNumber ?? 0, 0, 'Reorder');
} }
} }
@ -308,7 +308,7 @@ class GridFieldOrderableRows extends RequestHandler implements
// if it exists, not directly from the record // if it exists, not directly from the record
$throughListSorts = $this->getSortValuesFromManyManyThroughList($list, $this->getSortField()); $throughListSorts = $this->getSortValuesFromManyManyThroughList($list, $this->getSortField());
if (array_key_exists($record->ID, $throughListSorts)) { if (array_key_exists($record->ID, $throughListSorts ?? [])) {
$currentSortValue = $throughListSorts[$record->ID]; $currentSortValue = $throughListSorts[$record->ID];
} }
} }
@ -408,8 +408,8 @@ class GridFieldOrderableRows extends RequestHandler implements
// Get records from the `GridFieldEditableColumns` column // Get records from the `GridFieldEditableColumns` column
$gridFieldName = $grid->getName(); $gridFieldName = $grid->getName();
if (strpos($gridFieldName, '.') !== false) { if (strpos($gridFieldName ?? '', '.') !== false) {
$gridFieldName = str_replace('.', '_', $gridFieldName); $gridFieldName = str_replace('.', '_', $gridFieldName ?? '');
} }
$data = $request->postVar($gridFieldName); $data = $request->postVar($gridFieldName);
@ -549,7 +549,7 @@ class GridFieldOrderableRows extends RequestHandler implements
$items = $list->filter('ID', $sortedIDs)->sort($sortterm); $items = $list->filter('ID', $sortedIDs)->sort($sortterm);
// Ensure that each provided ID corresponded to an actual object. // Ensure that each provided ID corresponded to an actual object.
if (count($items) != count($sortedIDs)) { if (count($items ?? []) != count($sortedIDs ?? [])) {
return false; return false;
} }
@ -720,7 +720,7 @@ class GridFieldOrderableRows extends RequestHandler implements
protected function getSortTableClauseForIds(DataList $list, $ids) protected function getSortTableClauseForIds(DataList $list, $ids)
{ {
if (is_array($ids)) { if (is_array($ids)) {
$value = 'IN (' . implode(', ', array_map('intval', $ids)) . ')'; $value = 'IN (' . implode(', ', array_map('intval', $ids ?? [])) . ')';
} else { } else {
$value = '= ' . (int) $ids; $value = '= ' . (int) $ids;
} }
@ -734,7 +734,7 @@ class GridFieldOrderableRows extends RequestHandler implements
$foreignKey = $this->getManyManyInspectorForeignKey($introspector); $foreignKey = $this->getManyManyInspectorForeignKey($introspector);
$foreignID = (int) $list->getForeignID(); $foreignID = (int) $list->getForeignID();
if ($extra && array_key_exists($this->getSortField(), $extra)) { if ($extra && array_key_exists($this->getSortField(), $extra ?? [])) {
return sprintf( return sprintf(
'"%s" %s AND "%s" = %d', '"%s" %s AND "%s" = %d',
$key, $key,

View File

@ -69,7 +69,7 @@ class GridFieldOrderableRowsTest extends SapphireTest
$config = new GridFieldConfig_RelationEditor(); $config = new GridFieldConfig_RelationEditor();
$config->addComponent($orderable); $config->addComponent($orderable);
list($parentClass, $parentInstanceID) = explode('.', $fixtureID); list($parentClass, $parentInstanceID) = explode('.', $fixtureID ?? '');
$parent = $this->objFromFixture($parentClass, $parentInstanceID); $parent = $this->objFromFixture($parentClass, $parentInstanceID);
$grid = new GridField( $grid = new GridField(
@ -83,7 +83,7 @@ class GridFieldOrderableRowsTest extends SapphireTest
$desiredOrder = []; $desiredOrder = [];
// Make order non-contiguous, and 1-based // 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; $desiredOrder[$index * 2 + 1] = $id;
} }
@ -183,7 +183,7 @@ class GridFieldOrderableRowsTest extends SapphireTest
); );
$originalOrder = $parent->Children()->column('ID'); $originalOrder = $parent->Children()->column('ID');
$desiredOrder = array_reverse($originalOrder); $desiredOrder = array_reverse($originalOrder ?? []);
$this->assertNotEquals($originalOrder, $desiredOrder); $this->assertNotEquals($originalOrder, $desiredOrder);
@ -270,7 +270,7 @@ class GridFieldOrderableRowsTest extends SapphireTest
$desiredOrder = []; $desiredOrder = [];
// Make order non-contiguous, and 1-based // 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; $desiredOrder[$index * 2 + 1] = $id;
} }

View File

@ -80,11 +80,11 @@ class OrderableRowsThroughVersionedTest extends SapphireTest
$originalOrder = $parent->$relationName()->sort($sortName)->column('ID'); $originalOrder = $parent->$relationName()->sort($sortName)->column('ID');
// Ring (un)shift by one, e.g. 3,2,1 becomes 1,3,2. // Ring (un)shift by one, e.g. 3,2,1 becomes 1,3,2.
// then string key our new order starting at 1 // then string key our new order starting at 1
$desiredOrder = array_values($originalOrder); $desiredOrder = array_values($originalOrder ?? []);
array_unshift($desiredOrder, array_pop($desiredOrder)); array_unshift($desiredOrder, array_pop($desiredOrder));
$desiredOrder = array_combine( $desiredOrder = array_combine(
range('1', count($desiredOrder)), range('1', count($desiredOrder ?? [])),
$desiredOrder $desiredOrder ?? []
); );
$this->assertNotEquals($originalOrder, $desiredOrder); $this->assertNotEquals($originalOrder, $desiredOrder);

View File

@ -9,7 +9,7 @@ class StubA implements TestOnly
public function i18n_singular_name() public function i18n_singular_name()
{ {
$class = get_class($this); $class = get_class($this);
return substr($class, -1); return substr($class ?? '', -1);
} }
public function canCreate() public function canCreate()