Skip to content

Commit

Permalink
ENH PHP 8.1 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Apr 19, 2022
1 parent 3c296e3 commit 1ddc16f
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/Controllers/ElementSiteTreeFilterSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function applyDefaultFilters($query)

// Check whether the search term exists in the nested page content
$pageContent = $siteTree->getElementsForSearch();
return stripos($pageContent, $this->params['Term']) !== false;
return stripos($pageContent ?? '', $this->params['Term'] ?? '') !== false;
});

if ($siteTrees->count()) {
Expand Down
12 changes: 6 additions & 6 deletions src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getClientConfig()
'saveUrl' => $this->Link('api/saveForm'),
'saveMethod' => 'post',
'payloadFormat' => 'json',
'formNameTemplate' => sprintf(static::FORM_NAME_TEMPLATE, '{id}'),
'formNameTemplate' => sprintf(static::FORM_NAME_TEMPLATE ?? '', '{id}'),
];

// Configuration that is available per element type
Expand Down Expand Up @@ -93,7 +93,7 @@ public function getElementForm($elementID)
/** @var Form $form */
$form = $scaffolder->getForm(
$this,
sprintf(static::FORM_NAME_TEMPLATE, $elementID),
sprintf(static::FORM_NAME_TEMPLATE ?? '', $elementID),
['Record' => $element]
);

Expand Down Expand Up @@ -186,7 +186,7 @@ public function formAction(HTTPRequest $request)
$formName = $request->param('FormName');

// Get the element ID from the form name
$id = substr($formName, strlen(sprintf(self::FORM_NAME_TEMPLATE, '')));
$id = substr($formName ?? '', strlen(sprintf(self::FORM_NAME_TEMPLATE ?? '', '')));
$form = $this->getElementForm($id);

$field = $form->getRequestHandler()->handleField($request);
Expand All @@ -204,14 +204,14 @@ public function formAction(HTTPRequest $request)
public static function removeNamespacesFromFields(array $data, $elementID)
{
$output = [];
$template = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE, $elementID, '');
$template = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE ?? '', $elementID, '');
foreach ($data as $key => $value) {
// Only look at fields that match the namespace template
if (substr($key, 0, strlen($template)) !== $template) {
if (substr($key ?? '', 0, strlen($template ?? '')) !== $template) {
continue;
}

$fieldName = substr($key, strlen($template));
$fieldName = substr($key ?? '', strlen($template ?? ''));
$output[$fieldName] = $value;
}
return $output;
Expand Down
7 changes: 4 additions & 3 deletions src/Extensions/ElementalAreasExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ViewableData;

/**
* This extension handles most of the relationships between pages and element
Expand Down Expand Up @@ -114,7 +115,7 @@ public function getElementalTypes()
/** @var BaseElement $inst */
$inst = singleton($availableClass);

if (!in_array($availableClass, $disallowedElements) && $inst->canCreate()) {
if (!in_array($availableClass, $disallowedElements ?? []) && $inst->canCreate()) {
if ($inst->hasMethod('canCreateElement') && !$inst->canCreateElement()) {
continue;
}
Expand Down Expand Up @@ -260,7 +261,7 @@ public function supportsElemental()
return false;
} elseif ($ignored = Config::inst()->get(ElementalPageExtension::class, 'ignored_classes')) {
foreach ($ignored as $check) {
if (is_a($this->owner, $check)) {
if (is_a($this->owner, $check ?? '')) {
return false;
}
}
Expand Down Expand Up @@ -327,7 +328,7 @@ public function requireDefaultRecords()
}
}

$needsPublishing = Extensible::has_extension($elementalObject, Versioned::class)
$needsPublishing = ViewableData::has_extension($elementalObject, Versioned::class)
&& $elementalObject->isPublished();

/** @var ElementalAreasExtension $elementalObject */
Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/ElementalPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getElementsForSearch()
// CMS layout can break on the response. (SilverStripe 4.1.1)
SSViewer::set_themes($oldThemes);
}
return implode($this->owner->config()->get('search_index_element_delimiter'), $output);
return implode($this->owner->config()->get('search_index_element_delimiter') ?? '', $output);
}

public function MetaTags(&$tags)
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/EditFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function namespaceFields(FieldList $fields, array $context)
$elementID = $context['Record']->ID;

