Skip to content

Commit

Permalink
Merge pull request #62 from m4n1ok/typings-update
Browse files Browse the repository at this point in the history
Typings updates
  • Loading branch information
mrdoinel authored Sep 13, 2024
2 parents fd18822 + 20301b7 commit 7075d45
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 81 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
}
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0"
"friendsofphp/php-cs-fixer": "^3.0",
"larastan/larastan": "^2.0",
"phpstan/phpstan": "^1.12"
},
"scripts": {
"lint": [
"vendor/bin/phpstan analyse -c phpstan.neon src"
]
}
}
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 5

paths:
- src

universalObjectCratesClasses:

reportUnmatchedIgnoredErrors: false
2 changes: 1 addition & 1 deletion src/Facades/TwillImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TwillImage extends Facade
{
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return 'twill.image';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/GlideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class GlideController
{
public function __invoke($path, Application $app)
public function __invoke(string $path, Application $app)
{
return $app->make(Glide::class)->render($path);
}
Expand Down
71 changes: 44 additions & 27 deletions src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use A17\Twill\Image\Facades\TwillImage;
use A17\Twill\Image\Services\MediaSource;
use A17\Twill\Image\Services\ImageColumns;
use A17\Twill\Services\MediaLibrary\ImageService;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Support\Arrayable;
use A17\Twill\Image\Exceptions\ImageException;
use A17\Twill\Services\MediaLibrary\ImageServiceInterface;
use Illuminate\Foundation\Application;

class Image implements Arrayable
{
Expand Down Expand Up @@ -59,16 +62,28 @@ class Image implements Arrayable
protected $srcSetWidths = [];

/**
* @var string|ImageServiceInterface ImageService instance or class name
* ImageService instance or class name
*
* @var string|ImageServiceInterface
*/
protected $service;

/**
* @var ImageColumns|mixed
*/
private $columnsService;

/**
* @var MediaSource|mixed
*/
private $mediaSourceService;

/**
* @param object|Model $object
* @param string $role
* @param null|Media $media
*/
public function __construct($object, $role, $media = null)
public function __construct($object, string $role, ?Media $media = null)
{
$this->object = $object;

Expand All @@ -85,26 +100,26 @@ public function __construct($object, $role, $media = null)

/**
* @return MediaSource
* @throws ImageException
*/
private function mediaSourceService()
private function mediaSourceService(): MediaSource
{
return isset($this->mediaSourceService)
? $this->mediaSourceService
: $this->mediaSourceService = new MediaSource(
$this->object,
$this->role,
$this->media,
$this->service,
);
return $this->mediaSourceService ?? $this->mediaSourceService = new MediaSource(
$this->object,
$this->role,
$this->media,
$this->service
);
}

/**
* Pick a preset from the configuration file or pass an array with the image configuration
*
* @param array|string $preset
* @return $this
* @throws ImageException
*/
public function preset($preset)
public function preset($preset): Image
{
if (is_array($preset)) {
$this->applyPreset($preset);
Expand Down Expand Up @@ -139,9 +154,8 @@ protected function mediaQueryColumns($args)
* Set the list of srcset width to generate
*
* @param int[] $widths
* @return $this
*/
public function srcSetWidths($widths)
public function srcSetWidths(array $widths)
{
$this->srcSetWidths = $widths;
}
Expand All @@ -152,7 +166,7 @@ public function srcSetWidths($widths)
* @param string $crop
* @return $this
*/
public function crop($crop)
public function crop($crop): Image
{
$this->crop = $crop;

Expand All @@ -165,7 +179,7 @@ public function crop($crop)
* @param int $width
* @return $this
*/
public function width($width)
public function width(int $width): Image
{
$this->width = $width;

Expand All @@ -178,7 +192,7 @@ public function width($width)
* @param int $height
* @return $this
*/
public function height($height)
public function height(int $height): Image
{
$this->height = $height;

Expand All @@ -191,7 +205,7 @@ public function height($height)
* @param string $sizes
* @return $this
*/
public function sizes($sizes)
public function sizes(string $sizes): Image
{
$this->sizes = $sizes;

Expand All @@ -204,7 +218,7 @@ public function sizes($sizes)
* @param array $sources
* @return $this
*/
public function sources($sources = [])
public function sources(array $sources = []): Image
{
$this->sources = $sources;

Expand All @@ -215,10 +229,10 @@ public function sources($sources = [])
* Set the ImageService to use instead of the one provided
* by the service container
*
* @param array $service
* @param string|ImageServiceInterface $service
* @return $this
*/
public function service($service)
public function service($service): Image
{
$this->service = $service;

Expand All @@ -228,14 +242,13 @@ public function service($service)
/**
* Set alternative sources for the media.
*
* @param array $sources
* @return $this
* @throws ImageException
*/
public function generateSources()
public function generateSources(): array
{
$sources = [];

foreach ($this->sources ?? [] as $source) {
foreach ($this->sources as $source) {
if (!isset($source['media_query']) && !isset($source['mediaQuery']) && !isset($source['columns'])) {
throw new ImageException("Media query is mandatory in sources.");
}
Expand All @@ -252,7 +265,7 @@ public function generateSources()
$source['crop'],
$source['width'] ?? null,
$source['height'] ?? null,
$source['srcSetWidths'] ?? [],
$source['srcSetWidths'] ?? []
)->toArray()
];
}
Expand All @@ -267,10 +280,14 @@ public function generateSources()
*/
public function render($overrides = [])
{
/* @phpstan-ignore-next-line */
return TwillImage::render($this, $overrides);
}

public function toArray()
/**
* @throws ImageException
*/
public function toArray(): array
{
$arr = [
"image" => $this->mediaSourceService()->generate(
Expand Down
19 changes: 12 additions & 7 deletions src/Models/StaticImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace A17\Twill\Image\Models;

use A17\Twill\Models\Model;
use A17\Twill\Image\Models\Image;
use A17\Twill\Image\Models\Image as TwillImageModel;
use A17\Twill\Models\Behaviors\HasMedias;
use A17\Twill\Image\Exceptions\ImageException;

Expand All @@ -13,12 +13,14 @@ class StaticImage extends Model

protected $fillable = [];

public $mediasParams = [];

public static $role = 'static-image';

public static function makeFromSrc($args)
/**
* @throws ImageException
*/
public static function makeFromSrc($args): TwillImageModel
{
/* @phpstan-ignore-next-line */
$model = self::make();

$role = self::$role;
Expand All @@ -39,7 +41,8 @@ public static function makeFromSrc($args)
'alt' => $args['alt'] ?? null,
]);

if (!empty($preset['sources']) && $sources = $preset['sources']) {
if (!empty($preset['sources'])) {
$sources = $preset['sources'] ?? [];
foreach ($sources as $source) {
$model->makeMedia([
'src' => self::getFile($files, $source['crop']),
Expand Down Expand Up @@ -114,6 +117,7 @@ public function makeMedia($args)

$cropData = $this->calcCrop($width, $height, $ratio);

/* @phpstan-ignore-next-line */
$media = \A17\Twill\Models\Media::make([
'uuid' => $uuid,
'filename' => basename($uuid),
Expand All @@ -131,15 +135,16 @@ public function makeMedia($args)
$this,
$data,
config('twill.mediables_table', 'twill_mediables'),
true,
true
);

$media->setRelation('pivot', $pivot);

/* @phpstan-ignore-next-line */
$this->medias->add($media);
}

private function getInputSize($uuid)
private function getInputSize($uuid): array
{
$file_path = implode('/', [
rtrim(config('twill-image.static_local_path'), '/'),
Expand Down
12 changes: 6 additions & 6 deletions src/Services/ImageStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ class ImageStyles
* Set up the service to generate view inline styles for the wrapper, main image and placeholder elements
*
* @param array $styles
* @param array $classes
* @param string $classes
* @return void
*/
public function setup($styles = [], $classes = '')
public function setup(array $styles = [], string $classes = '')
{
$this->mode = config('twill-image.mode');
$this->setBaseStyling($styles, $classes);
}

/**
* Return inline stylesand classes for the wrapper element
* Return inline styles and classes for the wrapper element
*
* @return array
*/
public function wrapper()
public function wrapper(): array
{
$styles = $this->inlineStyles['wrapper'] ?? [];
$classes = $this->classes['wrapper'] ?? [];
Expand All @@ -46,11 +46,11 @@ public function wrapper()
}

/**
* Return inline stylesand classes for the placeholder element
* Return inline styles and classes for the placeholder element
*
* @return array
*/
public function placeholder()
public function placeholder(): array
{
$styles = array_merge(
$this->inlineStyles['main'] ?? [],
Expand Down
Loading

0 comments on commit 7075d45

Please sign in to comment.