From e5a9b149b1770cf696eb1bb47fdb6d03c2935c96 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 15 Nov 2024 19:43:30 +0900 Subject: [PATCH] phpstan fixes with move away from __get to dedicated get --- www/lib/CoreLibs/Convert/Color/CieXyz.php | 47 +++++++++----- www/lib/CoreLibs/Convert/Color/Color.php | 62 +++++++++---------- .../Convert/Color/Coordinates/HSB.php | 2 +- .../Convert/Color/Coordinates/HSL.php | 2 +- .../Convert/Color/Coordinates/HWB.php | 2 +- .../Interface/CoordinatesInterface.php | 2 +- .../Convert/Color/Coordinates/LCH.php | 8 +-- .../Convert/Color/Coordinates/Lab.php | 2 +- .../Convert/Color/Coordinates/RGB.php | 2 +- .../Convert/Color/Coordinates/XYZ.php | 2 +- www/lib/CoreLibs/Convert/Color/Utils.php | 6 +- www/lib/CoreLibs/Convert/Math.php | 9 ++- 12 files changed, 80 insertions(+), 66 deletions(-) diff --git a/www/lib/CoreLibs/Convert/Color/CieXyz.php b/www/lib/CoreLibs/Convert/Color/CieXyz.php index 65157597..d8fd927c 100644 --- a/www/lib/CoreLibs/Convert/Color/CieXyz.php +++ b/www/lib/CoreLibs/Convert/Color/CieXyz.php @@ -112,6 +112,21 @@ class CieXyz ); } + // MARK: helper convert any array to array{float, float, float} + + /** + * This is a hack for phpstan until we write a proper matrix to class + * conversion wrapper function + * + * @param array|float|int> $_array + * @return array{0:float,1:float,2:float} + */ + private static function convertArray(array $_array): array + { + /** @var array{0:float,1:float,2:float} */ + return [$_array[0], $_array[1], $_array[2]]; + } + // MARK: xyzD65 <-> xyzD50 /** @@ -122,14 +137,14 @@ class CieXyz */ private static function xyzD65ToXyzD50(XYZ $xyz): XYZ { - return new XYZ(Math::multiplyMatrices( + return new XYZ(self::convertArray(Math::multiplyMatrices( a: [ [1.0479298208405488, 0.022946793341019088, -0.05019222954313557], [0.029627815688159344, 0.990434484573249, -0.01707382502938514], [-0.009243058152591178, 0.015055144896577895, 0.7518742899580008], ], b: $xyz->returnAsArray(), - ), options: ["whitepoint" => 'D50']); + )), options: ["whitepoint" => 'D50']); } /** @@ -140,14 +155,14 @@ class CieXyz */ private static function xyzD50ToXyxD65(XYZ $xyz): XYZ { - return new XYZ(Math::multiplyMatrices( + return new XYZ(self::convertArray(Math::multiplyMatrices( a: [ [0.9554734527042182, -0.023098536874261423, 0.0632593086610217], [-0.028369706963208136, 1.0099954580058226, 0.021041398966943008], [0.012314001688319899, -0.020507696433477912, 1.3303659366080753], ], b: $xyz->returnAsArray() - ), options: ["whitepoint" => 'D65']); + )), options: ["whitepoint" => 'D65']); } // MARK: xyzD50 <-> Lab @@ -228,11 +243,11 @@ class CieXyz ]; return new XYZ( - array_map( + self::convertArray(array_map( fn ($k, $v) => $v * $d50[$k], array_keys($xyz), array_values($xyz), - ), + )), options: ["whitepoint" => 'D50'] ); } @@ -249,17 +264,17 @@ class CieXyz private static function linRgbToXyzD65(RGB $rgb): XYZ { // if not linear, convert to linear - if (!$rgb->linear) { + if (!$rgb->get('linear')) { $rgb = (new RGB($rgb->returnAsArray()))->toLinear(); } - return new XYZ(Math::multiplyMatrices( + return new XYZ(self::convertArray(Math::multiplyMatrices( [ [0.41239079926595934, 0.357584339383878, 0.1804807884018343], [0.21263900587151027, 0.715168678767756, 0.07219231536073371], [0.01933081871559182, 0.11919477979462598, 0.9505321522496607], ], $rgb->returnAsArray() - ), options: ["whitepoint" => 'D65']); + )), options: ["whitepoint" => 'D65']); } /** @@ -271,14 +286,14 @@ class CieXyz private static function xyzD65ToLinRgb(XYZ $xyz): RGB { // xyz D65 to linrgb - return new RGB(Math::multiplyMatrices( + return new RGB(self::convertArray(Math::multiplyMatrices( a : [ [ 3.2409699419045226, -1.537383177570094, -0.4986107602930034 ], [ -0.9692436362808796, 1.8759675015077202, 0.04155505740717559 ], [ 0.05563007969699366, -0.20397695888897652, 1.0569715142428786 ], ], b : $xyz->returnAsArray() - ), options: ["linear" => true]); + )), options: ["linear" => true]); } // MARK: xyzD65 <-> OkLab @@ -291,14 +306,14 @@ class CieXyz */ private static function xyzD65ToOkLab(XYZ $xyz): Lab { - return new Lab(Math::multiplyMatrices( + return new Lab(self::convertArray(Math::multiplyMatrices( [ [0.2104542553, 0.7936177850, -0.0040720468], [1.9779984951, -2.4285922050, 0.4505937099], [0.0259040371, 0.7827717662, -0.8086757660], ], array_map( - callback: fn ($v) => pow($v, 1 / 3), + callback: fn ($v) => pow((float)$v, 1 / 3), array: Math::multiplyMatrices( a: [ [0.8190224432164319, 0.3619062562801221, -0.12887378261216414], @@ -308,7 +323,7 @@ class CieXyz b: $xyz->returnAsArray(), ), ) - ), colorspace: 'OkLab'); + )), colorspace: 'OkLab'); } /** @@ -319,7 +334,7 @@ class CieXyz */ private static function okLabToXyzD65(Lab $lab): XYZ { - return new XYZ(Math::multiplyMatrices( + return new XYZ(self::convertArray(Math::multiplyMatrices( a: [ [1.2268798733741557, -0.5578149965554813, 0.28139105017721583], [-0.04057576262431372, 1.1122868293970594, -0.07171106666151701], @@ -337,7 +352,7 @@ class CieXyz b: $lab->returnAsArray(), ), ), - ), options: ["whitepoint" => 'D65']); + )), options: ["whitepoint" => 'D65']); } } diff --git a/www/lib/CoreLibs/Convert/Color/Color.php b/www/lib/CoreLibs/Convert/Color/Color.php index 744491fc..016d71af 100644 --- a/www/lib/CoreLibs/Convert/Color/Color.php +++ b/www/lib/CoreLibs/Convert/Color/Color.php @@ -55,13 +55,13 @@ class Color private static function __labToLch(Lab $lab): array { // cieLab to cieLch - $a = $lab->a; - $b = $lab->b; + $a = (float)$lab->get('a'); + $b = (float)$lab->get('b'); $hue = atan2($b, $a) * 180 / pi(); return [ - $lab->L, + (float)$lab->get('L'), sqrt($a ** 2 + $b ** 2), $hue >= 0 ? $hue : $hue + 360, ]; @@ -76,9 +76,9 @@ class Color private static function __lchToLab(LCH $lch): array { return [ - $lch->L, - $lch->C * cos($lch->H * pi() / 180), // a - $lch->C * sin($lch->H * pi() / 180), // b + (float)$lch->get('L'), + (float)$lch->get('C') * cos((float)$lch->get('H') * pi() / 180), // a + (float)$lch->get('C') * sin((float)$lch->get('H') * pi() / 180), // b ]; } @@ -94,9 +94,9 @@ class Color */ public static function rgbToHsl(RGB $rgb): HSL { - $red = $rgb->R / 255; - $green = $rgb->G / 255; - $blue = $rgb->B / 255; + $red = (float)$rgb->get('R') / 255; + $green = (float)$rgb->get('G') / 255; + $blue = (float)$rgb->get('B') / 255; $min = min($red, $green, $blue); $max = max($red, $green, $blue); @@ -147,9 +147,9 @@ class Color */ public static function hslToRgb(HSL $hsl): RGB { - $hue = $hsl->H; - $sat = $hsl->S; - $lum = $hsl->L; + $hue = (float)$hsl->get('H'); + $sat = (float)$hsl->get('S'); + $lum = (float)$hsl->get('L'); // calc to internal convert value for hue $hue = (1 / 360) * $hue; // convert to internal 0-1 format @@ -201,9 +201,9 @@ class Color */ public static function rgbToHsb(RGB $rgb): HSB { - $red = $rgb->R / 255; - $green = $rgb->G / 255; - $blue = $rgb->B / 255; + $red = (float)$rgb->get('R') / 255; + $green = (float)$rgb->get('G') / 255; + $blue = (float)$rgb->get('B') / 255; $MAX = max($red, $green, $blue); $MIN = min($red, $green, $blue); @@ -246,9 +246,9 @@ class Color */ public static function hsbToRgb(HSB $hsb): RGB { - $H = $hsb->H; - $S = $hsb->S; - $V = $hsb->B; + $H = (float)$hsb->get('H'); + $S = (float)$hsb->get('S'); + $V = (float)$hsb->get('B'); // convert to internal 0-1 format $S /= 100; $V /= 100; @@ -352,8 +352,8 @@ class Color */ public static function hslToHsb(HSL $hsl): HSB { - $saturation = $hsl->S / 100; - $lightness = $hsl->L / 100; + $saturation = (float)$hsl->get('S') / 100; + $lightness = (float)$hsl->get('L') / 100; // if lightness is 0, then we cannot return convert to hsb $value = $lightness + $saturation * min($lightness, 1 - $lightness); // print "Orig: " . print_r($hsl, true) . "\n"; @@ -366,7 +366,7 @@ class Color 200 * (1 - $lightness / $value); $value *= 100; return new HSB([ - $hsl->H, + (float)$hsl->get('H'), $saturation, $value, ]); @@ -381,9 +381,9 @@ class Color public static function hsbToHsl(HSB $hsb): HSL { // hsv/toHsl - $hue = $hsb->H; - $saturation = $hsb->S / 100; - $value = $hsb->B / 100; + $hue = (float)$hsb->get('H'); + $saturation = (float)$hsb->get('S') / 100; + $value = (float)$hsb->get('B') / 100; $lightness = $value * (1 - $saturation / 2); // check for B/W @@ -443,9 +443,9 @@ class Color { // hsv\Hwb return new HWB([ - $hsb->H, // hue, - $hsb->B * (100 - $hsb->S) / 100, // 2: brightness, 1: saturation - 100 - $hsb->B, + (float)$hsb->get('H'), // hue, + (float)$hsb->get('B') * (100 - (float)$hsb->get('S')) / 100, // 2: brightness, 1: saturation + 100 - (float)$hsb->get('B'), ]); } @@ -457,9 +457,9 @@ class Color */ public static function hwbToHsb(HWB $hwb): HSB { - $hue = $hwb->H; - $whiteness = $hwb->W / 100; - $blackness = $hwb->B / 100; + $hue = (float)$hwb->get('H'); + $whiteness = (float)$hwb->get('W') / 100; + $blackness = (float)$hwb->get('B') / 100; $sum = $whiteness + $blackness; // print "S: B/W: " . $sum . " /W: " . $whiteness . " /B: " . $blackness . "\n"; @@ -469,7 +469,7 @@ class Color $value = $whiteness / $sum * 100; } else { $value = 1 - $blackness; - $saturation = $value === 0 ? 0 : (1 - $whiteness / $value) * 100; + $saturation = $value === 0.0 ? 0 : (1 - $whiteness / $value) * 100; $value *= 100; } diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/HSB.php b/www/lib/CoreLibs/Convert/Color/Coordinates/HSB.php index 62d83a5b..1b15bd98 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/HSB.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/HSB.php @@ -124,7 +124,7 @@ class HSB implements Interface\CoordinatesInterface * @param string $name * @return float */ - public function __get(string $name): float|string|bool + public function get(string $name): float|string|bool { $name = strtoupper($name); if (!property_exists($this, $name)) { diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/HSL.php b/www/lib/CoreLibs/Convert/Color/Coordinates/HSL.php index c5198c23..1adbe5c9 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/HSL.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/HSL.php @@ -123,7 +123,7 @@ class HSL implements Interface\CoordinatesInterface * @param string $name * @return float */ - public function __get(string $name): float|string|bool + public function get(string $name): float|string|bool { if (!property_exists($this, $name)) { throw new \ErrorException('Creation of dynamic property is not allowed', 0); diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/HWB.php b/www/lib/CoreLibs/Convert/Color/Coordinates/HWB.php index dbad9861..898cf082 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/HWB.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/HWB.php @@ -123,7 +123,7 @@ class HWB implements Interface\CoordinatesInterface * @param string $name * @return float */ - public function __get(string $name): float|string|bool + public function get(string $name): float|string|bool { if (!property_exists($this, $name)) { throw new \ErrorException('Creation of dynamic property is not allowed', 0); diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/Interface/CoordinatesInterface.php b/www/lib/CoreLibs/Convert/Color/Coordinates/Interface/CoordinatesInterface.php index 72c056de..4606de42 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/Interface/CoordinatesInterface.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/Interface/CoordinatesInterface.php @@ -31,7 +31,7 @@ interface CoordinatesInterface * @param string $name * @return float */ - public function __get(string $name): float|string|bool; + public function get(string $name): float|string|bool; /** * Returns the color as array diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/LCH.php b/www/lib/CoreLibs/Convert/Color/Coordinates/LCH.php index 8f886292..ac57bdaa 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/LCH.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/LCH.php @@ -60,7 +60,7 @@ class LCH implements Interface\CoordinatesInterface * set from array * where 0: Lightness, 1: Chroma, 2: Hue * - * @param string|{0:float,1:float,2:float} $colors + * @param string|array{0:float,1:float,2:float} $colors * @param string $colorspace [default=''] * @param array $options [default=[]] * @return self @@ -145,7 +145,7 @@ class LCH implements Interface\CoordinatesInterface * @param string $name * @return float */ - public function __get(string $name): float|string|bool + public function get(string $name): float|string|bool { if (!property_exists($this, $name)) { throw new \ErrorException('Creation of dynamic property is not allowed', 0); @@ -214,9 +214,9 @@ class LCH implements Interface\CoordinatesInterface $string .= '(' . $this->L . ' ' - . $this->c + . $this->C . ' ' - . $this->h + . $this->H . Utils::setOpacity($opacity) . ');'; diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/Lab.php b/www/lib/CoreLibs/Convert/Color/Coordinates/Lab.php index d4e0b47e..3652187b 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/Lab.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/Lab.php @@ -151,7 +151,7 @@ class Lab implements Interface\CoordinatesInterface * @param string $name * @return float */ - public function __get(string $name): float|string|bool + public function get(string $name): float|string|bool { if (!property_exists($this, $name)) { throw new \ErrorException('Creation of dynamic property is not allowed', 0); diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/RGB.php b/www/lib/CoreLibs/Convert/Color/Coordinates/RGB.php index ce69e855..51d6a2b2 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/RGB.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/RGB.php @@ -111,7 +111,7 @@ class RGB implements Interface\CoordinatesInterface * @param string $name * @return float|bool */ - public function __get(string $name): float|string|bool + public function get(string $name): float|string|bool { if (!property_exists($this, $name)) { throw new \ErrorException('Creation of dynamic property is not allowed', 0); diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/XYZ.php b/www/lib/CoreLibs/Convert/Color/Coordinates/XYZ.php index 0909badb..3b0b9f9b 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/XYZ.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/XYZ.php @@ -118,7 +118,7 @@ class XYZ implements Interface\CoordinatesInterface * @param string $name * @return float */ - public function __get(string $name): float|string|bool + public function get(string $name): float|string|bool { if (!property_exists($this, $name)) { throw new \ErrorException('Creation of dynamic property is not allowed', 0); diff --git a/www/lib/CoreLibs/Convert/Color/Utils.php b/www/lib/CoreLibs/Convert/Color/Utils.php index b273f001..d4f10c37 100644 --- a/www/lib/CoreLibs/Convert/Color/Utils.php +++ b/www/lib/CoreLibs/Convert/Color/Utils.php @@ -15,11 +15,11 @@ use CoreLibs\Convert\Math; class Utils { - /** @var int deviation allowed for valid data checks, small */ + /** @var float deviation allowed for valid data checks, small */ public const EPSILON_SMALL = 0.000000000001; - /** @var int deviation allowed for valid data checks, medium */ + /** @var float deviation allowed for valid data checks, medium */ public const EPSILON_MEDIUM = 0.0000001; - /** @var int deviation allowed for valid data checks, big */ + /** @var float deviation allowed for valid data checks, big */ public const ESPILON_BIG = 0.0001; public static function compare(float $lower, float $value, float $upper, float $epslion): bool diff --git a/www/lib/CoreLibs/Convert/Math.php b/www/lib/CoreLibs/Convert/Math.php index 8b8043db..a20085f7 100644 --- a/www/lib/CoreLibs/Convert/Math.php +++ b/www/lib/CoreLibs/Convert/Math.php @@ -113,7 +113,6 @@ class Math break; case '==': return self::equalWithEpsilon($value, $limit, $epsilon); - break; case '>': if ($value > ($limit + $epsilon)) { return true; @@ -148,7 +147,7 @@ class Math if (!is_array($a[0] ?? null)) { // $a is vector, convert to [[a, b, c, ...]] - $a = [ $a ]; + $a = [$a]; } if (!is_array($b[0])) { @@ -164,7 +163,7 @@ class Math // transpose $b: $bCols = array_map( callback: fn ($k) => \array_map( - (fn ($i) => $i[$k]), + (fn ($i) => is_array($i) ? $i[$k] : 0), $b, ), array: array_keys($b[0]), @@ -176,7 +175,7 @@ class Math array_reduce( array: $row, callback: fn ($a, $v, $i = null) => $a + $v * ( - $col[$i ?? array_search($v, $row)] ?? 0 + $col[$i ?? array_search($v, $row) ?: 0] ), initial: 0, ) : @@ -198,7 +197,7 @@ class Math if ($p === 1) { // Avoid [[a], [b], [c], ...]]: return array_map( - callback: fn ($v) => $v[0], + callback: fn ($v) => $v[0] ?? 0, array: $product, ); }