Skip to content

Commit

Permalink
Merge pull request #371 from creative-commoners/pulls/4/php81
Browse files Browse the repository at this point in the history
ENH PHP 8.1 compatibility
  • Loading branch information
emteknetnz authored Apr 26, 2022
2 parents 03ded57 + 792d567 commit 84fc3e2
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 89 deletions.
6 changes: 3 additions & 3 deletions src/Controllers/QueuedJobsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function getEditForm($id = null, $fields = null)

if (QueuedJobDescriptor::singleton()->canCreate()) {
$types = ClassInfo::subclassesFor(AbstractQueuedJob::class);
$types = array_combine($types, $types);
$types = array_combine($types ?? [], $types ?? []);
foreach ($types as $class) {
$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable()) {
Expand Down Expand Up @@ -200,10 +200,10 @@ public function createjob($data, Form $form)
// If the user has select the European date format as their setting then replace '/' with '-' in the
// date string so PHP treats the date as this format.
if (Security::getCurrentUser()->DateFormat == self::$date_format_european) {
$time = str_replace('/', '-', $time);
$time = str_replace('/', '-', $time ?? '');
}

if ($jobType && class_exists($jobType) && is_subclass_of($jobType, QueuedJob::class)) {
if ($jobType && class_exists($jobType ?? '') && is_subclass_of($jobType, QueuedJob::class)) {
$jobClass = new ReflectionClass($jobType);
$job = $jobClass->newInstanceArgs($params);
if ($this->jobQueue->queueJob($job, $time)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Controllers/QueuedTaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public function index()

// universal tasks
foreach ($tasks as $task) {
if (in_array($task['class'], $blacklist)) {
if (in_array($task['class'], $blacklist ?? [])) {
$backlistedTasks[] = $task;

continue;
}

if (in_array($task['class'], $queuedOnlyList)) {
if (in_array($task['class'], $queuedOnlyList ?? [])) {
$queuedOnlyTasks[] = $task;

continue;
Expand Down Expand Up @@ -162,7 +162,7 @@ public function queueTask($request)
unset($variables['flush']);
unset($variables['flushtoken']);
unset($variables['isDev']);
$querystring = http_build_query($variables);
$querystring = http_build_query($variables ?? []);

$title = function ($content) {
printf(Director::is_cli() ? "%s\n\n" : '<h1>%s</h1>', $content);
Expand Down
20 changes: 10 additions & 10 deletions src/DataObjects/QueuedJobDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected function getJobDir()
$jobDir = TEMP_FOLDER . '/' . $jobDir;
}

if (!is_dir($jobDir)) {
if (!is_dir($jobDir ?? '')) {
Filesystem::makeFolder($jobDir);
}
return $jobDir;
Expand All @@ -264,8 +264,8 @@ public function cleanupJob()
{
// remove the job's temp file if it exists
$tmpFile = $this->getJobDir() . '/queuedjob-' . $this->ID;
if (file_exists($tmpFile)) {
unlink($tmpFile);
if (file_exists($tmpFile ?? '')) {
unlink($tmpFile ?? '');
}
}

Expand Down Expand Up @@ -294,8 +294,8 @@ public function onBeforeDelete()
*/
public function getMessages()
{
if (strlen($this->SavedJobMessages)) {
$messages = @unserialize($this->SavedJobMessages);
if (strlen($this->SavedJobMessages ?? '')) {
$messages = @unserialize($this->SavedJobMessages ?? '');
if (!empty($messages)) {
return DBField::create_field(
'HTMLText',
Expand All @@ -313,9 +313,9 @@ public function getMessages()
*/
public function getLastMessage()
{
if (strlen($this->SavedJobMessages)) {
$msgs = @unserialize($this->SavedJobMessages);
if (is_array($msgs) && sizeof($msgs)) {
if (strlen($this->SavedJobMessages ?? '')) {
$msgs = @unserialize($this->SavedJobMessages ?? '');
if (is_array($msgs) && sizeof($msgs ?? [])) {
return array_pop($msgs);
}
}
Expand Down Expand Up @@ -432,7 +432,7 @@ public function getCMSFields()
)
),
$jobTitle = TextField::create('JobTitle', 'Title'),
$status = DropdownField::create('JobStatus', 'Status', array_combine($statuses, $statuses)),
$status = DropdownField::create('JobStatus', 'Status', array_combine($statuses ?? [], $statuses ?? [])),
$jobType = DropdownField::create('JobType', 'Queue type', $this->getJobTypeValues()),
$runAs,
$startAfter = DatetimeField::create('StartAfter', 'Scheduled Start Time'),
Expand Down Expand Up @@ -547,7 +547,7 @@ public function getCMSFields()
)
);

if (strlen($this->SavedJobMessages)) {
if (strlen($this->SavedJobMessages ?? '')) {
$fields->addFieldToTab('Root.Messages', LiteralField::create('Messages', $this->getMessages()));
}

Expand Down
6 changes: 3 additions & 3 deletions src/Forms/GridFieldQueuedJobExecute.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct($action = 'execute', $check = null)
*/
public function augmentColumns($gridField, &$columns)
{
if (!in_array('Actions', $columns)) {
if (!in_array('Actions', $columns ?? [])) {
$columns[] = 'Actions';
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public function getColumnContent($gridField, $record, $columnName)
array('RecordID' => $record->ID)
);

$humanTitle = ucfirst($this->action);
$humanTitle = ucfirst($this->action ?? '');
$title = _t(__CLASS__ . '.' . $humanTitle, $humanTitle);

$field
Expand All @@ -163,7 +163,7 @@ public function getColumnContent($gridField, $record, $columnName)
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
$actions = $this->getActions(null);
if (in_array($actionName, $actions)) {
if (in_array($actionName, $actions ?? [])) {
$item = $gridField->getList()->byID($arguments['RecordID']);
if (!$item) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/CleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function process()
$this->reenqueue();
return;
}
$numJobs = count($staleJobs);
$numJobs = count($staleJobs ?? []);
$staleJobs = implode('\', \'', $staleJobs);
DB::query('DELETE FROM "QueuedJobDescriptor"
WHERE "ID"
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/DoormanQueuedJobTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function serialize()
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);
$data = unserialize($serialized ?? '');
$this->__unserialize($data);
}

Expand Down Expand Up @@ -233,7 +233,7 @@ public function isCancelled()
];

if ($this->descriptor) {
return in_array($this->descriptor->JobStatus, $cancelledStates, true);
return in_array($this->descriptor->JobStatus, $cancelledStates ?? [], true);
}

return true;
Expand Down
32 changes: 16 additions & 16 deletions src/Jobs/GenerateGoogleSitemapJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct()
{
$this->pagesToProcess = DB::query('SELECT ID FROM "SiteTree_Live" WHERE "ShowInSearch"=1')->column();
$this->currentStep = 0;
$this->totalSteps = count($this->pagesToProcess);
$this->totalSteps = count($this->pagesToProcess ?? []);
}

/**
Expand Down Expand Up @@ -81,9 +81,9 @@ public function setup()
Environment::increaseTimeLimitTo();

$restart = $this->currentStep == 0;
if (!$this->tempFile || !file_exists($this->tempFile)) {
$tmpfile = tempnam(TempFolder::getTempFolder(BASE_PATH), 'sitemap');
if (file_exists($tmpfile)) {
if (!$this->tempFile || !file_exists($this->tempFile ?? '')) {
$tmpfile = tempnam(TempFolder::getTempFolder(BASE_PATH) ?? '', 'sitemap');
if (file_exists($tmpfile ?? '')) {
$this->tempFile = $tmpfile;
}
$restart = true;
Expand All @@ -101,9 +101,9 @@ public function prepareForRestart()
{
parent::prepareForRestart();
// if the file we've been building is missing, lets fix it up
if (!$this->tempFile || !file_exists($this->tempFile)) {
$tmpfile = tempnam(TempFolder::getTempFolder(BASE_PATH), 'sitemap');
if (file_exists($tmpfile)) {
if (!$this->tempFile || !file_exists($this->tempFile ?? '')) {
$tmpfile = tempnam(TempFolder::getTempFolder(BASE_PATH) ?? '', 'sitemap');
if (file_exists($tmpfile ?? '')) {
$this->tempFile = $tmpfile;
}
$this->currentStep = 0;
Expand All @@ -117,14 +117,14 @@ public function process()
throw new Exception("Temporary sitemap file has not been set");
}

if (!file_exists($this->tempFile)) {
if (!file_exists($this->tempFile ?? '')) {
throw new Exception("Temporary file $this->tempFile has been deleted!");
}

$remainingChildren = $this->pagesToProcess;

// if there's no more, we're done!
if (!count($remainingChildren)) {
if (!count($remainingChildren ?? [])) {
$this->completeJob();
$this->isComplete = true;
return;
Expand Down Expand Up @@ -172,11 +172,11 @@ public function process()
// do the generation of the file in a temporary location
$content = $page->renderWith('SitemapEntry');

$fp = fopen($this->tempFile, "a");
$fp = fopen($this->tempFile ?? '', "a");
if (!$fp) {
throw new Exception("Could not open $this->tempFile for writing");
}
fputs($fp, $content, strlen($content));
fputs($fp, $content ?? '', strlen($content ?? ''));
fclose($fp);
}
}
Expand All @@ -185,7 +185,7 @@ public function process()
$this->pagesToProcess = $remainingChildren;
$this->currentStep++;

if (!count($remainingChildren)) {
if (!count($remainingChildren ?? [])) {
$this->completeJob();
$this->isComplete = true;
return;
Expand All @@ -199,15 +199,15 @@ protected function completeJob()
{
$content = '<?xml version="1.0" encoding="UTF-8"?>' .
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$content .= file_get_contents($this->tempFile);
$content .= file_get_contents($this->tempFile ?? '');
$content .= '</urlset>';

$sitemap = Director::baseFolder() . '/sitemap.xml';

file_put_contents($sitemap, $content);
file_put_contents($sitemap ?? '', $content);

if (file_exists($this->tempFile)) {
unlink($this->tempFile);
if (file_exists($this->tempFile ?? '')) {
unlink($this->tempFile ?? '');
}

$nextgeneration = Injector::inst()->create(GenerateGoogleSitemapJob::class);
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/PublishItemsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function process()
$remainingChildren = $this->remainingChildren;

// if there's no more, we're done!
if (!count($remainingChildren)) {
if (!count($remainingChildren ?? [])) {
$this->isComplete = true;
return;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public function process()
// and now we store the new list of remaining children
$this->remainingChildren = $remainingChildren;

if (!count($remainingChildren)) {
if (!count($remainingChildren ?? [])) {
$this->isComplete = true;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/RunBuildTaskJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function process()
}

$getVars = [];
parse_str($this->QueryString, $getVars);
parse_str($this->QueryString ?? '', $getVars);
$request = new HTTPRequest('GET', '/', $getVars);
$task->run($request);

Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/ScheduledExecutionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function process()
$timeStr = '+' . $executeInterval . ' ' . $object->ExecuteEvery;
}

$next = strtotime($timeStr);
$next = strtotime($timeStr ?? '');
if ($next > DBDatetime::now()->getTimestamp()) {
// in the future
$nextGen = DBDatetime::create()->setValue($next)->Rfc2822();
Expand Down
6 changes: 3 additions & 3 deletions src/QJUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function dbQuote($filter = array(), $join = " AND ")
// first break the field up into its two components
$operator = '';
if (is_string($field)) {
list($field, $operator) = explode(' ', trim($field));
list($field, $operator) = explode(' ', trim($field ?? ''));
}

$value = $this->recursiveQuote($value);

if (strpos($field, '.')) {
list($tb, $fl) = explode('.', $field);
if (strpos($field ?? '', '.')) {
list($tb, $fl) = explode('.', $field ?? '');
$string .= $sep . $quoteChar . $tb . $quoteChar . '.' . $quoteChar . $fl . $quoteChar
. " $operator " . $value;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/AbstractQueuedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private function loadCustomConfig()
*/
public function addMessage($message, $severity = 'INFO')
{
$severity = strtoupper($severity);
$severity = strtoupper($severity ?? '');
$this->messages[] = '[' . DBDatetime::now()->Rfc2822() . "][$severity] $message";
}

Expand Down
2 changes: 1 addition & 1 deletion src/Services/EmailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function createMissingDefaultJobReport(array $jobConfig, string $title):
{
$subject = sprintf('Default Job "%s" missing', $title);
$from = Config::inst()->get(Email::class, 'queued_job_admin_email');
$to = array_key_exists('email', $jobConfig) && $jobConfig['email']
$to = array_key_exists('email', $jobConfig ?? []) && $jobConfig['email']
? $jobConfig['email']
: $from;

Expand Down
2 changes: 1 addition & 1 deletion src/Services/GearmanQueueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public function startJobOnQueue(QueuedJobDescriptor $job)
*/
public function scheduleJob(QueuedJobDescriptor $job, $date)
{
$this->gearmanService->sendJob('scheduled', 'jobqueueExecute', array($job->ID), strtotime($date));
$this->gearmanService->sendJob('scheduled', 'jobqueueExecute', array($job->ID), strtotime($date ?? ''));
}
}
Loading

0 comments on commit 84fc3e2

Please sign in to comment.