foreach ($fields->dataFields() as $field) {
$namespacedName = sprintf(self::FIELD_NAMESPACE_TEMPLATE, $elementID, $field->getName());
$namespacedName = sprintf(self::FIELD_NAMESPACE_TEMPLATE ?? '', $elementID, $field->getName());
$field->setName($namespacedName);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Forms/ElementalAreaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function FieldHolder($properties = array())
{
$context = $this;

if (count($properties)) {
if (count($properties ?? [])) {
$context = $this->customise($properties);
}

Expand All @@ -119,7 +119,7 @@ public function getSchemaDataDefaults()
$schemaData['elemental-area-id'] = $area ? (int) $area->ID : null;

$allowedTypes = $this->getTypes();
$schemaData['allowed-elements'] = array_keys($allowedTypes);
$schemaData['allowed-elements'] = array_keys($allowedTypes ?? []);

return $schemaData;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ public function performReadonlyTransformation()
$readOnlyField = $this->castedCopy(CompositeField::class);
$blockReducer = $this->getReadOnlyBlockReducer();
$readOnlyField->setChildren(
FieldList::create(array_map($blockReducer, $this->getArea()->Elements()->toArray()))
FieldList::create(array_map($blockReducer, $this->getArea()->Elements()->toArray() ?? []))
);

$readOnlyField = $readOnlyField->performReadonlyTransformation();
Expand All @@ -213,23 +213,23 @@ public function performReadonlyTransformation()
public function setSubmittedValue($value, $data = null)
{
// Content comes through as a JSON encoded list through a hidden field.
return $this->setValue(json_decode($value, true));
return $this->setValue(json_decode($value ?? '', true));
}

public function saveInto(DataObjectInterface $dataObject)
{
parent::saveInto($dataObject);

$elementData = $this->Value();
$idPrefixLength = strlen(sprintf(ElementalAreaController::FORM_NAME_TEMPLATE, ''));
$idPrefixLength = strlen(sprintf(ElementalAreaController::FORM_NAME_TEMPLATE ?? '', ''));

if (!$elementData) {
return;
}

foreach ($elementData as $form => $data) {
// Extract the ID
$elementId = (int) substr($form, $idPrefixLength);
$elementId = (int) substr($form ?? '', $idPrefixLength ?? 0);

/** @var BaseElement $element */
$element = $this->getArea()->Elements()->byID($elementId);
Expand Down
10 changes: 5 additions & 5 deletions src/GraphQL/Resolvers/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ public static function newTitle(string $title = ''): ?string
$parts = [];

// does $title end with 'copy' (ignoring numbers for now)?
if (preg_match($hasCopyPattern, $title, $parts)) {
if (preg_match($hasCopyPattern ?? '', $title ?? '', $parts)) {
$copy = $parts[1];
// does $title end with numbers?
if (preg_match($hasNumPattern, $copy, $parts)) {
$num = trim($parts[1]);
$len = strlen($num);
if (preg_match($hasNumPattern ?? '', $copy ?? '', $parts)) {
$num = trim($parts[1] ?? '');
$len = strlen($num ?? '');
$inc = (int)$num + 1;
return substr($title, 0, -$len) . "$inc";
return substr($title ?? '', 0, -$len) . "$inc";
} else {
return $title . ' 2';
}
Expand Down
18 changes: 9 additions & 9 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public function getCMSFields()

$styles = $this->config()->get('styles');

if ($styles && count($styles) > 0) {
if ($styles && count($styles ?? []) > 0) {
$styleDropdown = DropdownField::create('Style', _t(__CLASS__.'.STYLE', 'Style variation'), $styles);

$fields->insertBefore($styleDropdown, 'ExtraClass');
Expand Down Expand Up @@ -454,7 +454,7 @@ public function getController()

$controllerClass = self::config()->controller_class;

if (!class_exists($controllerClass)) {
if (!class_exists($controllerClass ?? '')) {
throw new Exception(
'Could not find controller class ' . $controllerClass . ' as defined in ' . static::class
);
Expand Down Expand Up @@ -506,7 +506,7 @@ public function getSearchIndexable(): bool
public function getContentForSearchIndex(): string
{
// Strips tags but be sure there's a space between words.
$content = trim(strip_tags(str_replace('<', ' <', $this->forTemplate())));
$content = trim(strip_tags(str_replace('<', ' <', $this->forTemplate() ?? '') ?? ''));
// Allow projects to update indexable content of third-party elements.
$this->extend('updateContentForSearchIndex', $content);
return $content;
Expand Down Expand Up @@ -539,7 +539,7 @@ public function getRenderTemplates($suffix = '')
{
$classes = ClassInfo::ancestry($this->ClassName);
$classes[static::class] = static::class;
$classes = array_reverse($classes);
$classes = array_reverse($classes ?? []);
$templates = [];

foreach ($classes as $key => $class) {
Expand Down Expand Up @@ -599,7 +599,7 @@ public function updateFromFormData($data)
*/
protected function stripNamespacing($classname)
{
$classParts = explode('\\', $classname);
$classParts = explode('\\', $classname ?? '');
return array_pop($classParts);
}

Expand All @@ -608,7 +608,7 @@ protected function stripNamespacing($classname)
*/
public function getSimpleClassName()
{
return strtolower($this->sanitiseClassName($this->ClassName, '__'));
return strtolower($this->sanitiseClassName($this->ClassName, '__') ?? '');
}

/**
Expand Down Expand Up @@ -833,12 +833,12 @@ public function getAreaRelationName()
*/
public function sanitiseClassName($class, $delimiter = '-')
{
return str_replace('\\', $delimiter, $class);
return str_replace('\\', $delimiter ?? '', $class ?? '');
}

public function unsanitiseClassName($class, $delimiter = '-')
{
return str_replace($delimiter, '\\', $class);
return str_replace($delimiter ?? '', '\\', $class ?? '');
}

/**
Expand Down Expand Up @@ -1041,7 +1041,7 @@ public function getStyleVariant()
$styles = $this->config()->get('styles');

if (isset($styles[$style])) {
$style = strtolower($style);
$style = strtolower($style ?? '');
} else {
$style = '';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Models/ElementalArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use DNADesign\Elemental\Extensions\ElementalAreasExtension;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\ArrayList;
Expand All @@ -14,6 +13,7 @@
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\UnsavedRelationList;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ViewableData;

/**
* Class ElementalArea
Expand Down Expand Up @@ -79,7 +79,7 @@ public function supportedPageTypes()
$elementalClasses = [];

foreach (ClassInfo::getValidSubClasses(DataObject::class) as $class) {
if (Extensible::has_extension($class, ElementalAreasExtension::class)) {
if (ViewableData::has_extension($class, ElementalAreasExtension::class)) {
$elementalClasses[] = $class;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Reports/ElementsInUseReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,6 @@ public function getBreadcrumbs()
*/
protected function unsanitiseClassName($class)
{
return str_replace('-', '\\', $class);
return str_replace('-', '\\', $class ?? '');
}
}
2 changes: 1 addition & 1 deletion src/Services/ElementTabProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ protected function generateTabsForElement($elementClass)
*/
protected function getCacheKey($className)
{
return 'Tabs.' . str_replace(['\\'], '-', $className);
return 'Tabs.' . str_replace(['\\'], '-', $className ?? '');
}
}
2 changes: 1 addition & 1 deletion src/Tasks/MigrateContentToElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function run($request)
protected function isMigratable($pageType)
{
$migratable = SiteTree::has_extension($pageType, ElementalPageExtension::class);
if (in_array($pageType, Config::inst()->get(ElementalPageExtension::class, 'ignored_classes'))) {
if (in_array($pageType, Config::inst()->get(ElementalPageExtension::class, 'ignored_classes') ?? [])) {
$migratable = false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/TopPage/DataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use DNADesign\Elemental\Models\ElementalArea;
use Page;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Extensible;
use SilverStripe\ORM\DataExtension as BaseDataExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Queries\SQLUpdate;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ViewableData;

/**
* Class DataExtension
Expand Down Expand Up @@ -96,7 +96,7 @@ public function getTopPage(): ?Page
{
$list = [$this->owner];

while (count($list) > 0) {
while (count($list ?? []) > 0) {
/** @var DataObject|DataExtension $item */
$item = array_shift($list);

Expand Down Expand Up @@ -380,7 +380,7 @@ protected function getTopPageTable(): string
// Find the first ancestor table which has the extension applied
// Note that this extension is expected to be subclassed
foreach ($classes as $class) {
if (!Extensible::has_extension($class, static::class)) {
if (!ViewableData::has_extension($class, static::class)) {
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions src/TopPage/SiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function addDuplicatedObject(DataObject $object): void
return;
}

if (array_key_exists($key, $this->duplicatedObjects)) {
if (array_key_exists($key, $this->duplicatedObjects ?? [])) {
array_unshift($this->duplicatedObjects[$key], $object);

return;
Expand All @@ -123,7 +123,7 @@ protected function getDuplicatedPageKey(): ?string
{
$pages = $this->duplicatedPages;

if (count($pages) === 0) {
if (count($pages ?? []) === 0) {
return null;
}

Expand All @@ -148,7 +148,7 @@ protected function initDuplication(Page $original): void
return;
}

if (in_array($key, $this->duplicatedPages)) {
if (in_array($key, $this->duplicatedPages ?? [])) {
// this should never happen as it would indicate a duplication loop
return;
}
Expand Down Expand Up @@ -228,7 +228,7 @@ protected function writeDuplication(Page $original): void
return;
}

if (array_key_exists($key, $this->duplicatedObjects)) {
if (array_key_exists($key, $this->duplicatedObjects ?? [])) {
$objects = $this->duplicatedObjects[$key];

/** @var DataObject|DataExtension $object */
Expand Down
2 changes: 1 addition & 1 deletion tests/Behat/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function stepIPressTheButtonInTheAddBlockPopover($text)
{
$popover = $this->getSession()->getPage()->find('css', '.popover-option-set');

$blockType = strtolower($text);
$blockType = strtolower($text ?? '');

// Selector preferable not font-icon, but other class shared among all buttons
$button = $popover->find('css', '.font-icon-block-'. $blockType);
Expand Down
2 changes: 1 addition & 1 deletion tests/Behat/Context/FixtureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function contentBlocksAreNotInLineEditable()

$file = 'content-blocks-not-inline-editable.yml';
$path = $this->getDestinationConfigFolder($file);
file_put_contents($path, $config);
file_put_contents($path ?? '', $config);

$this->activatedConfigFiles[] = $path;

Expand Down
Loading

0 comments on commit 1ddc16f

Please sign in to comment.