Skip to content

Commit

Permalink
refactor: PHPstan code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 28, 2024
1 parent e12d981 commit 460389f
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 132 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ parameters:
level: 5
paths:
- src
excludePaths:
# Ignore library code
- src/lib/
17 changes: 11 additions & 6 deletions src/ImageOptimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
use nystudio107\imageoptimize\services\ServicesTrait;
use nystudio107\imageoptimize\utilities\ImageOptimizeUtility;
use nystudio107\imageoptimize\variables\ImageOptimizeVariable;
use Twig\Error\LoaderError;
use yii\base\Event;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use function function_exists;

/** @noinspection MissingPropertyAnnotationsInspection */

Expand Down Expand Up @@ -206,13 +208,13 @@ public function settingsHtml()
'image-optimize/settings/_settings.twig',
[
'settings' => $settings,
'gdInstalled' => \function_exists('imagecreatefromjpeg'),
'gdInstalled' => function_exists('imagecreatefromjpeg'),
'imageTransformTypeOptions' => $imageTransformTypeOptions,
'allImageTransformTypes' => $allImageTransformTypes,
'imageTransform' => ImageOptimize::$plugin->transformMethod,
]
);
} catch (\Twig\Error\LoaderError $e) {
} catch (LoaderError $e) {
Craft::error($e->getMessage(), __METHOD__);
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
Expand All @@ -237,6 +239,7 @@ protected function createSettingsModel()
*/
protected function setImageTransformComponent()
{
/** @var Settings $settings */
$settings = $this->getSettings();
$definition = array_merge(
$settings->imageTransformTypeSettings[$settings->transformClass] ?? [],
Expand Down Expand Up @@ -484,8 +487,8 @@ function(FieldEvent $event) {
'Fields::EVENT_AFTER_SAVE_FIELD',
__METHOD__
);
/** @var Settings $settings */
$settings = $this->getSettings();
/** @var Field $field */
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
$this->checkForOptimizedImagesField($event);
}
Expand All @@ -502,6 +505,7 @@ function(PluginEvent $event) {
'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS',
__METHOD__
);
/** @var Settings $settings */
$settings = $this->getSettings();
if ($settings->automaticallyResaveImageVariants) {
// After they have changed the settings, resave all of the assets
Expand All @@ -520,6 +524,7 @@ function(VolumeEvent $event) {
'Volumes::EVENT_AFTER_SAVE_VOLUME',
__METHOD__
);
/** @var Settings $settings */
$settings = $this->getSettings();
// Only worry about this volume if it's not new
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
Expand Down Expand Up @@ -631,17 +636,17 @@ protected function customFrontendRoutes(): array
*
* @param FieldEvent $event
*
* @throws \yii\base\InvalidConfigException
* @throws InvalidConfigException
*/
protected function checkForOptimizedImagesField(FieldEvent $event)
{
$thisField = $event->field;
if ($thisField instanceof OptimizedImages) {
$volumes = Craft::$app->getVolumes()->getAllVolumes();
/** @var Volume $volume */
foreach ($volumes as $volume) {
$needToReSave = false;
/** @var FieldLayout $fieldLayout */
/** @var Volume $volume */
/** @var ?FieldLayout $fieldLayout */
$fieldLayout = $volume->getFieldLayout();
// Loop through the fields in the layout to see if it contains our field
if ($fieldLayout) {
Expand Down
12 changes: 8 additions & 4 deletions src/console/controllers/OptimizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
namespace nystudio107\imageoptimize\console\controllers;

use Craft;
use craft\base\Field;
use craft\base\Volume;
use craft\helpers\App;
use craft\queue\QueueInterface;
use nystudio107\imageoptimize\ImageOptimize;
use yii\base\InvalidConfigException;
use yii\console\Controller;
use yii\queue\redis\Queue as RedisQueue;

Expand Down Expand Up @@ -68,7 +70,7 @@ public function options($actionID): array
*
* @param string|null $volumeHandle
*
* @throws \yii\base\InvalidConfigException
* @throws InvalidConfigException
*/
public function actionCreate($volumeHandle = null)
{
Expand All @@ -79,6 +81,7 @@ public function actionCreate($volumeHandle = null)

$fieldId = null;
if ($this->field !== null) {
/** @var Field $field */
$field = Craft::$app->getFields()->getFieldByHandle($this->field);
if ($field !== null) {
$fieldId = $field->id;
Expand Down Expand Up @@ -130,11 +133,12 @@ private function runCraftQueue()
{
// This might take a while
App::maxPowerCaptain();
/** @var QueueInterface|RedisQueue $queue */
$queue = Craft::$app->getQueue();
if ($queue instanceof QueueInterface) {
$queue->run();
} elseif ($queue instanceof RedisQueue) {
if ($queue instanceof RedisQueue) {
$queue->run(false);
} elseif ($queue instanceof QueueInterface) {
$queue->run();
}
}
}
6 changes: 4 additions & 2 deletions src/fields/OptimizedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use nystudio107\imageoptimize\gql\types\generators\OptimizedImagesGenerator;
use nystudio107\imageoptimize\ImageOptimize;
use nystudio107\imageoptimize\models\OptimizedImage;
use nystudio107\imageoptimize\models\Settings;
use ReflectionClass;
use ReflectionException;
use Twig\Error\LoaderError;
Expand Down Expand Up @@ -140,6 +141,7 @@ public function init()

// Handle cases where the plugin has been uninstalled
if (ImageOptimize::$plugin !== null) {
/** @var ?Settings $settings */
$settings = ImageOptimize::$plugin->getSettings();
if ($settings) {
if (empty($this->variants)) {
Expand Down Expand Up @@ -267,7 +269,7 @@ public function normalizeValue($value, ElementInterface $asset = null)
$model = $value;
} else {
// Just create a new empty model
$model = new OptimizedImage(null);
$model = new OptimizedImage([]);
}

return $model;
Expand Down Expand Up @@ -511,7 +513,7 @@ protected function getFieldVolumeInfo($fieldHandle): array
protected function volumeHasField(Volume $volume, string $fieldHandle): bool
{
$result = false;
/** @var FieldLayout $fieldLayout */
/** @var ?FieldLayout $fieldLayout */
$fieldLayout = $volume->getFieldLayout();
// Loop through the fields in the layout to see if there is an OptimizedImages field
if ($fieldLayout) {
Expand Down
27 changes: 0 additions & 27 deletions src/gql/types/OptimizedImagesType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
namespace nystudio107\imageoptimize\gql\types;

use craft\gql\base\ObjectType;

use GraphQL\Type\Definition\ResolveInfo;

use nystudio107\imageoptimize\models\OptimizedImage;

/**
Expand All @@ -32,104 +30,79 @@ protected function resolve($source, $arguments, $context, ResolveInfo $resolveIn
// Special-case the `src` field with arguments
case 'src':
$width = $arguments['width'] ?? 0;

return $source->src($width);
break;

// Special-case the `srcWebp` field with arguments
case 'srcWebp':
$width = $arguments['width'] ?? 0;

return $source->srcWebp($width);
break;

// Special-case the `srcset` field with arguments
case 'srcset':
$dpr = $arguments['dpr'] ?? false;

return $source->srcset($dpr);
break;

// Special-case the `srcsetMinWidth` field with arguments
case 'srcsetMinWidth':
$width = $arguments['width'] ?? 0;
$dpr = $arguments['dpr'] ?? false;

return $source->srcsetMinWidth($width, $dpr);
break;

// Special-case the `srcsetMaxWidth` field with arguments
case 'srcsetMaxWidth':
$width = $arguments['width'] ?? 0;
$dpr = $arguments['dpr'] ?? false;

return $source->srcsetMaxWidth($width, $dpr);
break;

// Special-case the `srcsetWebp` field with arguments
case 'srcsetWebp':
$dpr = $arguments['dpr'] ?? false;

return $source->srcsetWebp($dpr);
break;

// Special-case the `srcsetMinWidthWebp` field with arguments
case 'srcsetMinWidthWebp':
$width = $arguments['width'] ?? 0;
$dpr = $arguments['dpr'] ?? false;

return $source->srcsetMinWidthWebp($width, $dpr);
break;

// Special-case the `srcsetMaxWidthWebp` field with arguments
case 'srcsetMaxWidthWebp':
$width = $arguments['width'] ?? 0;
$dpr = $arguments['dpr'] ?? false;

return $source->srcsetMaxWidthWebp($width, $dpr);
break;

// Special-case the `maxSrcsetWidth` field
case 'maxSrcsetWidth':
return $source->maxSrcsetWidth();
break;

// Special-case the `placeholderImage` field
case 'placeholderImage':
return $source->placeholderImage();
break;

// Special-case the `placeholderBox` field
case 'placeholderBox':
$color = $arguments['color'] ?? null;

return $source->placeholderBox($color);
break;

// Special-case the `placeholderSilhouette` field
case 'placeholderSilhouette':
return $source->placeholderSilhouette();
break;

// Special-case the `srcUrls` field
case 'srcUrls':
$result = [];
foreach ($source->optimizedImageUrls as $width => $url) {
$result[] = ['width' => $width, 'url' => $url];
}

return $result;
break;

// Special-case the `colorPaletteRgb` field
case 'colorPaletteRgb':
return $source->colorPaletteRgb();
break;

// Default to just returning the field value
default:
return $source[$fieldName];
break;
}
}
}
11 changes: 6 additions & 5 deletions src/helpers/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use Craft;
use craft\errors\ImageException;
use craft\helpers\FileHelper;

use Imagine\Gd\Imagine as GdImagine;
use Imagine\Imagick\Imagine as ImagickImagine;
use Throwable;
use yii\base\InvalidConfigException;

/**
* @author nystudio107
Expand All @@ -33,7 +34,7 @@ class Image
* @return bool
*
* @throws ImageException
* @throws \yii\base\InvalidConfigException
* @throws InvalidConfigException
*/
public static function getIsAnimatedGif(string $path): bool
{
Expand Down Expand Up @@ -61,7 +62,7 @@ public static function getIsAnimatedGif(string $path): bool
if ($imageService->getIsGd()) {
return false;
}

if (!is_file($path)) {
Craft::error('Tried to load an image at ' . $path . ', but the file does not exist.', __METHOD__);
throw new ImageException(Craft::t('app', 'No file exists at the given path.'));
Expand All @@ -87,7 +88,7 @@ public static function getIsAnimatedGif(string $path): bool

try {
$image = $instance->open($path);
} catch (\Throwable $e) {
} catch (Throwable $e) {
throw new ImageException(Craft::t(
'app',
'The file “{path}” does not appear to be an image.',
Expand All @@ -97,6 +98,6 @@ public static function getIsAnimatedGif(string $path): bool

$extension = pathinfo($path, PATHINFO_EXTENSION);

return $extension === 'gif' && $image->layers();
return $extension === 'gif' && $image->layers()->count() > 1;
}
}
6 changes: 3 additions & 3 deletions src/imagetransforms/ImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
namespace nystudio107\imageoptimize\imagetransforms;

use Craft;

use craft\base\SavableComponent;
use craft\elements\Asset;
use craft\helpers\FileHelper;
use craft\helpers\StringHelper;
use nystudio107\imageoptimize\helpers\UrlHelper;
use nystudio107\imageoptimize\ImageOptimize;
use ReflectionClass;

/**
* @author nystudio107
Expand Down Expand Up @@ -47,7 +47,7 @@ public static function displayName(): string
*/
public static function getTemplatesRoot(): array
{
$reflect = new \ReflectionClass(static::class);
$reflect = new ReflectionClass(static::class);
$classPath = FileHelper::normalizePath(
dirname($reflect->getFileName())
. '/../templates'
Expand Down Expand Up @@ -148,7 +148,7 @@ public function appendExtension($pathOrUrl, $extension): string
{
$path = $this->decomposeUrl($pathOrUrl);
$path_parts = pathinfo($path['path']);
$new_path = ($path_parts['filename'] ?? '') . '.' . ($path_parts['extension'] ?? '') . $extension;
$new_path = ($path_parts['filename']) . '.' . ($path_parts['extension'] ?? '') . $extension;
if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') {
$dirname = $path_parts['dirname'];
$dirname = $dirname === '/' ? '' : $dirname;
Expand Down
4 changes: 1 addition & 3 deletions src/jobs/ResaveOptimizedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use Craft;
use craft\base\ElementInterface;

use craft\base\Field;
use craft\console\Application as ConsoleApplication;
use craft\db\Paginator;
Expand All @@ -22,7 +21,6 @@
use craft\queue\BaseJob;
use nystudio107\imageoptimize\fields\OptimizedImages as OptimizedImagesField;
use nystudio107\imageoptimize\ImageOptimize;

use yii\base\Exception;

/**
Expand Down Expand Up @@ -106,7 +104,7 @@ public function execute($queue)
$layout = $element->getFieldLayout();
if ($layout !== null) {
$fields = $layout->getFields();
/** @var $field Field */
/** @var Field $field */
foreach ($fields as $field) {
if ($field instanceof OptimizedImagesField && $element instanceof Asset) {
if ($this->fieldId === null || $field->id == $this->fieldId) {
Expand Down
Loading

0 comments on commit 460389f

Please sign in to comment.