Skip to content

Commit

Permalink
Merge pull request #205 from TypiCMS/master
Browse files Browse the repository at this point in the history
psr-4 + PHPStan level 5
  • Loading branch information
sdebacker committed May 15, 2022
2 parents 51892e4 + 4cee506 commit b8606d3
Show file tree
Hide file tree
Showing 27 changed files with 115 additions and 71 deletions.
33 changes: 25 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"name": "bkwld/croppa",
"description": "Image thumbnail creation through specially formatted URLs for Laravel",
"keywords": [
"image",
"thumb",
"resize",
"laravel"
],
"homepage": "https://github.com/bkwld/croppa",
"license": "MIT",
"authors": [
{
"name": "Robert Reinhard"
Expand All @@ -9,7 +17,6 @@
"name": "Samuel De Backer"
}
],
"license": "MIT",
"require": {
"php": "^8.0.2",
"illuminate/console": "^9.0",
Expand All @@ -23,26 +30,36 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5.10",
"mockery/mockery": "^1.4.4"
"mockery/mockery": "^1.4.4",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^7.5"
},
"suggest": {
"ext-exif": "Required if you want to have Croppa auto-rotate images from devices like mobile phones based on exif meta data."
},
"autoload": {
"psr-0": {
"Bkwld\\Croppa": "src/"
"psr-4": {
"Bkwld\\Croppa\\": "src"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload-dev": {
"psr-4": {
"Bkwld\\Croppa\\Test\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"extra": {
"laravel": {
"providers": [
"Bkwld\\Croppa\\CroppaServiceProvider"
],
"aliases": {
"Croppa": "Bkwld\\Croppa\\Facade"
"Croppa": "Bkwld\\Croppa\\Facades\\Croppa"
}
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
File renamed without changes.
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
- src

# The level 9 is the highest level
level: 5
6 changes: 1 addition & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Purge extends Command
protected $description = 'Delete all crops';

/**
* @var Bkwld\Croppa\Storage
* @var \Bkwld\Croppa\Storage
*/
protected $storage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function register()
});

// Interact with the disk.
$this->app->singleton(Storage::class, function ($app) {
return new Storage($app, $this->getConfig());
$this->app->singleton(Storage::class, function () {
return new Storage($this->getConfig());
});

// API for use in apps.
Expand All @@ -44,8 +44,8 @@ public function register()

public function boot()
{
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'croppa');
$this->publishes([__DIR__.'/../../config/config.php' => config_path('croppa.php')], 'croppa-config');
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'croppa');
$this->publishes([__DIR__.'/../config/config.php' => config_path('croppa.php')], 'croppa-config');

$this->app['router']
->get('{path}', 'Bkwld\Croppa\Handler@handle')
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 11 additions & 9 deletions src/Bkwld/Croppa/Handler.php → src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
*/
class Handler extends Controller
{
/**
* @var URL
*/
private $url;

/**
* @var Storage
*/
private $storage;

/**
* @var URL
* @var Request
*/
private $url;
private $request;

/**
* @var array
Expand All @@ -42,10 +47,8 @@ public function __construct(URL $url, Storage $storage, Request $request, ?array
* Handles a Croppa style route.
*
* @throws Exception
*
* @return Symfony\Component\HttpFoundation\StreamedResponse
*/
public function handle(string $requestPath)
public function handle(string $requestPath): mixed
{
// Validate the signing token
$token = $this->url->signingToken($requestPath);
Expand Down Expand Up @@ -121,10 +124,6 @@ public function render(string $requestPath): ?string
public function getContentType(string $path): string
{
switch (pathinfo($path, PATHINFO_EXTENSION)) {
case 'jpeg':
case 'jpg':
return 'image/jpeg';

case 'gif':
return 'image/gif';

Expand All @@ -133,6 +132,9 @@ public function getContentType(string $path): string

case 'webp':
return 'image/webp';

default:
return 'image/jpeg';
}
}
}
11 changes: 8 additions & 3 deletions src/Bkwld/Croppa/Helpers.php → src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
class Helpers
{
/**
* @var Bkwld\Croppa\Storage
* @var \Bkwld\Croppa\URL
*/
private $url;

/**
* @var \Bkwld\Croppa\Storage
*/
private $storage;

/**
* @var Bkwld\Croppa\URL
* @var \Bkwld\Croppa\Handler
*/
private $url;
private $handler;

/**
* Dependency injection.
Expand Down
15 changes: 6 additions & 9 deletions src/Bkwld/Croppa/Image.php → src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Image
{
/**
* @var Intervention\Image\Image
* @var \Intervention\Image\Image
*/
private $image;

Expand Down Expand Up @@ -122,10 +122,10 @@ public function trimPerc(array $coords): self
list($x1, $y1, $x2, $y2) = $coords;
$imgWidth = $this->image->width();
$imgHeight = $this->image->height();
$x = round($x1 * $imgWidth);
$y = round($y1 * $imgHeight);
$width = round($x2 * $imgWidth - $x);
$height = round($y2 * $imgHeight - $y);
$x = (int) round($x1 * $imgWidth);
$y = (int) round($y1 * $imgHeight);
$width = (int) round($x2 * $imgWidth - $x);
$height = (int) round($y2 * $imgHeight - $y);
$this->image->crop($width, $height, $x, $y);

return $this;
Expand Down Expand Up @@ -246,11 +246,8 @@ public function pad(?int $width, ?int $height, array $options): self

/**
* Apply filters that have been defined in the config as seperate classes.
*
* @param array $filters Array of filter instances
* @param mixed $options
*/
public function applyFilters($options): self
public function applyFilters(array $options): self
{
if (isset($options['filters']) && is_array($options['filters'])) {
array_map(function ($filter) {
Expand Down
30 changes: 7 additions & 23 deletions src/Bkwld/Croppa/Storage.php → src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,37 @@
/**
* Interact with filesystems.
*/
class Storage
final class Storage
{
/**
* @var Illuminate\Container\Container
*/
private $app;

/**
* @var array
*/
private $config;

/**
* @var FilesystemAdapter
* @var ?FilesystemAdapter
*/
private $cropsDisk;

/**
* @var FilesystemAdapter
* @var ?FilesystemAdapter
*/
private $srcDisk;

/**
* Inject dependencies.
*
* @param Illuminate\Container\Container
* @param null|mixed $app
*/
public function __construct($app = null, ?array $config = null)
public function __construct(?array $config = null)
{
$this->app = $app;
$this->config = $config;
}

/**
* Factory function to create an instance and then "mount" disks.
*
* @param Illuminate\Container\Container
* @param mixed $app
*
* @return Bkwld\Croppa\Storage
*/
public static function make($app, array $config)
public static function make(array $config)
{
return with(new static($app, $config))->mount();
return with(new static($config))->mount();
}

/**
Expand Down Expand Up @@ -135,8 +121,6 @@ public function cropExists(string $path): bool

/**
* Get the src path or throw an exception.
*
* @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function path(string $path): string
{
Expand Down Expand Up @@ -286,7 +270,7 @@ function ($file) use ($filter) {
* Take a an array of results from Flysystem's listContents and get a simpler
* array of paths to the files, relative to the crops_disk.
*/
protected function justPaths(array $files): array
private function justPaths(array $files): array
{
// Reset the indexes to be 0 based, mostly for unit testing
$files = array_values($files);
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/TestDelete.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

namespace Bkwld\Croppa\Test;

use Bkwld\Croppa\Helpers;
use Bkwld\Croppa\Storage;
use Bkwld\Croppa\URL;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\DirectoryListing;
use Mockery;
use PHPUnit\Framework\TestCase;

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/TestListAllCrops.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

namespace Bkwld\Croppa\Test;

use Bkwld\Croppa\Storage;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\DirectoryListing;
use Mockery;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -23,6 +26,8 @@ class TestListAllCrops extends TestCase

public function setUp(): void
{
parent::setUp();

// Mock src dir
$this->src_disk = Mockery::mock(FilesystemAdapter::class)
->shouldReceive('fileExists')->with('01/me.jpg')->andReturn(true)
Expand Down
4 changes: 4 additions & 0 deletions tests/TestResizing.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Bkwld\Croppa\Test;

use Bkwld\Croppa\Image;
use PHPUnit\Framework\TestCase;

Expand All @@ -18,6 +20,8 @@ class TestResizing extends TestCase

public function setUp(): void
{
parent::setUp();

// Make an image
$gd = imagecreate(500, 400);
ob_start();
Expand Down
7 changes: 6 additions & 1 deletion tests/TestTooManyCrops.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

namespace Bkwld\Croppa\Test;

use Bkwld\Croppa\Storage;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\DirectoryListing;
use Mockery;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -15,6 +18,8 @@ class TestTooManyCrops extends TestCase

public function setUp(): void
{
parent::setUp();

// Mock flysystem
$this->dir = Mockery::mock(FilesystemAdapter::class)
->shouldReceive('listContents')
Expand Down Expand Up @@ -50,7 +55,7 @@ public function testAcceptableNumber()

public function testTooMany()
{
$storage = new Storage(null, ['max_crops' => 3]);
$storage = new Storage(['max_crops' => 3]);
$storage->setCropsDisk($this->dir);
$this->assertTrue($storage->tooManyCrops('me.jpg'));
}
Expand Down

0 comments on commit b8606d3

Please sign in to comment.