Skip to content

Commit

Permalink
phpstan fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Aug 4, 2022
1 parent 1fa667e commit 7a36c12
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 19 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -9,7 +9,8 @@
"php": ">=7.4",
"nette/utils": "^3.2",
"league/flysystem": "^1.1",
"psr/event-dispatcher": "^1.0"
"psr/event-dispatcher": "^1.0",
"typertion/php": "~1.0.1"
},
"require-dev": {
"codeception/codeception": "^4.1.20",
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
@@ -1,5 +1,5 @@
parameters:
level: 8
level: 9
excludePaths:
analyse:
- src/Bridge/Nette/Macro/ImageMacro.php
5 changes: 4 additions & 1 deletion src/Bridge/Gumlet/GumletLinkGenerator.php
Expand Up @@ -13,6 +13,7 @@
use Contributte\Imagist\PathInfo\PathInfoFactoryInterface;
use Contributte\Imagist\Resolver\DefaultImageResolverInterface;
use InvalidArgumentException;
use Typertion\Php\TypeAssert;

final class GumletLinkGenerator implements LinkGeneratorInterface
{
Expand Down Expand Up @@ -99,7 +100,9 @@ private function createPath(PersistentImageInterface $image, array $options): st
self::GUMLET_CONTEXT_KEY => true,
]));

$query = array_merge($options['addons'] ?? [], $query);
$addons = isset($options['addons']) ? TypeAssert::array($options['addons']) : [];

$query = array_merge($addons, $query);

