From 6f5b32da260904ab94faef4f97934de96a6f9e6f Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 13 Apr 2022 10:31:07 +1200 Subject: [PATCH] ENH PHP 8.1 compatibility --- code/BasicFieldsTestPage.php | 2 +- code/Employee.php | 4 +-- code/multiform/TestMultiForm.php | 2 +- code/tasks/FTFileMakerTask.php | 40 ++++++++++++++-------------- code/tasks/FTPageMakerTask.php | 16 +++++------ code/tasks/FTPageTypeCreatorTask.php | 4 +-- 6 files changed, 34 insertions(+), 34 deletions(-) diff --git a/code/BasicFieldsTestPage.php b/code/BasicFieldsTestPage.php index 5f05c6d..9413b95 100644 --- a/code/BasicFieldsTestPage.php +++ b/code/BasicFieldsTestPage.php @@ -305,7 +305,7 @@ public function getCMSFields() ->setRightTitle($rightTitle) ->addExtraClass('my-extra-class'); - if (in_array($field->getName(), $blacklist)) { + if (in_array($field->getName(), $blacklist ?? [])) { continue; } diff --git a/code/Employee.php b/code/Employee.php index 72d9e5c..0bae537 100644 --- a/code/Employee.php +++ b/code/Employee.php @@ -111,10 +111,10 @@ public function requireDefaultRecords() srand(5); $companyIDs = Company::get()->column(); - $companyCount = sizeof($companyIDs); + $companyCount = sizeof($companyIDs ?? []); $words = $this->words(); - $wordCount = sizeof($words); + $wordCount = sizeof($words ?? []); $categories = ['marketing', 'management', 'rnd', 'hr']; foreach ($this->data() as $employeeName) { diff --git a/code/multiform/TestMultiForm.php b/code/multiform/TestMultiForm.php index 6aa75ce..5d067ed 100644 --- a/code/multiform/TestMultiForm.php +++ b/code/multiform/TestMultiForm.php @@ -78,7 +78,7 @@ public function getFields() $fields->push(new LiteralField("Heading", "

You have submitted the following information:

")); foreach ($savedData as $key=>$value) { - if (preg_match("/_copy$/", $key)) { + if (preg_match("/_copy$/", $key ?? '')) { continue; } diff --git a/code/tasks/FTFileMakerTask.php b/code/tasks/FTFileMakerTask.php index 5b19a0b..51e32a3 100644 --- a/code/tasks/FTFileMakerTask.php +++ b/code/tasks/FTFileMakerTask.php @@ -223,20 +223,20 @@ public function run($request) $fileCounts = $request->getVar('fileCounts'); if ($fileCounts) { - $counts = explode(',', $fileCounts); + $counts = explode(',', $fileCounts ?? ''); $this->fileCounts = array_map(function ($int) { - return (int) trim($int); - }, $counts); + return (int) trim($int ?? ''); + }, $counts ?? []); } else { $this->fileCounts = self::config()->get('fileCountByDepth'); } $folderCounts = $request->getVar('folderCounts'); if ($folderCounts) { - $counts = explode(',', $folderCounts); + $counts = explode(',', $folderCounts ?? ''); $this->folderCounts = array_map(function ($int) { - return (int) trim($int); - }, $counts); + return (int) trim($int ?? ''); + }, $counts ?? []); } else { $this->folderCounts = self::config()->get('folderCountByDepth'); } @@ -277,8 +277,8 @@ protected function downloadFixtureFiles() $fixtureFileNames = $this->fixtureFileNames; if (self::config()->get('documentsOnly')) { - $fixtureFileNames = array_filter($fixtureFileNames, function($v) { - return (bool) preg_match('%\.(docx|xlsx|pdf)$%', $v); + $fixtureFileNames = array_filter($fixtureFileNames ?? [], function($v) { + return (bool) preg_match('%\.(docx|xlsx|pdf)$%', $v ?? ''); }); } @@ -289,7 +289,7 @@ protected function downloadFixtureFiles() $path = TEMP_FOLDER . '/' . $filename; $paths[$filename] = $path; $url = "{$this->fixtureFileBaseUrl}/{$filename}"; - if (!file_exists($path)) { + if (!file_exists($path ?? '')) { $promises[$filename] = $client->getAsync($filename, [ 'sink' => $path ]); @@ -373,20 +373,20 @@ protected function generateFiles($fixtureFilePaths, $depth = 0, $prefix = "0", $ $randomFileName = array_keys($fixtureFilePaths)[rand(0, count($fixtureFilePaths) - 1)]; $randomFilePath = $fixtureFilePaths[$randomFileName]; - $fileName = pathinfo($randomFilePath, PATHINFO_FILENAME) + $fileName = pathinfo($randomFilePath ?? '', PATHINFO_FILENAME) . "-{$prefix}-{$j}" . "." - . pathinfo($randomFilePath, PATHINFO_EXTENSION); + . pathinfo($randomFilePath ?? '', PATHINFO_EXTENSION); // Add a random prefix to avoid all types of files showing up on a single screen page - $fileName = substr(md5($fileName), 0, 5) . '-' . $fileName; + $fileName = substr(md5($fileName ?? ''), 0, 5) . '-' . $fileName; $class = $this->fixtureFileTypes[$randomFileName]; // If we're uniquifying images, copy the path and watermark it. if ($class === Image::class && $uniqueImages) { - $copyPath = Path::join(dirname($randomFilePath), $fileName); - copy($randomFilePath, $copyPath); + $copyPath = Path::join(dirname($randomFilePath ?? ''), $fileName); + copy($randomFilePath ?? '', $copyPath ?? ''); $newPath = $this->watermarkImage($absWatermarkPath, $copyPath); if ($newPath) { $randomFilePath = $newPath; @@ -457,7 +457,7 @@ protected function putProtectedFilesInPublicAssetStore() protected function watermarkImage(string $stampPath, string $targetPath): ?string { // Load the stamp and the photo to apply the watermark to - $ext = strtolower(pathinfo($targetPath, PATHINFO_EXTENSION)); + $ext = strtolower(pathinfo($targetPath ?? '', PATHINFO_EXTENSION) ?? ''); $functions = null; if (in_array($ext, ['jpeg', 'jpg'])) { $functions = ['imagecreatefromjpeg', 'imagejpeg']; @@ -468,7 +468,7 @@ protected function watermarkImage(string $stampPath, string $targetPath): ?strin return null; } - $stamp = imagecreatefrompng($stampPath); + $stamp = imagecreatefrompng($stampPath ?? ''); $targetImage = call_user_func($functions[0], $targetPath); // Set the margins for the stamp and get the height/width of the stamp image @@ -477,8 +477,8 @@ protected function watermarkImage(string $stampPath, string $targetPath): ?strin $stampX = imagesx($stamp); $stampY = imagesy($stamp); - $marge_right = rand($stampX, $targetX - $stampX); - $marge_bottom = rand($stampY, $targetY - $stampY); + $marge_right = rand($stampX ?? 0, $targetX - $stampX); + $marge_bottom = rand($stampY ?? 0, $targetY - $stampY); // Copy the stamp image onto our photo using the margin offsets and the photo // width to calculate positioning of the stamp. @@ -489,8 +489,8 @@ protected function watermarkImage(string $stampPath, string $targetPath): ?strin $targetY - $stampY - $marge_bottom, 0, 0, - $stampX, - $stampY + $stampX ?? 0, + $stampY ?? 0 ); call_user_func($functions[1], $targetImage, $targetPath); diff --git a/code/tasks/FTPageMakerTask.php b/code/tasks/FTPageMakerTask.php index f7f93df..fa2eb2a 100644 --- a/code/tasks/FTPageMakerTask.php +++ b/code/tasks/FTPageMakerTask.php @@ -59,10 +59,10 @@ public function run($request) // Allow pageCountByDepth to be passed as comma-separated value, e.g. pageCounts=5,100,1,1 $pageCounts = $request->getVar('pageCounts'); if ($pageCounts) { - $counts = explode(',', $pageCounts); + $counts = explode(',', $pageCounts ?? ''); $this->pageCountByDepth = array_map(function ($int) { - return (int) trim($int); - }, $counts); + return (int) trim($int ?? ''); + }, $counts ?? []); } $this->generatePages(0, "", 0, $withBlocks); @@ -70,13 +70,13 @@ public function run($request) protected function generatePages($depth = 0, $prefix = "", $parentID = 0, $withBlocks = false) { - $maxDepth = count($this->pageCountByDepth); + $maxDepth = count($this->pageCountByDepth ?? []); $pageCount = $this->pageCountByDepth[$depth]; $testPageClasses = ClassInfo::implementorsOf('TestPageInterface'); for ($i=1; $i<=$pageCount; $i++) { $fullPrefix = $prefix ? "{$prefix}-{$i}" : $i; - $randomIndex = array_rand($testPageClasses); + $randomIndex = array_rand($testPageClasses ?? []); $pageClass = $testPageClasses[$randomIndex]; $page = new $pageClass(); $page->ParentID = $parentID; @@ -102,14 +102,14 @@ protected function generatePages($depth = 0, $prefix = "", $parentID = 0, $withB protected function generateBlocksForPage(Page $page) { - $classes = array_filter($this->config()->get('block_generators'), function ($callable, $class) { - return class_exists($class); + $classes = array_filter($this->config()->get('block_generators') ?? [], function ($callable, $class) { + return class_exists($class ?? ''); }, ARRAY_FILTER_USE_BOTH); // Generate a random amount of blocks in the preferred range $range = $this->blockCountRange; foreach(range($range[0], array_rand(range($range[0], $range[1]))) as $i) { - $class = array_rand($classes); + $class = array_rand($classes ?? []); $callable = $classes[$class]; $block = call_user_func($callable, $page); diff --git a/code/tasks/FTPageTypeCreatorTask.php b/code/tasks/FTPageTypeCreatorTask.php index e1815e7..541b822 100644 --- a/code/tasks/FTPageTypeCreatorTask.php +++ b/code/tasks/FTPageTypeCreatorTask.php @@ -54,8 +54,8 @@ public function run($request) $className = null; while ( !$className || - in_array($className, $pageTypes) || - class_exists(basename($className, 'php')) + in_array($className, $pageTypes ?? []) || + class_exists(basename($className ?? '', 'php')) ) { $className = $this->generateClassName(); }