Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x improve typing #381

Merged
merged 2 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
->setRules([
'@Symfony' => true,
'phpdoc_annotation_without_dot' => false,
'nullable_type_declaration_for_default_null_value' => [
'use_nullable_type_declaration' => true,
],
'phpdoc_to_comment' => [
'ignored_tags' => ['psalm-suppress'],
],
])
->setFinder($finder)
;
5 changes: 0 additions & 5 deletions psalm-baseline.xml

This file was deleted.

3 changes: 1 addition & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
Expand Down
20 changes: 7 additions & 13 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ class Api implements ApiInterface
{
/**
* Intervention image manager.
*
* @var ImageManager
*/
protected $imageManager;
protected ImageManager $imageManager;

/**
* Collection of manipulators.
*
* @var ManipulatorInterface[]
*/
protected $manipulators;
protected array $manipulators;

/**
* Create API instance.
Expand All @@ -37,10 +35,8 @@ public function __construct(ImageManager $imageManager, array $manipulators)
* Set the image manager.
*
* @param ImageManager $imageManager Intervention image manager.
*
* @return void
*/
public function setImageManager(ImageManager $imageManager)
public function setImageManager(ImageManager $imageManager): void
{
$this->imageManager = $imageManager;
}
Expand All @@ -50,7 +46,7 @@ public function setImageManager(ImageManager $imageManager)
*
* @return ImageManager Intervention image manager.
*/
public function getImageManager()
public function getImageManager(): ImageManager
{
return $this->imageManager;
}
Expand All @@ -59,10 +55,8 @@ public function getImageManager()
* Set the manipulators.
*
* @param ManipulatorInterface[] $manipulators Collection of manipulators.
*
* @return void
*/
public function setManipulators(array $manipulators)
public function setManipulators(array $manipulators): void
{
foreach ($manipulators as $manipulator) {
if (!($manipulator instanceof ManipulatorInterface)) {
Expand All @@ -78,7 +72,7 @@ public function setManipulators(array $manipulators)
*
* @return array Collection of manipulators.
*/
public function getManipulators()
public function getManipulators(): array
{
return $this->manipulators;
}
Expand All @@ -91,7 +85,7 @@ public function getManipulators()
*
* @return string Manipulated image binary data.
*/
public function run($source, array $params)
public function run(string $source, array $params): string
{
$image = $this->imageManager->read($source);

Expand Down
2 changes: 1 addition & 1 deletion src/Api/ApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface ApiInterface
*
* @return string Manipulated image binary data.
*/
public function run($source, array $params);
public function run(string $source, array $params): string;
}
2 changes: 1 addition & 1 deletion src/Manipulators/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use League\Glide\Manipulators\Helpers\Color;

/**
* @property string $bg
* @property string|null $bg
*/
class Background extends BaseManipulator
{
Expand Down
6 changes: 2 additions & 4 deletions src/Manipulators/BaseManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ abstract class BaseManipulator implements ManipulatorInterface
{
/**
* The manipulation params.
*
* @var array
*/
public $params = [];
public array $params = [];

/**
* Set the manipulation params.
Expand All @@ -32,7 +30,7 @@ public function setParams(array $params)
*
* @param string $name The manipulation name.
*
* @return string The manipulation value.
* @return mixed The manipulation value.
*/
public function __get($name)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Manipulators/Blur.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public function run(ImageInterface $image): ImageInterface
*
* @return int|null The resolved blur amount.
*/
public function getBlur()
public function getBlur(): ?int
{
if (!is_numeric($this->blur)) {
return;
return null;
}

if ($this->blur < 0 or $this->blur > 100) {
return;
return null;
}

return (int) $this->blur;
Expand Down
16 changes: 8 additions & 8 deletions src/Manipulators/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getBorder(ImageInterface $image): ?array
$color = $this->getColor(isset($values[1]) ? $values[1] : 'ffffff');
$method = $this->getMethod(isset($values[2]) ? $values[2] : 'overlay');

if ($width) {
if (null !== $width) {
return [$width, $color, $method];
}

Expand All @@ -78,7 +78,7 @@ public function getBorder(ImageInterface $image): ?array
*
* @return float|null The resolved border width.
*/
public function getWidth(ImageInterface $image, $dpr, $width): ?float
public function getWidth(ImageInterface $image, float $dpr, string $width): ?float
{
return (new Dimension($image, $dpr))->get($width);
}
Expand All @@ -90,7 +90,7 @@ public function getWidth(ImageInterface $image, $dpr, $width): ?float
*
* @return string The formatted color.
*/
public function getColor(string $color)
public function getColor(string $color): string
{
return (new Color($color))->formatted();
}
Expand All @@ -102,7 +102,7 @@ public function getColor(string $color)
*
* @return string The resolved border method.
*/
public function getMethod(string $method)
public function getMethod(string $method): string
{
if (!in_array($method, ['expand', 'shrink', 'overlay'], true)) {
return 'overlay';
Expand All @@ -116,7 +116,7 @@ public function getMethod(string $method)
*
* @return float The device pixel ratio.
*/
public function getDpr()
public function getDpr(): float
{
if (!is_numeric($this->dpr)) {
return 1.0;
Expand All @@ -138,7 +138,7 @@ public function getDpr()
*
* @return ImageInterface The manipulated image.
*/
public function runOverlay(ImageInterface $image, $width, $color): ImageInterface
public function runOverlay(ImageInterface $image, float $width, string $color): ImageInterface
{
return $image->drawRectangle(
(int) round($width / 2),
Expand All @@ -162,7 +162,7 @@ function (RectangleFactory $rectangle) use ($image, $width, $color) {
*
* @return ImageInterface The manipulated image.
*/
public function runShrink(ImageInterface $image, $width, $color): ImageInterface
public function runShrink(ImageInterface $image, float $width, string $color): ImageInterface
{
return $image
->resize(
Expand All @@ -186,7 +186,7 @@ public function runShrink(ImageInterface $image, $width, $color): ImageInterface
*
* @return ImageInterface The manipulated image.
*/
public function runExpand(ImageInterface $image, $width, $color): ImageInterface
public function runExpand(ImageInterface $image, float $width, string $color): ImageInterface
{
return $image->resizeCanvasRelative(
(int) round($width * 2),
Expand Down
6 changes: 3 additions & 3 deletions src/Manipulators/Brightness.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public function run(ImageInterface $image): ImageInterface
*
* @return int|null The resolved brightness amount.
*/
public function getBrightness()
public function getBrightness(): ?int
{
if (null === $this->bri || !preg_match('/^-*[0-9]+$/', $this->bri)) {
return;
return null;
}

if ($this->bri < -100 or $this->bri > 100) {
return;
return null;
}

return (int) $this->bri;
Expand Down
6 changes: 3 additions & 3 deletions src/Manipulators/Contrast.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public function run(ImageInterface $image): ImageInterface
*
* @return int|null The resolved contrast amount.
*/
public function getContrast()
public function getContrast(): ?int
{
if (null === $this->con || !preg_match('/^-*[0-9]+$/', $this->con)) {
return;
return null;
}

if ($this->con < -100 or $this->con > 100) {
return;
return null;
}

return (int) $this->con;
Expand Down
15 changes: 7 additions & 8 deletions src/Manipulators/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public function run(ImageInterface $image): ImageInterface
*
* @return string The resolved format.
*/
public function getFormat(ImageInterface $image)
public function getFormat(ImageInterface $image): string
{
if (array_key_exists($this->fm, static::supportedFormats())) {
return $this->fm;
}

/** @psalm-suppress RiskyTruthyFalsyComparison */
return array_search($image->origin()->mediaType(), static::supportedFormats(), true) ?: 'jpg';
}

Expand All @@ -72,7 +73,7 @@ public function getFormat(ImageInterface $image)
*
* @return array<string,string>
*/
public static function supportedFormats()
public static function supportedFormats(): array
{
return [
'avif' => 'image/avif',
Expand All @@ -90,15 +91,13 @@ public static function supportedFormats()
*
* @return int The resolved quality.
*/
public function getQuality()
public function getQuality(): int
{
$default = 90;

if (!is_numeric($this->q)) {
return $default;
}

if ($this->q < 0 or $this->q > 100) {
if (!is_numeric($this->q)
or $this->q < 0 or $this->q > 100
) {
return $default;
}

Expand Down
7 changes: 5 additions & 2 deletions src/Manipulators/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/
public function run(ImageInterface $image): ImageInterface
{
if ($flip = $this->getFlip()) {
$flip = $this->getFlip();
if (null !== $flip) {
if ('both' === $flip) {
return $image->flip()->flop();
}
Expand All @@ -40,10 +41,12 @@
*
* @return string|null The resolved flip.
*/
public function getFlip()
public function getFlip(): ?string
{
if (in_array($this->flip, ['h', 'v', 'both'], true)) {
return $this->flip;
}

return null;

Check warning on line 50 in src/Manipulators/Flip.php

View check run for this annotation

Codecov / codecov/patch

src/Manipulators/Flip.php#L50

Added line #L50 was not covered by tests
}
}
8 changes: 4 additions & 4 deletions src/Manipulators/Gamma.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function run(ImageInterface $image): ImageInterface
{
$gamma = $this->getGamma();

if ($gamma) {
if (null !== $gamma) {
$image->gamma($gamma);
}

Expand All @@ -32,14 +32,14 @@ public function run(ImageInterface $image): ImageInterface
*
* @return float|null The resolved gamma amount.
*/
public function getGamma()
public function getGamma(): ?float
{
if (null === $this->gam || !preg_match('/^[0-9]\.*[0-9]*$/', $this->gam)) {
return;
return null;
}

if ($this->gam < 0.1 or $this->gam > 9.99) {
return;
return null;
}

return (float) $this->gam;
Expand Down
Loading