Skip to content

Commit

Permalink
Refactor short callbacks to arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Dec 30, 2024
1 parent c66fa7d commit 2a62d49
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 67 deletions.
22 changes: 13 additions & 9 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable
*/
public function __construct(protected array $items = [])
{
//
}

/**
Expand Down Expand Up @@ -181,11 +182,13 @@ public function get(int|string $query, $default = null): mixed
*/
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,
)
);
}

/**
Expand All @@ -196,11 +199,12 @@ public function map(callable $callback): self
*/
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),
)
);
}

/**
Expand Down
21 changes: 12 additions & 9 deletions src/Colors/AbstractColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function channels(): array
*/
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.');
Expand All @@ -54,9 +55,10 @@ public function channel(string $classname): ColorChannelInterface
*/
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(),
);
}

/**
Expand All @@ -66,9 +68,10 @@ public function normalize(): array
*/
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()
);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Colors/Cmyk/Colorspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}

/**
Expand Down
25 changes: 14 additions & 11 deletions src/Colors/Hsl/Colorspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;

Expand All @@ -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
));
}

/**
Expand Down Expand Up @@ -66,9 +67,10 @@ protected function importRgbColor(ColorInterface $color): ColorInterface
}

// 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);
Expand Down Expand Up @@ -116,9 +118,10 @@ protected function importHsvColor(ColorInterface $color): ColorInterface
}

// 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;
Expand Down
19 changes: 8 additions & 11 deletions src/Colors/Hsv/Colorspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;

Expand All @@ -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
));
}

/**
Expand Down Expand Up @@ -66,9 +67,7 @@ protected function importRgbColor(ColorInterface $color): ColorInterface
}

// 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);
Expand Down Expand Up @@ -116,9 +115,7 @@ protected function importHslColor(ColorInterface $color): ColorInterface
}

// 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);
Expand Down
26 changes: 12 additions & 14 deletions src/Colors/Rgb/Colorspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;

Expand All @@ -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,
));
}

/**
Expand Down Expand Up @@ -98,9 +99,7 @@ protected function importHsvColor(ColorInterface $color): ColorInterface
};

// 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

Expand All @@ -119,9 +118,10 @@ protected function importHslColor(ColorInterface $color): ColorInterface
}

// 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));
Expand All @@ -136,9 +136,7 @@ protected function importHslColor(ColorInterface $color): ColorInterface
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

Expand Down
18 changes: 10 additions & 8 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ public static function tryCreate(string|self|MediaType|FileExtension $identifier
*/
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
);
}

/**
Expand All @@ -110,9 +111,10 @@ public function mediaType(): MediaType
*/
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
);
}

/**
Expand Down Expand Up @@ -153,15 +155,15 @@ public function encoder(mixed ...$options): EncoderInterface
$reflectionClass = new ReflectionClass($classname);
if ($constructor = $reflectionClass->getConstructor()) {
$parameters = array_map(
fn ($parameter) => $parameter->getName(),
fn($parameter) => $parameter->getName(),
$constructor->getParameters(),
);
}

// 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,
);

Expand Down

0 comments on commit 2a62d49

Please sign in to comment.