Merge pull request #102 from creative-commoners/pulls/4/php81

ENH PHP 8.1 compatibility
This commit is contained in:
Guy Sartorelli 2022-04-26 17:58:16 +12:00 committed by GitHub
commit 622864dc3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 34 deletions

View File

@ -305,7 +305,7 @@ class BasicFieldsTestPage extends TestPage
->setRightTitle($rightTitle) ->setRightTitle($rightTitle)
->addExtraClass('my-extra-class'); ->addExtraClass('my-extra-class');
if (in_array($field->getName(), $blacklist)) { if (in_array($field->getName(), $blacklist ?? [])) {
continue; continue;
} }

View File

@ -111,10 +111,10 @@ class Employee extends DataObject
srand(5); srand(5);
$companyIDs = Company::get()->column(); $companyIDs = Company::get()->column();
$companyCount = sizeof($companyIDs); $companyCount = sizeof($companyIDs ?? []);
$words = $this->words(); $words = $this->words();
$wordCount = sizeof($words); $wordCount = sizeof($words ?? []);
$categories = ['marketing', 'management', 'rnd', 'hr']; $categories = ['marketing', 'management', 'rnd', 'hr'];
foreach ($this->data() as $employeeName) { foreach ($this->data() as $employeeName) {

View File

@ -78,7 +78,7 @@ class TestMultiFormStepThree extends MultiFormStep
$fields->push(new LiteralField("Heading", "<h3>You have submitted the following information:</h3>")); $fields->push(new LiteralField("Heading", "<h3>You have submitted the following information:</h3>"));
foreach ($savedData as $key=>$value) { foreach ($savedData as $key=>$value) {
if (preg_match("/_copy$/", $key)) { if (preg_match("/_copy$/", $key ?? '')) {
continue; continue;
} }

View File

@ -223,20 +223,20 @@ class FTFileMakerTask extends BuildTask
$fileCounts = $request->getVar('fileCounts'); $fileCounts = $request->getVar('fileCounts');
if ($fileCounts) { if ($fileCounts) {
$counts = explode(',', $fileCounts); $counts = explode(',', $fileCounts ?? '');
$this->fileCounts = array_map(function ($int) { $this->fileCounts = array_map(function ($int) {
return (int) trim($int); return (int) trim($int ?? '');
}, $counts); }, $counts ?? []);
} else { } else {
$this->fileCounts = self::config()->get('fileCountByDepth'); $this->fileCounts = self::config()->get('fileCountByDepth');
} }
$folderCounts = $request->getVar('folderCounts'); $folderCounts = $request->getVar('folderCounts');
if ($folderCounts) { if ($folderCounts) {
$counts = explode(',', $folderCounts); $counts = explode(',', $folderCounts ?? '');
$this->folderCounts = array_map(function ($int) { $this->folderCounts = array_map(function ($int) {
return (int) trim($int); return (int) trim($int ?? '');
}, $counts); }, $counts ?? []);
} else { } else {
$this->folderCounts = self::config()->get('folderCountByDepth'); $this->folderCounts = self::config()->get('folderCountByDepth');
} }
@ -277,8 +277,8 @@ class FTFileMakerTask extends BuildTask
$fixtureFileNames = $this->fixtureFileNames; $fixtureFileNames = $this->fixtureFileNames;
if (self::config()->get('documentsOnly')) { if (self::config()->get('documentsOnly')) {
$fixtureFileNames = array_filter($fixtureFileNames, function($v) { $fixtureFileNames = array_filter($fixtureFileNames ?? [], function($v) {
return (bool) preg_match('%\.(docx|xlsx|pdf)$%', $v); return (bool) preg_match('%\.(docx|xlsx|pdf)$%', $v ?? '');
}); });
} }
@ -289,7 +289,7 @@ class FTFileMakerTask extends BuildTask
$path = TEMP_FOLDER . '/' . $filename; $path = TEMP_FOLDER . '/' . $filename;
$paths[$filename] = $path; $paths[$filename] = $path;
$url = "{$this->fixtureFileBaseUrl}/{$filename}"; $url = "{$this->fixtureFileBaseUrl}/{$filename}";
if (!file_exists($path)) { if (!file_exists($path ?? '')) {
$promises[$filename] = $client->getAsync($filename, [ $promises[$filename] = $client->getAsync($filename, [
'sink' => $path 'sink' => $path
]); ]);
@ -373,20 +373,20 @@ class FTFileMakerTask extends BuildTask
$randomFileName = array_keys($fixtureFilePaths)[rand(0, count($fixtureFilePaths) - 1)]; $randomFileName = array_keys($fixtureFilePaths)[rand(0, count($fixtureFilePaths) - 1)];
$randomFilePath = $fixtureFilePaths[$randomFileName]; $randomFilePath = $fixtureFilePaths[$randomFileName];
$fileName = pathinfo($randomFilePath, PATHINFO_FILENAME) $fileName = pathinfo($randomFilePath ?? '', PATHINFO_FILENAME)
. "-{$prefix}-{$j}" . "-{$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 // 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]; $class = $this->fixtureFileTypes[$randomFileName];
// If we're uniquifying images, copy the path and watermark it. // If we're uniquifying images, copy the path and watermark it.
if ($class === Image::class && $uniqueImages) { if ($class === Image::class && $uniqueImages) {
$copyPath = Path::join(dirname($randomFilePath), $fileName); $copyPath = Path::join(dirname($randomFilePath ?? ''), $fileName);
copy($randomFilePath, $copyPath); copy($randomFilePath ?? '', $copyPath ?? '');
$newPath = $this->watermarkImage($absWatermarkPath, $copyPath); $newPath = $this->watermarkImage($absWatermarkPath, $copyPath);
if ($newPath) { if ($newPath) {
$randomFilePath = $newPath; $randomFilePath = $newPath;
@ -457,7 +457,7 @@ class FTFileMakerTask extends BuildTask
protected function watermarkImage(string $stampPath, string $targetPath): ?string protected function watermarkImage(string $stampPath, string $targetPath): ?string
{ {
// Load the stamp and the photo to apply the watermark to // 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; $functions = null;
if (in_array($ext, ['jpeg', 'jpg'])) { if (in_array($ext, ['jpeg', 'jpg'])) {
$functions = ['imagecreatefromjpeg', 'imagejpeg']; $functions = ['imagecreatefromjpeg', 'imagejpeg'];
@ -468,7 +468,7 @@ class FTFileMakerTask extends BuildTask
return null; return null;
} }
$stamp = imagecreatefrompng($stampPath); $stamp = imagecreatefrompng($stampPath ?? '');
$targetImage = call_user_func($functions[0], $targetPath); $targetImage = call_user_func($functions[0], $targetPath);
// Set the margins for the stamp and get the height/width of the stamp image // Set the margins for the stamp and get the height/width of the stamp image
@ -477,8 +477,8 @@ class FTFileMakerTask extends BuildTask
$stampX = imagesx($stamp); $stampX = imagesx($stamp);
$stampY = imagesy($stamp); $stampY = imagesy($stamp);
$marge_right = rand($stampX, $targetX - $stampX); $marge_right = rand($stampX ?? 0, $targetX - $stampX);
$marge_bottom = rand($stampY, $targetY - $stampY); $marge_bottom = rand($stampY ?? 0, $targetY - $stampY);
// Copy the stamp image onto our photo using the margin offsets and the photo // Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp. // width to calculate positioning of the stamp.
@ -489,8 +489,8 @@ class FTFileMakerTask extends BuildTask
$targetY - $stampY - $marge_bottom, $targetY - $stampY - $marge_bottom,
0, 0,
0, 0,
$stampX, $stampX ?? 0,
$stampY $stampY ?? 0
); );
call_user_func($functions[1], $targetImage, $targetPath); call_user_func($functions[1], $targetImage, $targetPath);

View File

@ -59,10 +59,10 @@ class FTPageMakerTask extends BuildTask
// Allow pageCountByDepth to be passed as comma-separated value, e.g. pageCounts=5,100,1,1 // Allow pageCountByDepth to be passed as comma-separated value, e.g. pageCounts=5,100,1,1
$pageCounts = $request->getVar('pageCounts'); $pageCounts = $request->getVar('pageCounts');
if ($pageCounts) { if ($pageCounts) {
$counts = explode(',', $pageCounts); $counts = explode(',', $pageCounts ?? '');
$this->pageCountByDepth = array_map(function ($int) { $this->pageCountByDepth = array_map(function ($int) {
return (int) trim($int); return (int) trim($int ?? '');
}, $counts); }, $counts ?? []);
} }
$this->generatePages(0, "", 0, $withBlocks); $this->generatePages(0, "", 0, $withBlocks);
@ -70,13 +70,13 @@ class FTPageMakerTask extends BuildTask
protected function generatePages($depth = 0, $prefix = "", $parentID = 0, $withBlocks = false) protected function generatePages($depth = 0, $prefix = "", $parentID = 0, $withBlocks = false)
{ {
$maxDepth = count($this->pageCountByDepth); $maxDepth = count($this->pageCountByDepth ?? []);
$pageCount = $this->pageCountByDepth[$depth]; $pageCount = $this->pageCountByDepth[$depth];
$testPageClasses = ClassInfo::implementorsOf('TestPageInterface'); $testPageClasses = ClassInfo::implementorsOf('TestPageInterface');
for ($i=1; $i<=$pageCount; $i++) { for ($i=1; $i<=$pageCount; $i++) {
$fullPrefix = $prefix ? "{$prefix}-{$i}" : $i; $fullPrefix = $prefix ? "{$prefix}-{$i}" : $i;
$randomIndex = array_rand($testPageClasses); $randomIndex = array_rand($testPageClasses ?? []);
$pageClass = $testPageClasses[$randomIndex]; $pageClass = $testPageClasses[$randomIndex];
$page = new $pageClass(); $page = new $pageClass();
$page->ParentID = $parentID; $page->ParentID = $parentID;
@ -102,14 +102,14 @@ class FTPageMakerTask extends BuildTask
protected function generateBlocksForPage(Page $page) protected function generateBlocksForPage(Page $page)
{ {
$classes = array_filter($this->config()->get('block_generators'), function ($callable, $class) { $classes = array_filter($this->config()->get('block_generators') ?? [], function ($callable, $class) {
return class_exists($class); return class_exists($class ?? '');
}, ARRAY_FILTER_USE_BOTH); }, ARRAY_FILTER_USE_BOTH);
// Generate a random amount of blocks in the preferred range // Generate a random amount of blocks in the preferred range
$range = $this->blockCountRange; $range = $this->blockCountRange;
foreach(range($range[0], array_rand(range($range[0], $range[1]))) as $i) { foreach(range($range[0], array_rand(range($range[0], $range[1]))) as $i) {
$class = array_rand($classes); $class = array_rand($classes ?? []);
$callable = $classes[$class]; $callable = $classes[$class];
$block = call_user_func($callable, $page); $block = call_user_func($callable, $page);

View File

@ -54,8 +54,8 @@ class FTPageTypeCreatorTask extends BuildTask
$className = null; $className = null;
while ( while (
!$className || !$className ||
in_array($className, $pageTypes) || in_array($className, $pageTypes ?? []) ||
class_exists(basename($className, 'php')) class_exists(basename($className ?? '', 'php'))
) { ) {
$className = $this->generateClassName(); $className = $this->generateClassName();
} }