if (!$query) {
return $this->hashIfNeed($path, true);
Expand Down
6 changes: 5 additions & 1 deletion src/Bridge/Nette/Filter/NetteResourceFactory.php
Expand Up @@ -6,6 +6,7 @@
use Contributte\Imagist\Filter\Context\ContextInterface;
use Contributte\Imagist\Filter\Resource\ResourceFactoryInterface;
use Nette\Utils\Image;
use Typertion\Php\TypeAssert;

final class NetteResourceFactory implements ResourceFactoryInterface
{
Expand All @@ -21,7 +22,10 @@ public function toString(object $resource, FileInterface $source, ContextInterfa
{
assert($resource instanceof Image);

return $resource->toString($source->getMimeType()->getImageType(), $context->get(self::QUALITY_CONTEXT));
return $resource->toString(
$source->getMimeType()->getImageType(),
TypeAssert::intOrNull($context->get(self::QUALITY_CONTEXT))
);
}

}
5 changes: 3 additions & 2 deletions src/Bridge/Nette/Form/ImageUploadControl.php
Expand Up @@ -15,6 +15,7 @@
use Nette\Forms\Controls\UploadControl;
use Nette\Http\FileUpload;
use Nette\Utils\Html;
use Typertion\Php\TypeAssert;

final class ImageUploadControl extends UploadControl
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public function loadHttpData(): void
{
parent::loadHttpData();

$this->setValue($this->uploadValue = $this->value);
$this->setValue($this->uploadValue = TypeAssert::instanceOfOrNull($this->value, FileUpload::class));

if ($this->remove) {
$removeAnyway = $this->remove->getHttpData($this);
Expand Down Expand Up @@ -71,7 +72,7 @@ public function setScope(?Scope $scope)
public function setValue($value)
{
if ($value === null) {
$this->entity = $this->entity->withValue(null);
$this->entity = $this->entity->withValue();
} elseif ($value instanceof FileUpload) {
$image = null;
if ($value->isOk() && $value->isImage()) {
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Nette/Tracy/ImagistBlueScreen.php
Expand Up @@ -19,6 +19,7 @@ public static function install(BlueScreen $blueScreen): void

// phpcs:disable SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable
$expanded = null;
/** @var array{file?: string}[] $stack */
$stack = $exception->getBackTrace();
$dump = $blueScreen->getDumper();
foreach ($stack as $key => $trace) {
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/PersistentImage.php
Expand Up @@ -13,7 +13,7 @@ public function __construct(string $id)
}

/**
* @return mixed[]
* @return array{string, Scope}
*/
protected function parseId(string $id): array
{
Expand Down
10 changes: 6 additions & 4 deletions src/Filesystem/FilesystemAbstract.php
Expand Up @@ -35,7 +35,7 @@ public function delete(PathInfoInterface $path): bool
'disable_asserts',
true,
false,
fn() => $this->adapter->delete($path->toString())
fn(): bool => $this->adapter->delete($path->toString())
);
}

Expand All @@ -50,15 +50,15 @@ public function listContents(string $path): array
/**
* @inheritDoc
*/
public function put(PathInfoInterface $path, $content, array $config = []): void
public function put(PathInfoInterface $path, string $content, array $config = []): void
{
$this->adapter->put($path->toString(), $content, $config);
}

/**
* @inheritDoc
*/
public function putWithMkdir(PathInfoInterface $path, $content, array $config = []): void
public function putWithMkdir(PathInfoInterface $path, string $content, array $config = []): void
{
$this->adapter->createDir($path->toString($path::BUCKET | $path::SCOPE | $path::FILTER));

Expand Down Expand Up @@ -94,9 +94,11 @@ public function mimeType(PathInfoInterface $path): ?string
}

/**
* @template T
* @param mixed $value
* @param mixed $default
* @return mixed
* @param callable(): T $callback
* @return T
*/
protected function withTemporaryConfig(string $name, $value, $default, callable $callback) // phpcs:ignore -- phpcs bug
{
Expand Down
6 changes: 2 additions & 4 deletions src/Filesystem/FilesystemInterface.php
Expand Up @@ -8,10 +8,9 @@ interface FilesystemInterface
{

/**
* @param mixed $content
* @param mixed[] $config
*/
public function putWithMkdir(PathInfoInterface $path, $content, array $config = []): void; // phpcs:ignore -- cs bug
public function putWithMkdir(PathInfoInterface $path, string $content, array $config = []): void; // phpcs:ignore -- cs bug

public function exists(PathInfoInterface $path): bool;

Expand All @@ -26,10 +25,9 @@ public function delete(PathInfoInterface $path);
public function listContents(string $path): array;

/**
* @param mixed $content
* @param mixed[] $config
*/
public function put(PathInfoInterface $path, $content, array $config = []): void; // phpcs:ignore -- cs bug
public function put(PathInfoInterface $path, string $content, array $config = []): void; // phpcs:ignore -- cs bug

public function read(PathInfoInterface $path): string;

Expand Down
2 changes: 1 addition & 1 deletion src/Filesystem/GoogleStorageFilesystem.php
Expand Up @@ -36,7 +36,7 @@ public function __construct(string $bucket, string $keyFilePath)
/**
* @inheritDoc
*/
public function putWithMkdir(PathInfoInterface $path, $content, array $config = []): void
public function putWithMkdir(PathInfoInterface $path, string $content, array $config = []): void
{
$this->put($path, $content, $config);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Remover/PersistentImageRemover.php
Expand Up @@ -9,6 +9,8 @@
use Contributte\Imagist\Filter\Context\ContextInterface;
use Contributte\Imagist\Filter\Internal\VoidFilter;
use Contributte\Imagist\PathInfo\PathInfoFactoryInterface;
use Typertion\Php\ArrayTypeAssert;
use Typertion\Php\TypeAssert;

final class PersistentImageRemover implements RemoverInterface
{
Expand Down Expand Up @@ -44,15 +46,18 @@ private function removeFiltered(PersistentImageInterface $image): void
$path = $this->pathInfoFactory->create($image->withFilter(new VoidFilter('void')));

foreach ($this->filesystem->listContents($path->toString($path::BUCKET | $path::SCOPE)) as $path) {
if ($path['type'] !== 'dir') {
$path = TypeAssert::array($path);

if (ArrayTypeAssert::string($path, 'type') !== 'dir') {
continue;
}

if (!$path['filename'] || $path['filename'][0] !== '_') {
$filename = isset($path['filename']) ? TypeAssert::string($path['filename']) : '';
if (!$filename || $filename[0] !== '_') {
continue;
}

$this->fileFactory->create($image->withFilter(new VoidFilter(substr($path['filename'], 1))))
$this->fileFactory->create($image->withFilter(new VoidFilter(substr($filename, 1))))
->delete();
}
}
Expand Down

0 comments on commit 7a36c12

Please sign in to comment.