ENH PHP 8.1 compatibility

This commit is contained in:
Steve Boyd 2022-04-13 13:54:56 +12:00
parent 5aca443a7c
commit 51d29b3d5a
3 changed files with 11 additions and 11 deletions

View File

@ -66,7 +66,7 @@ class WidgetAreaEditor extends FormField
$available = Config::inst()->get($class, 'only_available_in'); $available = Config::inst()->get($class, 'only_available_in');
if (!empty($available) && is_array($available)) { if (!empty($available) && is_array($available)) {
if (in_array($this->Name, $available)) { if (in_array($this->Name, $available ?? [])) {
$widgets->push(singleton($class)); $widgets->push(singleton($class));
} }
} else { } else {
@ -168,12 +168,12 @@ class WidgetAreaEditor extends FormField
if (empty($newWidgetData['Type'])) { if (empty($newWidgetData['Type'])) {
$newWidgetData['Type'] = ''; $newWidgetData['Type'] = '';
} }
$newWidgetData['Type'] = str_replace('_', '\\', $newWidgetData['Type']); $newWidgetData['Type'] = str_replace('_', '\\', $newWidgetData['Type'] ?? '');
// create a new object // create a new object
if (!$widget if (!$widget
&& !empty($newWidgetData['Type']) && !empty($newWidgetData['Type'])
&& class_exists($newWidgetData['Type']) && class_exists($newWidgetData['Type'] ?? '')
&& is_subclass_of($newWidgetData['Type'], Widget::class) && is_subclass_of($newWidgetData['Type'], Widget::class)
) { ) {
$widget = Injector::inst()->create($newWidgetData['Type']); $widget = Injector::inst()->create($newWidgetData['Type']);

View File

@ -191,7 +191,7 @@ class Widget extends DataObject
if ($value) { if ($value) {
$field->setValue($value); $field->setValue($value);
} }
$namefiltered = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->FormID . "][\\1]", $name); $namefiltered = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->FormID . "][\\1]", $name ?? '');
$field->setName($namefiltered); $field->setName($namefiltered);
$outputFields->push($field); $outputFields->push($field);
@ -230,14 +230,14 @@ class Widget extends DataObject
return $this->controller; return $this->controller;
} }
foreach (array_reverse(ClassInfo::ancestry(get_class($this))) as $widgetClass) { foreach (array_reverse(ClassInfo::ancestry(get_class($this)) ?? []) as $widgetClass) {
$controllerClass = "{$widgetClass}Controller"; $controllerClass = "{$widgetClass}Controller";
if (class_exists($controllerClass)) { if (class_exists($controllerClass ?? '')) {
break; break;
} }
} }
if (!class_exists($controllerClass)) { if (!class_exists($controllerClass ?? '')) {
throw new Exception('Could not find controller class for ' . static::class); throw new Exception('Could not find controller class for ' . static::class);
} }
@ -268,7 +268,7 @@ class Widget extends DataObject
//Look for checkbox fields not present in the data //Look for checkbox fields not present in the data
foreach ($fields as $field) { foreach ($fields as $field) {
if ($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) { if ($field instanceof CheckboxField && !array_key_exists($field->getName(), $data ?? [])) {
$field->setValue(false); $field->setValue(false);
$field->saveInto($this); $field->saveInto($this);
} }

View File

@ -106,7 +106,7 @@ class WidgetController extends Controller
*/ */
public function Content() public function Content()
{ {
return $this->renderWith(array_reverse(ClassInfo::ancestry(get_class($this->widget)))); return $this->renderWith(array_reverse(ClassInfo::ancestry(get_class($this->widget)) ?? []));
} }
/** /**
@ -136,8 +136,8 @@ class WidgetController extends Controller
$leftandmain->doInit(); $leftandmain->doInit();
// Decode if fully qualified - @see Widget::ClassName // Decode if fully qualified - @see Widget::ClassName
$className = str_replace('_', '\\', $this->urlParams['ID']); $className = str_replace('_', '\\', $this->urlParams['ID'] ?? '');
if (class_exists($className) && is_subclass_of($className, Widget::class)) { if (class_exists($className ?? '') && is_subclass_of($className, Widget::class)) {
$obj = new $className(); $obj = new $className();
return $obj->EditableSegment(); return $obj->EditableSegment();
} else { } else {