Skip to content
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
12 changes: 0 additions & 12 deletions config/mago-analyze-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ code = "mixed-assignment"
message = "Assigning `mixed` type to a variable may lead to unexpected behavior."
count = 4

[[issues]]
file = "src/Command/InfoCommand.php"
code = "invalid-array-index"
message = "Invalid index type `int` used for array access on `string('kMGTPE')`."
count = 1

[[issues]]
file = "src/Command/InfoCommand.php"
code = "invalid-array-index"
Expand All @@ -60,12 +54,6 @@ code = "mixed-argument"
message = 'Invalid argument type for argument #1 of `Cloudinary\Api\Admin\AssetsTrait::asset`: expected `string`, but found `mixed`.'
count = 1

[[issues]]
file = "src/Command/InfoCommand.php"
code = "possibly-invalid-operand"
message = "Possibly invalid type for right operand."
count = 1

[[issues]]
file = "src/Command/UploadCommand.php"
code = "invalid-array-index"
Expand Down
12 changes: 6 additions & 6 deletions config/mago-lint-baseline.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
variant = "loose"

[[issues]]
file = "src/Command/InfoCommand.php"
code = "psl-math-functions"
message = "Use the Psl math function instead of the PHP counterpart."
count = 2

[[issues]]
file = "src/Factory/CloudinaryFactory.php"
code = "no-isset"
message = "Use of the `isset` construct."
count = 1

[[issues]]
file = "src/Factory/CloudinaryFactory.php"
code = "psl-array-functions"
message = "Use the Psl array function instead of the PHP counterpart."
count = 5
2 changes: 0 additions & 2 deletions config/mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ interface-name = { enabled = false }
trait-name = { enabled = false }
yoda-conditions = { enabled = false }
assertion-style = { style = "self" }
psl-array-functions = { enabled = false }
psl-datetime = { enabled = false }
psl-string-functions = { enabled = false }

[analyzer]
plugins = ["psl"]
Expand Down
36 changes: 36 additions & 0 deletions config/phpstan-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* PSL maps its PSR-4 prefixes onto the directories holding its function files, so asking whether
* "Psl\Env\get_var" is a class makes an autoloader include a file which only declares a function.
* The class still does not exist, the next autoloader includes the very same file again, and PHP
* dies with "Cannot redeclare Psl\Env\get_var()". Since 2.2.13 PHPStan performs such a lookup while
* analysing, which kills its worker processes.
*
* Wrapping every registered autoloader stops the second include: a name which is already a defined
* function can not be a class waiting to be autoloaded, so there is nothing left to look for.
*
* Taken from open-api-generator. Here it happens on PHP 8.3, with PSL 3.3 as well as 4.3, for example
* for Psl\Env\get_var() and Psl\File\read(); PHP 8.5 is not affected. Remove this file together with
* support for PHP 8.3, or once PHPStan runs on PHP 8.3 without it.
*/

declare(strict_types=1);

$autoloaders = spl_autoload_functions();

foreach ($autoloaders as $autoloader) {
spl_autoload_unregister($autoloader);
}

foreach ($autoloaders as $autoloader) {
spl_autoload_register(
static function (string $name) use ($autoloader): void {
if (function_exists($name)) {
return;
}

$autoloader($name);
},
);
}
2 changes: 2 additions & 0 deletions config/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
parameters:
bootstrapFiles:
- phpstan-bootstrap.php
level: max
paths:
- ../src
Expand Down
21 changes: 10 additions & 11 deletions src/Command/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Cloudinary\Api\ApiResponse;
use Override;
use Psl\Iter;
use Psl\Math;
use Psl\Str;
use Speicher210\CloudinaryBundle\Cloudinary\Admin;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableCell;
Expand All @@ -15,9 +18,6 @@
use Symfony\Component\Console\Style\SymfonyStyle;

use function is_scalar;
use function log;
use function pow;
use function sprintf;

