clean-code #4

Merged
TorstenHettstedt merged 9 commits from clean-code into master 2021-03-23 16:25:58 +01:00
16 changed files with 148 additions and 138 deletions
+4 -2
View File
@@ -22,11 +22,13 @@
},
"require": {
"php": "^8.0",
"phpunit/php-code-coverage": "^9.2"
"phpunit/php-code-coverage": "^9.2",
"jetbrains/phpstorm-attributes": "^1.0.0"
},
"require-dev": {
"codeception/codeception": "^4.1.18",
"codeception/module-phpbrowser": "^1.0.0",
"codeception/module-asserts": "^1.0.0"
"codeception/module-asserts": "^1.0.0",
"phpstan/phpstan" : "^0.12.82"
}
}
+6
View File
@@ -0,0 +1,6 @@
parameters:
level: 6
paths:
- src
- tests/unit
- tests/_support/Helper
+1 -1
View File
@@ -8,7 +8,7 @@ use InvalidArgumentException;
abstract class AbstractColorModel
{
protected function checkValue(float $value)
protected function checkValue(float $value): void
{
if ($value > 1.0) {
throw new InvalidArgumentException('Ein Wert größer als 1 ist nicht definierbar.');
@@ -0,0 +1,61 @@
<?php
namespace TorstenHettstedt\Colors\Transformer\Utilities;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
abstract class AbstractHueSaturationValueToUtility
{
/** @var int : Größe des Intervals in 1/100 Grad */
protected const INTERVAL = 60.0 * 100.0;
// Daten für HSV
/** @var float : Definition des Farbton als Anteil eines Vollkreises '$Hue * π' */
protected float $hue;
/** @var float : Anteil am Vollkreis in 1/100 Grad */
protected float $hueAsPercentGrad;
/** @var float : Definition der Sättigung der Farbe */
protected float $saturation;
/** @var float : Definition der Helligkeit der Farbe */
protected float $value;
/**
* HueSaturationValueToRedGreenBlue constructor.
* @param HueSaturationValue $from_color
* @noinspection PhpPureAttributeCanBeAddedInspection
*/
public function __construct(HueSaturationValue $from_color)
{
$this->hue = $from_color->getHue();
$this->hueAsPercentGrad = intval($this->hue * self::INTERVAL * 6.0);
$this->saturation = $from_color->getSaturation();
$this->value = $from_color->getValue();
}
protected function processConvert(): void
{
if ($this->saturation > 0.0) {
$this->splitColor();
}
}
abstract protected function splitColor(): void;
/**
* @param bool $reciprocal
*
* @return float
*/
protected function calcColorValue($reciprocal = false): float
{
// Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]'
$remainder_grad = $this->hueAsPercentGrad % self::INTERVAL;
// Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]'
$remainder = $remainder_grad / self::INTERVAL;
return $this->saturation * ($reciprocal ? (1 - $remainder) : $remainder);
}
}
@@ -2,6 +2,7 @@
namespace TorstenHettstedt\Colors\Transformer\Utilities;
use JetBrains\PhpStorm\Pure;
use TorstenHettstedt\Colors\ColorModels\CyanMagentaYellowKey;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
@@ -72,6 +73,7 @@ class CyanMagentaYellowKeyToHueSaturationValue
->setValue($this->value);
}
#[Pure]
protected function buildHueAmount(): float
{
// Wenn beide Werte gleich sind, ist die Endfarbe Grau.
@@ -5,23 +5,9 @@ namespace TorstenHettstedt\Colors\Transformer\Utilities;
use TorstenHettstedt\Colors\ColorModels\CyanMagentaYellowKey;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
class HueSaturationValueToCyanMagentaYellowKey
class HueSaturationValueToCyanMagentaYellowKey extends AbstractHueSaturationValueToUtility
{
/** @var int : Größe des Intervals in 1/100 Grad */
protected const INTERVAL = 60.0 * 100.0;
// Daten für HSV
/** @var float : Definition des Farbton als Anteil eines Vollkreises '$Hue * π' */
protected float $hue;
/** @var float : Anteil am Vollkreis in 1/100 Grad */
protected float $hueAsPercentGrad;
/** @var float : Definition der Sättigung der Farbe */
protected float $saturation;
/** @var float : Definition der Helligkeit der Farbe */
protected float $value;
// Daten für CMYK
/** @var float : Definition des Cyan-Anteil */
protected float $cyan = 0.0;
@@ -32,19 +18,6 @@ class HueSaturationValueToCyanMagentaYellowKey
/** @var float : Definition des Schwarzanteil */
protected float $key = 0.0;
/**
* HueSaturationValueToRedGreenBlue constructor.
* @param HueSaturationValue $from_color
* @noinspection PhpPureAttributeCanBeAddedInspection
*/
public function __construct(HueSaturationValue $from_color)
{
$this->hue = $from_color->getHue();
$this->hueAsPercentGrad = intval($this->hue * self::INTERVAL * 6);
$this->saturation = $from_color->getSaturation();
$this->value = $from_color->getValue();
}
public function convert(): CyanMagentaYellowKey
{
$this->processConvert();
@@ -60,17 +33,15 @@ class HueSaturationValueToCyanMagentaYellowKey
protected function processConvert(): void
{
$this->key = 1 - $this->value;
if ($this->saturation !== 0.0) {
$this->splitColor();
}
parent::processConvert();
}
protected function splitColor(): void
{
// ID des zu nutzenden Interval
$interval_id = intdiv($this->hueAsPercentGrad , self::INTERVAL);
$interval_id = $this->hueAsPercentGrad / self::INTERVAL;
switch ($interval_id) {
switch ((int)$interval_id) {
case 1: // Yellow
$this->cyan = $this->calcColorValue();
$this->yellow = $this->saturation;
@@ -98,13 +69,4 @@ class HueSaturationValueToCyanMagentaYellowKey
}
}
protected function calcColorValue($reciprocal = false): float
{
// Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]'
$remainder_grad = $this->hueAsPercentGrad % self::INTERVAL;
// Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]'
$remainder = $remainder_grad / self::INTERVAL;
return $this->saturation * ($reciprocal ? (1 - $remainder) : $remainder);
}
}
@@ -4,24 +4,10 @@
namespace TorstenHettstedt\Colors\Transformer\Utilities;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
use TorstenHettstedt\Colors\ColorModels\RedGreenBlue;
class HueSaturationValueToRedGreenBlue
class HueSaturationValueToRedGreenBlue extends AbstractHueSaturationValueToUtility
{
/** @var int : Größe des Intervals in 1/100 Grad */
protected const INTERVAL = 60.0 * 100.0;
// Daten für HSV
/** @var float : Definition des Farbton als Anteil eines Vollkreises '$Hue * π' */
protected float $hue;
/** @var float : Anteil am Vollkreis in 1/100 Grad */
protected float $hueAsPercentGrad;
/** @var float : Definition der Sättigung der Farbe */
protected float $saturation;
/** @var float : Definition der Helligkeit der Farbe */
protected float $value;
// Daten für RGB
/** @var float : Definition des Rotanteil */
protected float $red = 1.0;
@@ -30,19 +16,6 @@ class HueSaturationValueToRedGreenBlue
/** @var float : Definition des Blauanteil */
protected float $blue = 1.0;
/**
* HueSaturationValueToRedGreenBlue constructor.
* @param HueSaturationValue $from_color
* @noinspection PhpPureAttributeCanBeAddedInspection
*/
public function __construct(HueSaturationValue $from_color)
{
$this->hue = $from_color->getHue();
$this->hueAsPercentGrad = intval($this->hue * self::INTERVAL * 6);
$this->saturation = $from_color->getSaturation();
$this->value = $from_color->getValue();
}
public function convert(): RedGreenBlue
{
$this->processConvert();
@@ -57,19 +30,17 @@ class HueSaturationValueToRedGreenBlue
protected function processConvert(): void
{
$this->red = $this->green = $this->blue = $this->value;
if ($this->saturation > 0.0) {
$this->splitColor();
}
parent::processConvert();
}
protected function splitColor(): void
{
// ID des zu nutzenden Interval
$interval_id = intdiv($this->hueAsPercentGrad , self::INTERVAL);
$interval_id = $this->hueAsPercentGrad / self::INTERVAL;
// Ist der Wert der entgegen gesetzten Farbe.
$opposite_color_value = $this->value * ( 1 - $this->saturation);
switch ($interval_id) {
switch ((int)$interval_id) {
case 1:
$this->red = $this->calcColorValue();
$this->blue = $opposite_color_value;
@@ -99,12 +70,8 @@ class HueSaturationValueToRedGreenBlue
protected function calcColorValue($reciprocal = false): float
{
// Rest-Anteil am Vollkreis in 1/100 Grad
$remainder_grad = $this->hueAsPercentGrad % self::INTERVAL;
// Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]'
$remainder = $remainder_grad / self::INTERVAL;
$color_value = $this->value;
$color_value *= 1 - ($this->saturation * ($reciprocal ? (1 - $remainder) : $remainder));
$color_value *= 1 - parent::calcColorValue($reciprocal);
return $color_value;
}
@@ -2,6 +2,7 @@
namespace TorstenHettstedt\Colors\Transformer\Utilities;
use JetBrains\PhpStorm\Pure;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
use TorstenHettstedt\Colors\ColorModels\RedGreenBlue;
@@ -70,6 +71,7 @@ class RedGreenBlueToHueSaturationValue
->setValue($this->value);
}
#[Pure]
protected function buildHueAmount(): float
{
// Wenn beide Werte gleich sind, ist die Endfarbe Grau.
@@ -11,6 +11,9 @@ class ValidColorValuesProvider
const RGB = 'rgb';
const HSV = 'hsv';
/**
* @var array<string, array<string, float>[]>
*/
protected array $definitions = [
'weiß' => [
self::CMYK => [
@@ -302,6 +305,11 @@ class ValidColorValuesProvider
],
];
/**
* @param array<string> $order
*
* @return array<string, array<string, float>[]>
*/
public function getValidColorValues(array $order): array
{
$values = [];
@@ -8,7 +8,7 @@ use Codeception\Test\Unit;
abstract class AbstractColorModelUnit extends Unit
{
abstract public function testConstruct();
abstract public function testConstruct(): void;
/**
* @return float[][]
@@ -8,7 +8,7 @@ use TorstenHettstedt\Colors\ColorModels\CyanMagentaYellowKey;
class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
{
public function testConstruct()
public function testConstruct(): void
{
$color = new CyanMagentaYellowKey();
@@ -23,7 +23,7 @@ class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetCyanWithValidValue(float $value)
public function testSetCyanWithValidValue(float $value): void
{
$color = new CyanMagentaYellowKey();
@@ -36,7 +36,7 @@ class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetMagentaWithValidValue(float $value)
public function testSetMagentaWithValidValue(float $value): void
{
$color = new CyanMagentaYellowKey();
@@ -49,7 +49,7 @@ class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetYellowWithValidValue(float $value)
public function testSetYellowWithValidValue(float $value): void
{
$color = new CyanMagentaYellowKey();
@@ -62,7 +62,7 @@ class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetKeyWithValidValue(float $value)
public function testSetKeyWithValidValue(float $value): void
{
$color = new CyanMagentaYellowKey();
@@ -75,7 +75,7 @@ class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetCyanWithInvalidValue(float $value)
public function testSetCyanWithInvalidValue(float $value): void
{
$color = new CyanMagentaYellowKey();
@@ -88,7 +88,7 @@ class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetMagentaWithInvalidValue(float $value)
public function testSetMagentaWithInvalidValue(float $value): void
{
$color = new CyanMagentaYellowKey();
@@ -101,7 +101,7 @@ class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetYellowWithInvalidValue(float $value)
public function testSetYellowWithInvalidValue(float $value): void
{
$color = new CyanMagentaYellowKey();
@@ -114,7 +114,7 @@ class CyanMagentaYellowKeyTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetKeyWithInvalidValue(float $value)
public function testSetKeyWithInvalidValue(float $value): void
{
$color = new CyanMagentaYellowKey();
@@ -8,7 +8,7 @@ use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
class HueSaturationValueTest extends AbstractColorModelUnit
{
public function testConstruct()
public function testConstruct(): void
{
$color = new HueSaturationValue();
@@ -22,7 +22,7 @@ class HueSaturationValueTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetHueWithValidValue(float $value)
public function testSetHueWithValidValue(float $value): void
{
$color = new HueSaturationValue();
@@ -35,7 +35,7 @@ class HueSaturationValueTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetHueWithInvalidValue(float $value)
public function testSetHueWithInvalidValue(float $value): void
{
$color = new HueSaturationValue();
@@ -48,7 +48,7 @@ class HueSaturationValueTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetSaturationWithValidValue(float $value)
public function testSetSaturationWithValidValue(float $value): void
{
$color = new HueSaturationValue();
@@ -61,7 +61,7 @@ class HueSaturationValueTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetSaturationWithInvalidValue(float $value)
public function testSetSaturationWithInvalidValue(float $value): void
{
$color = new HueSaturationValue();
@@ -74,7 +74,7 @@ class HueSaturationValueTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetValueWithValidValue(float $value)
public function testSetValueWithValidValue(float $value): void
{
$color = new HueSaturationValue();
@@ -87,7 +87,7 @@ class HueSaturationValueTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetValueWithInvalidValue(float $value)
public function testSetValueWithInvalidValue(float $value): void
{
$color = new HueSaturationValue();
+7 -7
View File
@@ -8,7 +8,7 @@ use TorstenHettstedt\Colors\ColorModels\RedGreenBlue;
class RedGreenBlueTest extends AbstractColorModelUnit
{
public function testConstruct()
public function testConstruct(): void
{
$color = new RedGreenBlue();
@@ -22,7 +22,7 @@ class RedGreenBlueTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetRedWithValidValue(float $value)
public function testSetRedWithValidValue(float $value): void
{
$color = new RedGreenBlue();
@@ -35,7 +35,7 @@ class RedGreenBlueTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetRedWithInvalidValue(float $value)
public function testSetRedWithInvalidValue(float $value): void
{
$color = new RedGreenBlue();
@@ -47,7 +47,7 @@ class RedGreenBlueTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetGreenWithValidValue(float $value)
public function testSetGreenWithValidValue(float $value): void
{
$color = new RedGreenBlue();
@@ -60,7 +60,7 @@ class RedGreenBlueTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetGreenWithInvalidValue(float $value)
public function testSetGreenWithInvalidValue(float $value): void
{
$color = new RedGreenBlue();
@@ -73,7 +73,7 @@ class RedGreenBlueTest extends AbstractColorModelUnit
*
* @dataProvider validValueProvider
*/
public function testSetBlueWithValidValue(float $value)
public function testSetBlueWithValidValue(float $value): void
{
$color = new RedGreenBlue();
@@ -86,7 +86,7 @@ class RedGreenBlueTest extends AbstractColorModelUnit
*
* @dataProvider invalidValueProvider
*/
public function testSetBlueWithInvalidValue(float $value)
public function testSetBlueWithInvalidValue(float $value): void
{
$color = new RedGreenBlue();
@@ -16,7 +16,7 @@ class CyanMagentaYellowKeyTransformerTest extends Unit
protected CyanMagentaYellowKey $color;
/**
* @return float[][][]
* @return array<string, array<string, float>[]>
*/
public function validRgbValuesProvider(): array
{
@@ -27,7 +27,7 @@ class CyanMagentaYellowKeyTransformerTest extends Unit
}
/**
* @return float[][][]
* @return array<string, array<string, float>[]>
*/
public function validHsvValuesProvider(): array
{
@@ -38,14 +38,14 @@ class CyanMagentaYellowKeyTransformerTest extends Unit
}
/**
* @param array $cmyk
* @param array $rgb
* @param array<string, float> $cmyk
* @param array<string, float> $rgb
*
* @dataProvider validRgbValuesProvider
*
* @throws Exception
*/
public function testToRedGreenBlue(array $cmyk, array $rgb)
public function testToRedGreenBlue(array $cmyk, array $rgb): void
{
$this->color = $this->make(CyanMagentaYellowKey::class, $cmyk);
@@ -60,13 +60,13 @@ class CyanMagentaYellowKeyTransformerTest extends Unit
}
/**
* @param array $cmyk
* @param array<string, float> $cmyk
*
* @dataProvider validRgbValuesProvider
*
* @throws Exception
*/
public function testToCyanMagentaYellowKey(array $cmyk)
public function testToCyanMagentaYellowKey(array $cmyk): void
{
$this->color = $this->make(CyanMagentaYellowKey::class, $cmyk);
@@ -82,15 +82,15 @@ class CyanMagentaYellowKeyTransformerTest extends Unit
}
/**
* @param array $cmyk
* @param array $hsv
* @param array<string, float> $cmyk
* @param array<string, float> $hsv
*
* @throws Exception
*
* @dataProvider validHsvValuesProvider
*
*/
public function testToHueSaturationValue(array $cmyk, array $hsv)
public function testToHueSaturationValue(array $cmyk, array $hsv): void
{
$this->color = $this->make(CyanMagentaYellowKey::class, $cmyk);
@@ -16,7 +16,7 @@ class HueSaturationValueTransformerTest extends Unit
protected HueSaturationValue $color;
/**
* @return float[][][]
* @return array<string, array<string, float>[]>
*/
public function validCmykValuesProvider(): array
{
@@ -27,7 +27,7 @@ class HueSaturationValueTransformerTest extends Unit
}
/**
* @return float[][][]
* @return array<string, array<string, float>[]>
*/
public function validRgbValuesProvider(): array
{
@@ -38,14 +38,14 @@ class HueSaturationValueTransformerTest extends Unit
}
/**
* @param array $hsv
* @param array $rgb
* @param array<string, float> $hsv
* @param array<string, float> $rgb
*
* @dataProvider validRgbValuesProvider
*
* @throws Exception
*/
public function testToRedGreenBlue(array $hsv, array $rgb)
public function testToRedGreenBlue(array $hsv, array $rgb): void
{
$this->color = $this->make(HueSaturationValue::class, $hsv);
@@ -60,14 +60,14 @@ class HueSaturationValueTransformerTest extends Unit
}
/**
* @param array $hsv
* @param array $cmyk
* @param array<string, float> $hsv
* @param array<string, float> $cmyk
*
* @throws Exception
* @dataProvider validCmykValuesProvider
*
*/
public function testToCyanMagentaYellowKey(array $hsv, array $cmyk)
public function testToCyanMagentaYellowKey(array $hsv, array $cmyk): void
{
$this->color = $this->make(HueSaturationValue::class, $hsv);
@@ -83,12 +83,12 @@ class HueSaturationValueTransformerTest extends Unit
}
/**
* @param array $hsv
* @param array<string, float> $hsv
*
* @throws Exception
* @dataProvider validCmykValuesProvider
*/
public function testToHueSaturationValue(array $hsv)
public function testToHueSaturationValue(array $hsv): void
{
$this->color = $this->make(HueSaturationValue::class, $hsv);
@@ -16,7 +16,7 @@ class RedGreenBlueTransformerTest extends Unit
protected RedGreenBlue $color;
/**
* @return float[][][]
* @return array<string, array<string, float>[]>
*/
public function validCmykValuesProvider(): array
{
@@ -27,7 +27,7 @@ class RedGreenBlueTransformerTest extends Unit
}
/**
* @return float[][][]
* @return array<string, array<string, float>[]>
*/
public function validHsvValuesProvider(): array
{
@@ -38,13 +38,13 @@ class RedGreenBlueTransformerTest extends Unit
}
/**
* @param array $rgb
* @param array<string, float> $rgb
*
* @dataProvider validCmykValuesProvider
*
* @throws Exception
*/
public function testToRedGreenBlue(array $rgb)
public function testToRedGreenBlue(array $rgb): void
{
$this->color = $this->make(RedGreenBlue::class, $rgb);
@@ -59,14 +59,14 @@ class RedGreenBlueTransformerTest extends Unit
}
/**
* @param array $rgb
* @param array $cmyk
* @param array<string, float> $rgb
* @param array<string, float> $cmyk
*
* @throws Exception
* @dataProvider validCmykValuesProvider
*
*/
public function testToCyanMagentaYellowKey(array $rgb, array $cmyk)
public function testToCyanMagentaYellowKey(array $rgb, array $cmyk): void
{
$this->color = $this->make(RedGreenBlue::class, $rgb);
@@ -82,13 +82,13 @@ class RedGreenBlueTransformerTest extends Unit
}
/**
* @param array $rgb
* @param array $hsv
* @param array<string, float> $rgb
* @param array<string, float> $hsv
*
* @throws Exception
* @dataProvider validHsvValuesProvider
*/
public function testToHueSaturationValue(array $rgb, array $hsv)
public function testToHueSaturationValue(array $rgb, array $hsv): void
{
$this->color = $this->make(RedGreenBlue::class, $rgb);