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 #102

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 code/BasicFieldsTestPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions code/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion code/multiform/TestMultiForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getFields()
$fields->push(new LiteralField("Heading", "<h3>You have submitted the following information:</h3>"));

foreach ($savedData as $key=>$value) {
if (preg_match("/_copy$/", $key)) {
if (preg_match("/_copy$/", $key ?? '')) {
continue;
}

Expand Down
40 changes: 20 additions & 20 deletions code/tasks/FTFileMakerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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 ?? '');
});
}

Expand All @@ -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
]);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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);

Expand Down
16 changes: 8 additions & 8 deletions code/tasks/FTPageMakerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,24 @@ 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);
}

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;
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions code/tasks/FTPageTypeCreatorTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down