1
0
mirror of https://github.com/Intervention/image.git synced 2025-07-31 11:00:12 +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 = [])
{
//
}
/**
@@ -181,11 +182,13 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable
*/
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
{
$items = array_filter($this->items, function ($item) use ($callback) {
return $callback($item);
});
return new self($items);
return new self(
array_filter(
$this->items,
fn($item) => $callback($item),
)
);
}
/**

View File

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

View File

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

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\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -32,11 +33,11 @@ class Colorspace implements ColorspaceInterface
*/
public function colorFromNormalized(array $normalized): ColorInterface
{
$values = array_map(function ($classname, $value_normalized) {
return (new $classname(normalized: $value_normalized))->value();
}, self::$channels, $normalized);
return new Color(...$values);
return new Color(...array_map(
fn($classname, $value_normalized) => (new $classname(normalized: $value_normalized))->value(),
self::$channels,
$normalized
));
}
/**
@@ -66,9 +67,10 @@ class Colorspace implements ColorspaceInterface
}
// normalized values of rgb channels
$values = array_map(function ($channel) {
return $channel->normalize();
}, $color->channels());
$values = array_map(
fn(ColorChannelInterface $channel) => $channel->normalize(),
$color->channels(),
);
// take only RGB
$values = array_slice($values, 0, 3);
@@ -116,9 +118,10 @@ class Colorspace implements ColorspaceInterface
}
// normalized values of hsv channels
list($h, $s, $v) = array_map(function ($channel) {
return $channel->normalize();
}, $color->channels());
list($h, $s, $v) = array_map(
fn(ColorChannelInterface $channel) => $channel->normalize(),
$color->channels(),
);
// calculate Luminance
$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\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -32,11 +33,11 @@ class Colorspace implements ColorspaceInterface
*/
public function colorFromNormalized(array $normalized): ColorInterface
{
$values = array_map(function ($classname, $value_normalized) {
return (new $classname(normalized: $value_normalized))->value();
}, self::$channels, $normalized);
return new Color(...$values);
return new Color(...array_map(
fn($classname, $value_normalized) => (new $classname(normalized: $value_normalized))->value(),
self::$channels,
$normalized
));
}
/**
@@ -66,9 +67,7 @@ class Colorspace implements ColorspaceInterface
}
// normalized values of rgb channels
$values = array_map(function ($channel) {
return $channel->normalize();
}, $color->channels());
$values = array_map(fn(ColorChannelInterface $channel) => $channel->normalize(), $color->channels());
// take only RGB
$values = array_slice($values, 0, 3);
@@ -116,9 +115,7 @@ class Colorspace implements ColorspaceInterface
}
// normalized values of hsl channels
list($h, $s, $l) = array_map(function ($channel) {
return $channel->normalize();
}, $color->channels());
list($h, $s, $l) = array_map(fn(ColorChannelInterface $channel) => $channel->normalize(), $color->channels());
$v = $l + $s * min($l, 1 - $l);
$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\Cmyk\Color as CmykColor;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -32,11 +33,11 @@ class Colorspace implements ColorspaceInterface
*/
public function colorFromNormalized(array $normalized): ColorInterface
{
$values = array_map(function ($classname, $value_normalized) {
return (new $classname(normalized: $value_normalized))->value();
}, self::$channels, $normalized);
return new Color(...$values);
return new Color(...array_map(
fn($classname, $value_normalized) => (new $classname(normalized: $value_normalized))->value(),
self::$channels,
$normalized,
));
}
/**
@@ -98,9 +99,7 @@ class Colorspace implements ColorspaceInterface
};
// add to each value
$values = array_map(function ($value) use ($color, $chroma) {
return $value + $color->value()->normalize() - $chroma;
}, $values);
$values = array_map(fn($value) => $value + $color->value()->normalize() - $chroma, $values);
array_push($values, 1); // append alpha channel value
@@ -119,9 +118,10 @@ class Colorspace implements ColorspaceInterface
}
// normalized values of hsl channels
list($h, $s, $l) = array_map(function ($channel) {
return $channel->normalize();
}, $color->channels());
list($h, $s, $l) = array_map(
fn(ColorChannelInterface $channel) => $channel->normalize(),
$color->channels()
);
$c = (1 - abs(2 * $l - 1)) * $s;
$x = $c * (1 - abs(fmod($h * 6, 2) - 1));
@@ -136,9 +136,7 @@ class Colorspace implements ColorspaceInterface
default => [$c, 0, $x],
};
$values = array_map(function ($value) use ($m) {
return $value + $m;
}, $values);
$values = array_map(fn($value) => $value + $m, $values);
array_push($values, 1); // append alpha channel value

View File

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