Navigation Menu

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 12, 2022
1 parent f292a27 commit d362535
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/Forms/GridFieldQueuedExportButton.php
Expand Up @@ -195,7 +195,7 @@ public function checkExport($gridField, $request = null)
]);

if ($job->JobStatus == QueuedJob::STATUS_COMPLETE) {
if (file_exists($this->getExportPath($id))) {
if (file_exists($this->getExportPath($id) ?? '')) {
$data->DownloadLink = $gridField->Link('/export_download/' . $job->Signature);
} else {
$data->ErrorMessage = _t(
Expand Down Expand Up @@ -253,10 +253,10 @@ public function downloadExport($gridField, $request = null)
$servedName = "export-$now.csv";

$path = $this->getExportPath($id);
$content = file_get_contents($path);
$content = file_get_contents($path ?? '');

unlink($path);
rmdir(dirname($path));
unlink($path ?? '');
rmdir(dirname($path ?? ''));

$response = HTTPRequest::send_file($content, $servedName, 'text/csv');
$response->addHeader('Set-Cookie', 'downloaded_' . $id . '=true; Path=/');
Expand Down
24 changes: 12 additions & 12 deletions src/Jobs/GenerateCSVJob.php
Expand Up @@ -139,11 +139,11 @@ public function setSession($session)
{
// None of the gridfield actions are needed, and they make the stored session bigger, so pull
// them out.
$actionkeys = array_filter(array_keys($session), function ($i) {
return strpos($i, 'gf_') === 0;
$actionkeys = array_filter(array_keys($session ?? []), function ($i) {
return strpos($i ?? '', 'gf_') === 0;
});

$session = array_diff_key($session, array_flip($actionkeys));
$session = array_diff_key($session ?? [], array_flip($actionkeys ?? []));

// This causes problems with logins
unset($session['HTTP_USER_AGENT']);
Expand Down Expand Up @@ -176,7 +176,7 @@ public function setIncludeHeader($includeHeader)

protected function makeDir($path)
{
if (!is_dir($path)) {
if (!is_dir($path ?? '')) {
// whether to use 'chmod' to override 'mkdir' perms which obey umask
$ignore_umask = $this->config()->get('ignore_umask');

Expand All @@ -189,14 +189,14 @@ protected function makeDir($path)
}

// convert from octal to decimal for mkdir
$permission_mode = octdec($permission_mode);
$permission_mode = octdec($permission_mode ?? '');

// make dir with perms that obey the executing user's umask
mkdir($path, $permission_mode, true);
mkdir($path ?? '', $permission_mode ?? 0, true);

// override perms to ignore user's umask?
if ($ignore_umask) {
chmod($path, $permission_mode);
chmod($path ?? '', $permission_mode ?? 0);
}
}
}
Expand Down Expand Up @@ -234,7 +234,7 @@ protected function getCSVWriter()
$csvWriter->addFormatter(function (array $row) {
foreach ($row as &$item) {
// [SS-2017-007] Sanitise XLS executable column values with a leading tab
if (preg_match('/^[-@=+].*/', $item)) {
if (preg_match('/^[-@=+].*/', $item ?? '')) {
$item = "\t" . $item;
}
}
Expand Down Expand Up @@ -284,15 +284,15 @@ protected function getGridField()
$queryParams = [$actionKey => $actionValue, 'SecurityID' => $token];

// Get the filters and assign to the url as a get parameter
if (is_array($this->Filters) && count($this->Filters) > 0) {
if (is_array($this->Filters) && count($this->Filters ?? []) > 0) {
foreach ($this->Filters as $filter => $value) {
$queryParams['filters'][$filter] = $value;
}
}

$url = Controller::join_links(
$this->GridFieldURL,
'?' . http_build_query($queryParams)
'?' . http_build_query($queryParams ?? [])
);

// Restore into the current session the user the job is exporting as
Expand Down Expand Up @@ -330,7 +330,7 @@ protected function outputHeader($gridField, $columns)
// determine the CSV headers. If a field is callable (e.g. anonymous function) then use the
// source name as the header instead
foreach ($columns as $columnSource => $columnHeader) {
if (is_array($columnHeader) && array_key_exists('title', $columnHeader)) {
if (is_array($columnHeader) && array_key_exists('title', $columnHeader ?? [])) {
$headers[] = $columnHeader['title'];
} else {
$headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader;
Expand Down Expand Up @@ -447,7 +447,7 @@ public function process()
// Check to see if we need to wait for some time for asset synchronisation to complete
$sleepTime = (int) $this->config()->get('sync_sleep_seconds');
if ($sleepTime > 0) {
sleep($sleepTime);
sleep($sleepTime ?? 0);
}

$this->isComplete = true;
Expand Down
10 changes: 5 additions & 5 deletions tests/GenerateCSVJobTest.php
Expand Up @@ -35,7 +35,7 @@ protected function setUp(): void
protected function tearDown(): void
{
foreach ($this->paths as $path) {
Filesystem::removeFolder(dirname($path));
Filesystem::removeFolder(dirname($path ?? ''));
}
parent::tearDown();
}
Expand Down Expand Up @@ -70,9 +70,9 @@ public function testGenerateExport()
'"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"',
'',
];
$actual = file_get_contents($path);
$actual = file_get_contents($path ?? '');
// Note: strtolower() is for case insensitive comparison, since field label casing changed in SS 4.3
$this->assertStringContainsString('title,content,"publish on"', strtolower($actual));
$this->assertStringContainsString('title,content,"publish on"', strtolower($actual ?? ''));
$this->assertStringContainsString(implode("\r\n", $expected), $actual);
}

Expand Down Expand Up @@ -118,9 +118,9 @@ public function testGenerateExportOverMultipleSteps()
'"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"',
'',
];
$actual = file_get_contents($path);
$actual = file_get_contents($path ?? '');
// Note: strtolower() is for case insensitive comparison, since field label casing changed in SS 4.3
$this->assertStringContainsString('title,content,"publish on"', strtolower($actual));
$this->assertStringContainsString('title,content,"publish on"', strtolower($actual ?? ''));
$this->assertStringContainsString(implode("\r\n", $expected), $actual);
}

Expand Down

0 comments on commit d362535

Please sign in to comment.