final class InfoCommand extends Command
{
Expand Down Expand Up @@ -102,15 +102,14 @@ private function renderDerivedResources(SymfonyStyle $symfonyStyle, array $deriv

private function formatSize(int $bytes): string
{
$unit = 1024;
if ($bytes <= $unit) {
return $bytes . ' b';
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
$size = $bytes;
$unit = 0;
while (Math\round($size, 1) >= 1024 && $unit < Iter\count($units) - 1) {
$size /= 1024;
$unit++;
}

$exp = (int) (log($bytes) / log($unit));
$pre = 'kMGTPE';
$pre = $pre[$exp - 1];

return sprintf('%.1f %sB', $bytes / pow($unit, $exp), $pre);
return $unit === 0 ? $bytes . ' B' : Str\format('%.1f %s', $size, $units[$unit]);
}
}
14 changes: 5 additions & 9 deletions tests/Command/DisplaySnapshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
namespace Speicher210\CloudinaryBundle\Tests\Command;

use PHPUnit\Framework\Assert;
use Psl\Env;
use Psl\File;
use Psl\Regex;

use function file_get_contents;
use function file_put_contents;
use function getenv;

trait DisplaySnapshots
{
/**
Expand All @@ -22,13 +20,11 @@ private static function assertDisplayMatchesSnapshot(string $snapshot, string $d
{
$file = __DIR__ . '/Expected/' . $snapshot . '.txt';

if (getenv('UPDATE_SNAPSHOTS') === '1') {
file_put_contents($file, $display);
if (Env\get_var('UPDATE_SNAPSHOTS') === '1') {
File\write($file, $display, File\WriteMode::Truncate);
}

$expected = file_get_contents($file);
Assert::assertIsString($expected, 'Missing snapshot ' . $file);
Assert::assertSame(self::normalizeDisplay($expected), self::normalizeDisplay($display));
Assert::assertSame(self::normalizeDisplay(File\read($file)), self::normalizeDisplay($display));
}

/**
Expand Down
20 changes: 11 additions & 9 deletions tests/Command/Expected/info-verbose.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
bytes 2048
----------- --------------

----------------------------------------------------------------------------------------------------------------
Derived resources
------ -------- -------- ---------------- ----------------------------------------------------------------------
ID Format Size Transformation URL
------ -------- -------- ---------------- ----------------------------------------------------------------------
d1 webp 512 b w_100 https://res.cloudinary.com/demo/image/upload/w_100/folder/image.webp
d2 avif 2.0 kB w_200 https://res.cloudinary.com/demo/image/upload/w_200/folder/image.avif
d3 png 1.5 MB w_2000 https://res.cloudinary.com/demo/image/upload/w_2000/folder/image.png
------ -------- -------- ---------------- ----------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
Derived resources
------ -------- --------- ---------------- ----------------------------------------------------------------------
ID Format Size Transformation URL
------ -------- --------- ---------------- ----------------------------------------------------------------------
d1 webp 512 B w_100 https://res.cloudinary.com/demo/image/upload/w_100/folder/image.webp
d2 avif 2.0 KiB w_200 https://res.cloudinary.com/demo/image/upload/w_200/folder/image.avif
d3 png 1.5 MiB w_2000 https://res.cloudinary.com/demo/image/upload/w_2000/folder/image.png
d4 gif 1.0 KiB w_10 https://res.cloudinary.com/demo/image/upload/w_10/folder/image.gif
d5 jpg 1.0 MiB w_1500 https://res.cloudinary.com/demo/image/upload/w_1500/folder/image.jpg
------ -------- --------- ---------------- ----------------------------------------------------------------------
14 changes: 14 additions & 0 deletions tests/Command/InfoCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ private function adminReturningTheAsset(): Admin
'transformation' => 'w_2000',
'url' => 'https://res.cloudinary.com/demo/image/upload/w_2000/folder/image.png',
],
[
'id' => 'd4',
'format' => 'gif',
'bytes' => 1024,
'transformation' => 'w_10',
'url' => 'https://res.cloudinary.com/demo/image/upload/w_10/folder/image.gif',
],
[
'id' => 'd5',
'format' => 'jpg',
'bytes' => 1_048_575,
'transformation' => 'w_1500',
'url' => 'https://res.cloudinary.com/demo/image/upload/w_1500/folder/image.jpg',
],
],
],
[],
Expand Down
30 changes: 11 additions & 19 deletions tests/Command/UploadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psl\Env;
use Psl\Filesystem;
use Psl\SecureRandom;
use Psl\Type;
use Psl\Vec;
use Speicher210\CloudinaryBundle\Cloudinary\Uploader;
use Speicher210\CloudinaryBundle\Command\UploadCommand;
use Symfony\Component\Console\Tester\CommandTester;

use function mkdir;
use function preg_quote;
use function realpath;
use function rmdir;
use function sort;
use function sys_get_temp_dir;
use function touch;
use function uniqid;
use function unlink;

/**
* The table shows absolute paths, so its column widths depend on where the tests run: the display is not compared with
Expand All @@ -33,29 +29,26 @@ final class UploadCommandTest extends TestCase
{
private const array FILE_NAMES = ['document.pdf', 'logo.png', 'photo.jpg'];

/** @var non-empty-string */
private string $directory;

#[Override]
protected function setUp(): void
{
$directory = sys_get_temp_dir() . '/' . uniqid('cloudinary-bundle-upload-', true);
mkdir($directory);
$directory = Env\temp_dir() . '/cloudinary-bundle-upload-' . SecureRandom\string(16);
Filesystem\create_directory($directory);
foreach (self::FILE_NAMES as $fileName) {
touch($directory . '/' . $fileName);
Filesystem\create_file($directory . '/' . $fileName);
}

// The command shows real paths, and the temporary directory may be behind a symbolic link.
$this->directory = Type\string()->assert(realpath($directory));
$this->directory = Type\non_empty_string()->coerce(Filesystem\canonicalize($directory));
}

#[Override]
protected function tearDown(): void
{
foreach (self::FILE_NAMES as $fileName) {
unlink($this->directory . '/' . $fileName);
}

rmdir($this->directory);
Filesystem\delete_directory($this->directory, recursive: true);
}

public function testUploadsEveryFileWithThePrefixedFileNameAsPublicId(): void
Expand All @@ -78,14 +71,13 @@ static function (mixed $file, array $options) use (&$uploads): ApiResponse {

$commandTester->assertCommandIsSuccessful();

sort($uploads);
self::assertSame(
[
[$this->directory . '/document.pdf', ['public_id' => 'uploads/document']],
[$this->directory . '/logo.png', ['public_id' => 'uploads/logo']],
[$this->directory . '/photo.jpg', ['public_id' => 'uploads/photo']],
],
$uploads,
Vec\sort($uploads),
);

$display = $commandTester->getDisplay();
Expand Down
Loading