Skip to content

Commit

Permalink
psr4 + PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed May 15, 2022
1 parent 70bc676 commit 4cee506
Show file tree
Hide file tree
Showing 25 changed files with 69 additions and 60 deletions.
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,26 @@
},
"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-4": {
"Bkwld\\Croppa\\": "src/"
"Bkwld\\Croppa\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Bkwld\\Croppa\\Test\\": "tests"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"test": "vendor/bin/phpunit"
},
"extra": {
"laravel": {
"providers": [
Expand All @@ -56,5 +59,7 @@
"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.
2 changes: 2 additions & 0 deletions tests/TestListAllCrops.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,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
2 changes: 2 additions & 0 deletions tests/TestResizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class TestResizing extends TestCase

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

// Make an image
$gd = imagecreate(500, 400);
ob_start();
Expand Down
4 changes: 3 additions & 1 deletion tests/TestTooManyCrops.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,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 @@ -53,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
2 changes: 2 additions & 0 deletions tests/TestUrlMatching.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class TestUrlMatching extends TestCase

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

$this->url = new URL([
'path' => 'uploads/(.*)$',
]);
Expand Down
2 changes: 2 additions & 0 deletions tests/TestUrlParsing.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TestUrlParsing extends TestCase

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

$this->url = new URL([
'path' => 'uploads/(.*)$',
'filters' => [
Expand Down

0 comments on commit 4cee506

Please sign in to comment.