Skip to content

Commit

Permalink
[TASK] Fix phpstan checkFunctionArgumentTypes errors in ext:core Imaging
Browse files Browse the repository at this point in the history
This patch fixes incompatible type usage in function arguments
and is preparatory work for introducing native type hints and
strict mode in all core files.

Releases: master, 10.4
Resolves: #92277
Change-Id: I56783a73d0bd0932ee26ce208e64fabbdc113699
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/66039
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
  • Loading branch information
alexanderschnitzler authored and ervaude committed Oct 6, 2020
1 parent aa482c2 commit 84dea4f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
75 changes: 38 additions & 37 deletions typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
Expand Up @@ -614,8 +614,8 @@ public function makeText(&$im, $conf, $workArea)
$fileMask = $tmpStr . '_maskNT.' . $this->gifExtension;
// Scalefactor
$sF = MathUtility::forceIntegerInRange($conf['niceText.']['scaleFactor'], 2, 5);
$newW = ceil($sF * imagesx($im));
$newH = ceil($sF * imagesy($im));
$newW = (int)ceil($sF * imagesx($im));
$newH = (int)ceil($sF * imagesy($im));
// Make mask
$maskImg = imagecreatetruecolor($newW, $newH);
$Bcolor = imagecolorallocate($maskImg, 255, 255, 255);
Expand Down Expand Up @@ -986,8 +986,8 @@ public function ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fon
{
// Initialize:
$stringParts = $this->splitString($string, $splitRendering, $fontSize, $fontFile);
$x = ceil($sF * $x);
$y = ceil($sF * $y);
$x = (int)ceil($sF * $x);
$y = (int)ceil($sF * $y);
// Traverse string parts:
foreach ($stringParts as $i => $strCfg) {
// Initialize:
Expand Down Expand Up @@ -1080,7 +1080,7 @@ public function splitString($string, $splitRendering, $fontSize, $fontFile)
// Initialize range:
$ranges = GeneralUtility::trimExplode(',', $cfg['value'], true);
foreach ($ranges as $i => $rangeDef) {
$ranges[$i] = GeneralUtility::intExplode('-', $ranges[$i]);
$ranges[$i] = GeneralUtility::intExplode('-', (string)$ranges[$i]);
if (!isset($ranges[$i][1])) {
$ranges[$i][1] = $ranges[$i][0];
}
Expand Down Expand Up @@ -1250,6 +1250,7 @@ protected function getWordPairsForLineBreak($string)
{
$wordPairs = [];
$wordsArray = preg_split('#([- .,!:]+)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
$wordsArray = is_array($wordsArray) ? $wordsArray : [];
$wordsCount = count($wordsArray);
for ($index = 0; $index < $wordsCount; $index += 2) {
$wordPairs[] = $wordsArray[$index] . $wordsArray[$index + 1];
Expand Down Expand Up @@ -1448,11 +1449,11 @@ public function makeShadow(&$im, $conf, $workArea, $txtConf)
if ($conf['intensity']) {
$intensity = MathUtility::forceIntegerInRange($conf['intensity'], 0, 100);
}
$intensity = ceil(255 - $intensity / 100 * 255);
$intensity = (int)ceil(255 - $intensity / 100 * 255);
$this->inputLevels($blurTextImg, 0, $intensity);
$opacity = MathUtility::forceIntegerInRange((int)$conf['opacity'], 0, 100);
if ($opacity && $opacity < 100) {
$high = ceil(255 * $opacity / 100);
$high = (int)ceil(255 * $opacity / 100);
// Reducing levels as the opacity demands
$this->outputLevels($blurTextImg, 0, $high);
}
Expand Down Expand Up @@ -1506,8 +1507,8 @@ public function makeBox(&$im, $conf, $workArea)
// PHP 0 = opaque, 127 = transparent
// TYPO3 100 = opaque, 0 = transparent
$opacity = MathUtility::forceIntegerInRange((int)$conf['opacity'], 1, 100, 1);
$opacity = abs($opacity - 100);
$opacity = round(127 * $opacity / 100);
$opacity = (int)abs($opacity - 100);
$opacity = (int)round(127 * $opacity / 100);
}
$tmpColor = imagecolorallocatealpha($im, $cols[0], $cols[1], $cols[2], $opacity);
imagefilledrectangle($im, $cords[0], $cords[1], $cords[0] + $cords[2] - 1, $cords[1] + $cords[3] - 1, $tmpColor);
Expand Down Expand Up @@ -1587,35 +1588,35 @@ public function IMparams($setup)
break;
case 'blur':
if ($this->processorEffectsEnabled) {
$commands .= $this->v5_blur($value);
$commands .= $this->v5_blur((int)$value);
}
break;
case 'sharpen':
if ($this->processorEffectsEnabled) {
$commands .= $this->v5_sharpen($value);
$commands .= $this->v5_sharpen((int)$value);
}
break;
case 'rotate':
$commands .= ' -rotate ' . MathUtility::forceIntegerInRange($value, 0, 360);
$commands .= ' -rotate ' . MathUtility::forceIntegerInRange((int)$value, 0, 360);
break;
case 'solarize':
$commands .= ' -solarize ' . MathUtility::forceIntegerInRange($value, 0, 99);
$commands .= ' -solarize ' . MathUtility::forceIntegerInRange((int)$value, 0, 99);
break;
case 'swirl':
$commands .= ' -swirl ' . MathUtility::forceIntegerInRange($value, 0, 1000);
$commands .= ' -swirl ' . MathUtility::forceIntegerInRange((int)$value, 0, 1000);
break;
case 'wave':
$params = GeneralUtility::intExplode(',', $value);
$commands .= ' -wave ' . MathUtility::forceIntegerInRange($params[0], 0, 99) . 'x' . MathUtility::forceIntegerInRange($params[1], 0, 99);
break;
case 'charcoal':
$commands .= ' -charcoal ' . MathUtility::forceIntegerInRange($value, 0, 100);
$commands .= ' -charcoal ' . MathUtility::forceIntegerInRange((int)$value, 0, 100);
break;
case 'gray':
$commands .= ' -colorspace GRAY';
break;
case 'edge':
$commands .= ' -edge ' . MathUtility::forceIntegerInRange($value, 0, 99);
$commands .= ' -edge ' . MathUtility::forceIntegerInRange((int)$value, 0, 99);
break;
case 'emboss':
$commands .= ' -emboss';
Expand All @@ -1627,10 +1628,10 @@ public function IMparams($setup)
$commands .= ' -flop';
break;
case 'colors':
$commands .= ' -colors ' . MathUtility::forceIntegerInRange($value, 2, 255);
$commands .= ' -colors ' . MathUtility::forceIntegerInRange((int)$value, 2, 255);
break;
case 'shear':
$commands .= ' -shear ' . MathUtility::forceIntegerInRange($value, -90, 90);
$commands .= ' -shear ' . MathUtility::forceIntegerInRange((int)$value, -90, 90);
break;
case 'invert':
$commands .= ' -negate';
Expand Down Expand Up @@ -1844,9 +1845,9 @@ public function inputLevels(&$im, $low, $high)
$totalCols = imagecolorstotal($im);
for ($c = 0; $c < $totalCols; $c++) {
$cols = imagecolorsforindex($im, $c);
$cols['red'] = MathUtility::forceIntegerInRange(($cols['red'] - $low) / $delta * 255, 0, 255);
$cols['green'] = MathUtility::forceIntegerInRange(($cols['green'] - $low) / $delta * 255, 0, 255);
$cols['blue'] = MathUtility::forceIntegerInRange(($cols['blue'] - $low) / $delta * 255, 0, 255);
$cols['red'] = MathUtility::forceIntegerInRange((int)(($cols['red'] - $low) / $delta * 255), 0, 255);
$cols['green'] = MathUtility::forceIntegerInRange((int)(($cols['green'] - $low) / $delta * 255), 0, 255);
$cols['blue'] = MathUtility::forceIntegerInRange((int)(($cols['blue'] - $low) / $delta * 255), 0, 255);
imagecolorset($im, $c, $cols['red'], $cols['green'], $cols['blue']);
}
}
Expand Down Expand Up @@ -1896,7 +1897,7 @@ public function IMreduceColors($file, $cols)
*/
public function v5_sharpen($factor)
{
$factor = MathUtility::forceIntegerInRange(ceil($factor / 10), 0, 10);
$factor = MathUtility::forceIntegerInRange((int)ceil($factor / 10), 0, 10);
$sharpenArr = explode(',', ',' . $this->im5fx_sharpenSteps);
$sharpenF = trim($sharpenArr[$factor]);
if ($sharpenF) {
Expand All @@ -1917,7 +1918,7 @@ public function v5_sharpen($factor)
*/
public function v5_blur($factor)
{
$factor = MathUtility::forceIntegerInRange(ceil($factor / 10), 0, 10);
$factor = MathUtility::forceIntegerInRange((int)ceil($factor / 10), 0, 10);
$blurArr = explode(',', ',' . $this->im5fx_blurSteps);
$blurF = trim($blurArr[$factor]);
if ($blurF) {
Expand Down Expand Up @@ -1967,12 +1968,12 @@ public function convertColor($string)
// Finding the RGB definitions of the color:
$string = $cParts[0];
if (strpos($string, '#') !== false) {
$string = preg_replace('/[^A-Fa-f0-9]*/', '', $string);
$string = preg_replace('/[^A-Fa-f0-9]*/', '', $string) ?? '';
$col[] = hexdec(substr($string, 0, 2));
$col[] = hexdec(substr($string, 2, 2));
$col[] = hexdec(substr($string, 4, 2));
} elseif (strpos($string, ',') !== false) {
$string = preg_replace('/[^,0-9]*/', '', $string);
$string = preg_replace('/[^,0-9]*/', '', $string) ?? '';
$strArr = explode(',', $string);
$col[] = (int)$strArr[0];
$col[] = (int)$strArr[1];
Expand All @@ -1990,14 +1991,14 @@ public function convertColor($string)
$cParts[1] = trim($cParts[1]);
if ($cParts[1][0] === '*') {
$val = (float)substr($cParts[1], 1);
$col[0] = MathUtility::forceIntegerInRange($col[0] * $val, 0, 255);
$col[1] = MathUtility::forceIntegerInRange($col[1] * $val, 0, 255);
$col[2] = MathUtility::forceIntegerInRange($col[2] * $val, 0, 255);
$col[0] = MathUtility::forceIntegerInRange((int)($col[0] * $val), 0, 255);
$col[1] = MathUtility::forceIntegerInRange((int)($col[1] * $val), 0, 255);
$col[2] = MathUtility::forceIntegerInRange((int)($col[2] * $val), 0, 255);
} else {
$val = (int)$cParts[1];
$col[0] = MathUtility::forceIntegerInRange($col[0] + $val, 0, 255);
$col[1] = MathUtility::forceIntegerInRange($col[1] + $val, 0, 255);
$col[2] = MathUtility::forceIntegerInRange($col[2] + $val, 0, 255);
$col[0] = MathUtility::forceIntegerInRange((int)($col[0] + $val), 0, 255);
$col[1] = MathUtility::forceIntegerInRange((int)($col[1] + $val), 0, 255);
$col[2] = MathUtility::forceIntegerInRange((int)($col[2] + $val), 0, 255);
}
}
return $col;
Expand Down Expand Up @@ -2126,7 +2127,7 @@ public function imageMagickConvert($imagefile, $newExt = '', $w = '', $h = '', $
}
$info[0] = $data[0];
$info[1] = $data[1];
$frame = $this->addFrameSelection ? (int)$frame : '';
$frame = $this->addFrameSelection ? (int)$frame : 0;
if (!$params) {
$params = $this->cmds[$newExt];
}
Expand Down Expand Up @@ -2300,8 +2301,8 @@ protected function generateStatusHashForImageFile($filePath)
* Get numbers for scaling the image based on input
*
* @param array $info Current image information: Width, Height etc.
* @param int $w "required" width
* @param int $h "required" height
* @param string $w "required" width
* @param string $h "required" height
* @param array $options Options: Keys are like "maxW", "maxH", "minW", "minH
* @return array
* @internal
Expand All @@ -2312,8 +2313,8 @@ public function getImageScale($info, $w, $h, $options)
$out = [];
$max = strpos($w . $h, 'm') !== false ? 1 : 0;
if (strpos($w . $h, 'c') !== false) {
$out['cropH'] = (int)substr(strstr($w, 'c'), 1);
$out['cropV'] = (int)substr(strstr($h, 'c'), 1);
$out['cropH'] = (int)substr((string)strstr($w, 'c'), 1);
$out['cropV'] = (int)substr((string)strstr($h, 'c'), 1);
$crs = true;
} else {
$crs = false;
Expand Down Expand Up @@ -2750,7 +2751,7 @@ public function imgTag($imgInfo)
public function ImageWrite($destImg, $theImage, $quality = 0)
{
imageinterlace($destImg, 0);
$ext = strtolower(substr($theImage, strrpos($theImage, '.') + 1));
$ext = strtolower(substr($theImage, (int)strrpos($theImage, '.') + 1));
$result = false;
switch ($ext) {
case 'jpg':
Expand Down
Expand Up @@ -92,10 +92,16 @@ protected function getInlineSvg($source)
}

$svgContent = file_get_contents($source);
$svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
if ($svgContent === false) {
return '';
}
$svgContent = (string)preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
// Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
$previousValueOfEntityLoader = libxml_disable_entity_loader(true);
$svgElement = simplexml_load_string($svgContent);
if ($svgElement === false) {
return '';
}
libxml_disable_entity_loader($previousValueOfEntityLoader);

// remove xml version tag
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/Imaging/IconRegistry.php
Expand Up @@ -730,7 +730,7 @@ public function getCacheIdentifier(): string
$this->initialize();
}

return sha1(json_encode($this->icons));
return sha1((string)json_encode($this->icons));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/Imaging/ImageDimension.php
Expand Up @@ -148,7 +148,7 @@ private static function getConfigurationForImageCropScaleMask(TaskInterface $tas
$options['crop'] = new Area($cropData->x, $cropData->y, $cropData->width, $cropData->height);
} else {
[$offsetLeft, $offsetTop, $newWidth, $newHeight] = explode(',', $configuration['crop'], 4);
$options['crop'] = new Area($offsetLeft, $offsetTop, $newWidth, $newHeight);
$options['crop'] = new Area((float)$offsetLeft, (float)$offsetTop, (float)$newWidth, (float)$newHeight);
}
if ($options['crop']->isEmpty()) {
unset($options['crop']);
Expand Down

0 comments on commit 84dea4f

Please sign in to comment.