diff --git a/src/Extensions/GridFieldDetailFormItemRequestExtension.php b/src/Extensions/GridFieldDetailFormItemRequestExtension.php index 46af26a..8c5aeb1 100644 --- a/src/Extensions/GridFieldDetailFormItemRequestExtension.php +++ b/src/Extensions/GridFieldDetailFormItemRequestExtension.php @@ -47,7 +47,7 @@ class GridFieldDetailFormItemRequestExtension extends Extension $classes = $component->getClasses($grid); - if (!count($classes)) { + if (!count($classes ?? [])) { return false; } diff --git a/src/GridFieldAddNewInlineButton.php b/src/GridFieldAddNewInlineButton.php index f3a5c83..4dcb729 100755 --- a/src/GridFieldAddNewInlineButton.php +++ b/src/GridFieldAddNewInlineButton.php @@ -118,7 +118,7 @@ class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements 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()); @@ -129,7 +129,7 @@ class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements $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]', @@ -149,7 +149,7 @@ class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements $content = str_replace( sprintf('[%s][0]', GridFieldEditableColumns::POST_KEY), sprintf('[%s][{%%=o.num%%}]', self::POST_KEY), - $content + $content ?? '' ); } @@ -215,7 +215,7 @@ class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements } 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); diff --git a/src/GridFieldAddNewMultiClass.php b/src/GridFieldAddNewMultiClass.php index 065d330..2bf5ae7 100755 --- a/src/GridFieldAddNewMultiClass.php +++ b/src/GridFieldAddNewMultiClass.php @@ -124,7 +124,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements $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; @@ -135,7 +135,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements if (!is_string($class)) { $class = $title; } - if (!class_exists($class)) { + if (!class_exists($class ?? '')) { continue; } $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.'); } - if (!$class || !array_key_exists($class, $classes)) { + if (!$class || !array_key_exists($class, $classes ?? [])) { throw new HTTPResponse_Exception(400); } @@ -241,7 +241,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements { $classes = $this->getClasses($grid); - if (!count($classes)) { + if (!count($classes ?? [])) { return array(); } @@ -293,7 +293,7 @@ class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements */ 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) { - return str_replace('-', '\\', $class); + return str_replace('-', '\\', $class ?? ''); } } diff --git a/src/GridFieldAddNewMultiClassHandler.php b/src/GridFieldAddNewMultiClassHandler.php index f97bfdf..63234d1 100644 --- a/src/GridFieldAddNewMultiClassHandler.php +++ b/src/GridFieldAddNewMultiClassHandler.php @@ -31,6 +31,6 @@ class GridFieldAddNewMultiClassHandler extends GridFieldDetailForm_ItemRequest */ protected function sanitiseClassName($class) { - return str_replace('\\', '-', $class); + return str_replace('\\', '-', $class ?? ''); } } diff --git a/src/GridFieldConfigurablePaginator.php b/src/GridFieldConfigurablePaginator.php index 54e5c7d..f5872fd 100644 --- a/src/GridFieldConfigurablePaginator.php +++ b/src/GridFieldConfigurablePaginator.php @@ -171,7 +171,7 @@ class GridFieldConfigurablePaginator extends GridFieldPaginator $this->pageSizes = $pageSizes; // Reset items per page - $this->setItemsPerPage(current($pageSizes)); + $this->setItemsPerPage(current($pageSizes ?? [])); return $this; } @@ -356,7 +356,7 @@ class GridFieldConfigurablePaginator extends GridFieldPaginator return array( 'footer' => $forTemplate->renderWith( __CLASS__, - array('Colspan' => count($gridField->getColumns())) + array('Colspan' => count($gridField->getColumns() ?? [])) ) ); } diff --git a/src/GridFieldEditableColumns.php b/src/GridFieldEditableColumns.php index 5f48c5f..9be290a 100644 --- a/src/GridFieldEditableColumns.php +++ b/src/GridFieldEditableColumns.php @@ -61,7 +61,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements 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()) { @@ -82,7 +82,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements // 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); } @@ -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]); } @@ -157,7 +157,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements } 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); @@ -244,7 +244,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements 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(); } } @@ -259,7 +259,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements // 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 { diff --git a/src/GridFieldExternalLink.php b/src/GridFieldExternalLink.php index 75dcdd1..9996481 100644 --- a/src/GridFieldExternalLink.php +++ b/src/GridFieldExternalLink.php @@ -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'; } } diff --git a/src/GridFieldOrderableRows.php b/src/GridFieldOrderableRows.php index 70c2f66..c03b9bb 100755 --- a/src/GridFieldOrderableRows.php +++ b/src/GridFieldOrderableRows.php @@ -205,7 +205,7 @@ class GridFieldOrderableRows extends RequestHandler implements 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) { @@ -239,7 +239,7 @@ class GridFieldOrderableRows extends RequestHandler implements 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) { @@ -277,8 +277,8 @@ class GridFieldOrderableRows extends RequestHandler implements 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'); } } @@ -308,7 +308,7 @@ class GridFieldOrderableRows extends RequestHandler implements // 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]; } } @@ -408,8 +408,8 @@ class GridFieldOrderableRows extends RequestHandler implements // 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); @@ -549,7 +549,7 @@ class GridFieldOrderableRows extends RequestHandler implements $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; } @@ -720,7 +720,7 @@ class GridFieldOrderableRows extends RequestHandler implements 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; } @@ -734,7 +734,7 @@ class GridFieldOrderableRows extends RequestHandler implements $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, diff --git a/tests/GridFieldOrderableRowsTest.php b/tests/GridFieldOrderableRowsTest.php index 44e9f1b..008d86b 100644 --- a/tests/GridFieldOrderableRowsTest.php +++ b/tests/GridFieldOrderableRowsTest.php @@ -69,7 +69,7 @@ class GridFieldOrderableRowsTest extends SapphireTest $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( @@ -83,7 +83,7 @@ class GridFieldOrderableRowsTest extends SapphireTest $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; } @@ -183,7 +183,7 @@ class GridFieldOrderableRowsTest extends SapphireTest ); $originalOrder = $parent->Children()->column('ID'); - $desiredOrder = array_reverse($originalOrder); + $desiredOrder = array_reverse($originalOrder ?? []); $this->assertNotEquals($originalOrder, $desiredOrder); @@ -270,7 +270,7 @@ class GridFieldOrderableRowsTest extends SapphireTest $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; } diff --git a/tests/OrderableRowsThroughVersionedTest.php b/tests/OrderableRowsThroughVersionedTest.php index 473e033..a7190fc 100644 --- a/tests/OrderableRowsThroughVersionedTest.php +++ b/tests/OrderableRowsThroughVersionedTest.php @@ -80,11 +80,11 @@ class OrderableRowsThroughVersionedTest extends SapphireTest $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); diff --git a/tests/Stub/StubA.php b/tests/Stub/StubA.php index e6081ae..a284110 100644 --- a/tests/Stub/StubA.php +++ b/tests/Stub/StubA.php @@ -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()