1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-13 17:34:04 +02:00

Refactor short callbacks to arrow functions

This commit is contained in:
Oliver Vogel
2024-12-30 10:16:03 +01:00
parent c66fa7dac8
commit 2a62d490cc
7 changed files with 74 additions and 67 deletions

View File

@@ -23,6 +23,7 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable
*/ */
public function __construct(protected array $items = []) public function __construct(protected array $items = [])
{ {
//
} }
/** /**
@@ -181,11 +182,13 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable
*/ */
public function map(callable $callback): self public function map(callable $callback): self
{ {
$items = array_map(function ($item) use ($callback) {
return $callback($item);
}, $this->items);
return new self($items); return new self(
array_map(
fn($item) => $callback($item),
$this->items,
)
);
} }
/** /**
@@ -196,11 +199,12 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable
*/ */
public function filter(callable $callback): self public function filter(callable $callback): self
{ {
$items = array_filter($this->items, function ($item) use ($callback) { return new self(
return $callback($item); array_filter(
}); $this->items,
fn($item) => $callback($item),
return new self($items); )
);
} }
/** /**

View File

@@ -36,9 +36,10 @@ abstract class AbstractColor implements ColorInterface
*/ */
public function channel(string $classname): ColorChannelInterface public function channel(string $classname): ColorChannelInterface
{ {
$channels = array_filter($this->channels(), function (ColorChannelInterface $channel) use ($classname) { $channels = array_filter(
return $channel::class == $classname; $this->channels(),
}); fn(ColorChannelInterface $channel) => $channel::class == $classname,
);
if (count($channels) == 0) { if (count($channels) == 0) {
throw new ColorException('Color channel ' . $classname . ' could not be found.'); throw new ColorException('Color channel ' . $classname . ' could not be found.');
@@ -54,9 +55,10 @@ abstract class AbstractColor implements ColorInterface
*/ */
public function normalize(): array public function normalize(): array
{ {
return array_map(function (ColorChannelInterface $channel) { return array_map(
return $channel->normalize(); fn(ColorChannelInterface $channel) => $channel->normalize(),
}, $this->channels()); $this->channels(),
);
} }
/** /**
@@ -66,9 +68,10 @@ abstract class AbstractColor implements ColorInterface
*/ */
public function toArray(): array public function toArray(): array
{ {
return array_map(function (ColorChannelInterface $channel) { return array_map(
return $channel->value(); fn(ColorChannelInterface $channel) => $channel->value(),
}, $this->channels()); $this->channels()
);
} }
/** /**

View File

@@ -34,11 +34,11 @@ class Colorspace implements ColorspaceInterface
*/ */
public function colorFromNormalized(array $normalized): ColorInterface public function colorFromNormalized(array $normalized): ColorInterface
{ {
$values = array_map(function ($classname, $value_normalized) { return new Color(...array_map(
return (new $classname(normalized: $value_normalized))->value(); fn($classname, $value_normalized) => (new $classname(normalized: $value_normalized))->value(),
}, self::$channels, $normalized); self::$channels,
$normalized,
return new Color(...$values); ));
} }
/** /**

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Hsv\Color as HsvColor; use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -32,11 +33,11 @@ class Colorspace implements ColorspaceInterface
*/ */
public function colorFromNormalized(array $normalized): ColorInterface public function colorFromNormalized(array $normalized): ColorInterface
{ {
$values = array_map(function ($classname, $value_normalized) { return new Color(...array_map(
return (new $classname(normalized: $value_normalized))->value(); fn($classname, $value_normalized) => (new $classname(normalized: $value_normalized))->value(),
}, self::$channels, $normalized); self::$channels,
$normalized
return new Color(...$values); ));
} }
/** /**
@@ -66,9 +67,10 @@ class Colorspace implements ColorspaceInterface
} }
// normalized values of rgb channels // normalized values of rgb channels
$values = array_map(function ($channel) { $values = array_map(
return $channel->normalize(); fn(ColorChannelInterface $channel) => $channel->normalize(),
}, $color->channels()); $color->channels(),
);
// take only RGB // take only RGB
$values = array_slice($values, 0, 3); $values = array_slice($values, 0, 3);
@@ -116,9 +118,10 @@ class Colorspace implements ColorspaceInterface
} }
// normalized values of hsv channels // normalized values of hsv channels
list($h, $s, $v) = array_map(function ($channel) { list($h, $s, $v) = array_map(
return $channel->normalize(); fn(ColorChannelInterface $channel) => $channel->normalize(),
}, $color->channels()); $color->channels(),
);
// calculate Luminance // calculate Luminance
$luminance = (2 - $s) * $v / 2; $luminance = (2 - $s) * $v / 2;

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Hsl\Color as HslColor; use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -32,11 +33,11 @@ class Colorspace implements ColorspaceInterface
*/ */
public function colorFromNormalized(array $normalized): ColorInterface public function colorFromNormalized(array $normalized): ColorInterface
{ {
$values = array_map(function ($classname, $value_normalized) { return new Color(...array_map(
return (new $classname(normalized: $value_normalized))->value(); fn($classname, $value_normalized) => (new $classname(normalized: $value_normalized))->value(),
}, self::$channels, $normalized); self::$channels,
$normalized
return new Color(...$values); ));
} }
/** /**
@@ -66,9 +67,7 @@ class Colorspace implements ColorspaceInterface
} }
// normalized values of rgb channels // normalized values of rgb channels
$values = array_map(function ($channel) { $values = array_map(fn(ColorChannelInterface $channel) => $channel->normalize(), $color->channels());
return $channel->normalize();
}, $color->channels());
// take only RGB // take only RGB
$values = array_slice($values, 0, 3); $values = array_slice($values, 0, 3);
@@ -116,9 +115,7 @@ class Colorspace implements ColorspaceInterface
} }
// normalized values of hsl channels // normalized values of hsl channels
list($h, $s, $l) = array_map(function ($channel) { list($h, $s, $l) = array_map(fn(ColorChannelInterface $channel) => $channel->normalize(), $color->channels());
return $channel->normalize();
}, $color->channels());
$v = $l + $s * min($l, 1 - $l); $v = $l + $s * min($l, 1 - $l);
$s = ($v == 0) ? 0 : 2 * (1 - $l / $v); $s = ($v == 0) ? 0 : 2 * (1 - $l / $v);

View File

@@ -8,6 +8,7 @@ use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Hsl\Color as HslColor; use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Cmyk\Color as CmykColor; use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -32,11 +33,11 @@ class Colorspace implements ColorspaceInterface
*/ */
public function colorFromNormalized(array $normalized): ColorInterface public function colorFromNormalized(array $normalized): ColorInterface
{ {
$values = array_map(function ($classname, $value_normalized) { return new Color(...array_map(
return (new $classname(normalized: $value_normalized))->value(); fn($classname, $value_normalized) => (new $classname(normalized: $value_normalized))->value(),
}, self::$channels, $normalized); self::$channels,
$normalized,
return new Color(...$values); ));
} }
/** /**
@@ -98,9 +99,7 @@ class Colorspace implements ColorspaceInterface
}; };
// add to each value // add to each value
$values = array_map(function ($value) use ($color, $chroma) { $values = array_map(fn($value) => $value + $color->value()->normalize() - $chroma, $values);
return $value + $color->value()->normalize() - $chroma;
}, $values);
array_push($values, 1); // append alpha channel value array_push($values, 1); // append alpha channel value
@@ -119,9 +118,10 @@ class Colorspace implements ColorspaceInterface
} }
// normalized values of hsl channels // normalized values of hsl channels
list($h, $s, $l) = array_map(function ($channel) { list($h, $s, $l) = array_map(
return $channel->normalize(); fn(ColorChannelInterface $channel) => $channel->normalize(),
}, $color->channels()); $color->channels()
);
$c = (1 - abs(2 * $l - 1)) * $s; $c = (1 - abs(2 * $l - 1)) * $s;
$x = $c * (1 - abs(fmod($h * 6, 2) - 1)); $x = $c * (1 - abs(fmod($h * 6, 2) - 1));
@@ -136,9 +136,7 @@ class Colorspace implements ColorspaceInterface
default => [$c, 0, $x], default => [$c, 0, $x],
}; };
$values = array_map(function ($value) use ($m) { $values = array_map(fn($value) => $value + $m, $values);
return $value + $m;
}, $values);
array_push($values, 1); // append alpha channel value array_push($values, 1); // append alpha channel value

View File

@@ -86,9 +86,10 @@ enum Format
*/ */
public function mediaTypes(): array public function mediaTypes(): array
{ {
return array_filter(MediaType::cases(), function ($mediaType) { return array_filter(
return $mediaType->format() === $this; MediaType::cases(),
}); fn(MediaType $mediaType) => $mediaType->format() === $this
);
} }
/** /**
@@ -110,9 +111,10 @@ enum Format
*/ */
public function fileExtensions(): array public function fileExtensions(): array
{ {
return array_filter(FileExtension::cases(), function ($fileExtension) { return array_filter(
return $fileExtension->format() === $this; FileExtension::cases(),
}); fn(FileExtension $fileExtension) => $fileExtension->format() === $this
);
} }
/** /**
@@ -153,7 +155,7 @@ enum Format
$reflectionClass = new ReflectionClass($classname); $reflectionClass = new ReflectionClass($classname);
if ($constructor = $reflectionClass->getConstructor()) { if ($constructor = $reflectionClass->getConstructor()) {
$parameters = array_map( $parameters = array_map(
fn ($parameter) => $parameter->getName(), fn($parameter) => $parameter->getName(),
$constructor->getParameters(), $constructor->getParameters(),
); );
} }
@@ -161,7 +163,7 @@ enum Format
// filter out unavailable options of target encoder // filter out unavailable options of target encoder
$options = array_filter( $options = array_filter(
$options, $options,
fn ($key) => in_array($key, $parameters), fn($key) => in_array($key, $parameters),
ARRAY_FILTER_USE_KEY, ARRAY_FILTER_USE_KEY,
); );