Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH PHP 8.1 compatibility #266

Merged
merged 1 commit into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Forms/ResourceLocatorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function setValue($value, $data = null)

public function setSubmittedValue($value, $data = null)
{
return $this->setValue(json_decode($value, true));
return $this->setValue(json_decode($value ?? '', true));
}

public function dataValue()
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/ResultConditionsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function Type()
public function performReadonlyTransformation()
{
// If we have no value then defer to the parent that renders a "none" field
$value = trim($this->Value());
$value = trim($this->Value() ?? '');
if (empty($value)) {
return parent::performReadonlyTransformation();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/ResourceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getCMSFields()

// See https://github.com/silverstripe/silverstripe-framework/issues/8696
foreach ([$summary, $detail, $readableLabel, $originalLabel] as $field) {
$field->setTitle(ucfirst(strtolower($field->Title())));
$field->setTitle(ucfirst(strtolower($field->Title() ?? '')));
}
});
return parent::getCMSFields();
Expand Down
4 changes: 2 additions & 2 deletions src/Model/ResourceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getCMSFields()
// See https://github.com/silverstripe/silverstripe-framework/issues/8696
foreach (['AllColumns', 'FilterLabel'] as $fieldName) {
$field = $fields->dataFieldByName($fieldName);
$field->setTitle(ucfirst(strtolower($field->Title())));
$field->setTitle(ucfirst(strtolower($field->Title() ?? '')));
}
});

Expand Down Expand Up @@ -146,7 +146,7 @@ public function getClientConfig()
'label' => $field->ReadableLabel,
'target' => $field->OriginalLabel,
];
}, $this->FilterFields()->toArray()),
}, $this->FilterFields()->toArray() ?? []),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/ResourceFilter/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getClientConfig()
*/
protected function getConfiguredOptions()
{
$spec = json_decode($this->Options, true) ?: [];
$spec = json_decode($this->Options ?? '', true) ?: [];

if (!isset($spec['selectType'])) {
return [];
Expand Down
4 changes: 2 additions & 2 deletions src/Page/CKANRegistryPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public function getCMSFields()
$columns['Position'] = _t(__CLASS__ . '.POS', 'Pos.', 'Abbreviated version of position');

$editable = array_flip(['ShowInResultsView', 'ShowInDetailView']);
$component->setDisplayFields(array_diff_key($columns, $editable));
$component->setDisplayFields(array_diff_key($columns ?? [], $editable));
// set this way so that title translations are preserved
$editableColumns->setDisplayFields(array_intersect_key($columns, $editable));
$editableColumns->setDisplayFields(array_intersect_key($columns ?? [], $editable));
}
}
$columnsConfig->addComponent($editableColumns, $before);
Expand Down
8 changes: 4 additions & 4 deletions src/Page/CKANRegistryPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function (ResourceField $field) {
'ReadableLabel' => $field->ReadableLabel,
'ShowInResultsView' => (bool) $field->ShowInResultsView,
'ShowInDetailView' => (bool) $field->ShowInDetailView,
'DisplayConditions' => json_decode($field->DisplayConditions, true),
'DisplayConditions' => json_decode($field->DisplayConditions ?? '', true),
'RemoveDuplicates' => (bool) $field->RemoveDuplicates,
'Type' => $field->Type,
];
Expand All @@ -77,7 +77,7 @@ function (ResourceField $field) {
'ShowInResultsView' => true,
'ShowInDetailView' => true,
'RemoveDuplicates' => true,
])->Sort('Position', 'ASC')->toArray()
])->Sort('Position', 'ASC')->toArray() ?? []
),
'filters' => array_map(
function (ResourceFilter $filter) {
Expand All @@ -86,7 +86,7 @@ function (ResourceFilter $filter) {
'type' => array_pop($explodedClassName),
] + $filter->getClientConfig();
},
$resource->Filters()->sort('Order')->toArray()
$resource->Filters()->sort('Order')->toArray() ?? []
)
];

Expand Down Expand Up @@ -121,6 +121,6 @@ public function getBasePath(DataObject $holder = null)
}

$link = $holder->RelativeLink();
return Director::baseURL() . trim($link, '/');
return Director::baseURL() . trim($link ?? '', '/');
}
}
6 changes: 3 additions & 3 deletions src/Service/APIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getData(Resource $resource, $action = 'datastore_search', $id =
{
$endpoint = sprintf(
'%s/api/%s/action/%s?id=%s',
trim($resource->Endpoint, '/'),
trim($resource->Endpoint ?? '', '/'),
APIClientInterface::API_VERSION,
$action,
$resource->{$id}
Expand All @@ -63,11 +63,11 @@ public function getData(Resource $resource, $action = 'datastore_search', $id =
throw new RuntimeException('CKAN API is not available. Error code ' . $statusCode);
}

if (!count(preg_grep('#application/json#', $response->getHeader('Content-Type')))) {
if (!count(preg_grep('#application/json#', $response->getHeader('Content-Type') ?? []) ?? [])) {
throw new RuntimeException('CKAN API returns an invalid response: Content-Type is not JSON');
}

$responseBody = json_decode($response->getBody()->getContents(), true);
$responseBody = json_decode($response->getBody()->getContents() ?? '', true);

if (!$responseBody || !isset($responseBody['success']) || !$responseBody['success']) {
throw new RuntimeException('CKAN API returns an invalid response: Responded as invalid');
Expand Down
10 changes: 5 additions & 5 deletions src/Service/ResourcePopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ protected function validateResource(Resource $resource)
protected function parseName($id)
{
// Parse "camelCase" to "space case"
$name = strtolower(preg_replace('/(?<=[a-z\d])([A-Z])/', ' \1', $id));
$name = strtolower(preg_replace('/(?<=[a-z\d])([A-Z])/', ' \1', $id ?? '') ?? '');

// Swap out certain characters with spaces
$name = str_replace(['_', '-'], ' ', $name);
$name = str_replace(['_', '-'], ' ', $name ?? '');

// Remove some non-alphanumeric characters
$name = trim(preg_replace('/[^\/\w\s]/', '', $name));
$name = trim(preg_replace('/[^\/\w\s]/', '', $name ?? '') ?? '');

// Remove extra spaces around slashes
$name = str_replace(' / ', '/', $name);
$name = str_replace(' / ', '/', $name ?? '');

return ucfirst($name);
return ucfirst($name ?? '');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected function assertArrayEqualsInOrder($expected, $actual)
'. Actual: ' . print_r($actual, true);

foreach ($actual as $key => $value) {
$expectedKey = key($expected);
$expectedKey = key($expected ?? []);
$expectedValue = array_shift($expected);
$this->assertSame($expectedKey, $key, $message);
$this->assertEquals($expectedValue, $value, $message);
Expand Down