Skip to content

Commit

Permalink
Merge pull request #12 from intaro/phpstan
Browse files Browse the repository at this point in the history
Phpstan and php cs are added
  • Loading branch information
1on authored Jul 26, 2022
2 parents 29372fd + 083a92b commit eec98e5
Show file tree
Hide file tree
Showing 8 changed files with 3,233 additions and 24 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ permissions:

jobs:
build:

runs-on: ubuntu-latest

steps:
Expand All @@ -20,6 +19,11 @@ jobs:
- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
/var/
.php-cs-fixer.cache
6 changes: 4 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public function getConfigTreeBuilder()
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
// @phpstan-ignore-next-line
$rootNode = $treeBuilder->root('intaro_file_uploader');
}

$rootNode
->children()
->arrayNode('uploaders')
Expand Down Expand Up @@ -56,7 +57,8 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->end();
->end()
;

return $treeBuilder;
}
Expand Down
22 changes: 13 additions & 9 deletions DependencyInjection/IntaroFileUploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ public function prepend(ContainerBuilder $container): void

foreach ($config['uploaders'] as $uploaderType => $uploaders) {
foreach ($uploaders as $name => $options) {
$container->setDefinition("intaro.{$name}_uploader",
$container->setDefinition(
"intaro.{$name}_uploader",
new Definition(
'%intaro_file_uploader.class%',
[
new Reference("gaufrette.{$name}_filesystem"),
$options['path'],
$options['allowed_types'],
]
));
'%intaro_file_uploader.class%',
[
new Reference("gaufrette.{$name}_filesystem"),
$options['path'],
$options['allowed_types'],
]
)
);
}
}
}

// @phpstan-ignore-next-line у configs сложная структура
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
Expand All @@ -48,7 +51,8 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('services.yml');
}

protected function generateGaufretteConfig($config)
// @phpstan-ignore-next-line у config и возвращаемого значения сложная структура
protected function generateGaufretteConfig(array $config): array
{
$filesystems = [];
$adapters = [];
Expand Down
31 changes: 20 additions & 11 deletions Services/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Gaufrette\Adapter\AwsS3;
use Gaufrette\Adapter\Local;
use Gaufrette\Adapter\MetadataSupporter;
use Gaufrette\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand All @@ -12,9 +13,11 @@ class FileUploader
{
private Filesystem $filesystem;
private string $path;
private ?array $allowedTypes;
/** @var array<string> */
private array $allowedTypes;
private \Transliterator $translator;

/** @param array<string> $allowedTypes */
public function __construct(
Filesystem $filesystem,
string $path,
Expand All @@ -23,7 +26,12 @@ public function __construct(
$this->filesystem = $filesystem;
$this->path = $path;
$this->allowedTypes = $allowedTypes;
$this->translator = \Transliterator::create('Any-Latin;Latin-ASCII;Lower;[\u0080-\u7fff] remove');

$translator = \Transliterator::create('Any-Latin;Latin-ASCII;Lower;[\u0080-\u7fff] remove');
if (!$translator) {
throw new \RuntimeException('Failed to create Transliterator');
}
$this->translator = $translator;
}

/**
Expand All @@ -42,14 +50,14 @@ public function upload(UploadedFile $file): string

$adapter = $this->filesystem->getAdapter();

if (!($adapter instanceof Local)) {
if ($adapter instanceof MetadataSupporter) {
$adapter->setMetadata(
$filename,
['contentType' => $fileMimeType]
);
}

$adapter->write($filename, file_get_contents($file->getPathname()));
$adapter->write($filename, (string) file_get_contents($file->getPathname()));

return $filename;
}
Expand All @@ -68,14 +76,14 @@ public function uploadByPath(string $pathname, bool $unlinkAfterUpload = true):

$adapter = $this->filesystem->getAdapter();

if (!($adapter instanceof Local)) {
if ($adapter instanceof MetadataSupporter) {
$adapter->setMetadata(
$filename,
['contentType' => $fileMimeType]
);
}

$adapter->write($filename, file_get_contents($file->getPathname()));
$adapter->write($filename, (string) file_get_contents($file->getPathname()));

if ($unlinkAfterUpload) {
unlink($file->getPathname());
Expand All @@ -94,7 +102,7 @@ public function uploadByContent(string $fileContent, string $filename, string $m

$adapter = $this->filesystem->getAdapter();

if (!($adapter instanceof Local)) {
if ($adapter instanceof MetadataSupporter) {
$adapter->setMetadata(
$filename,
['contentType' => $mimeType]
Expand Down Expand Up @@ -131,16 +139,16 @@ protected function clearName(string $originalName): string
return preg_replace(
'/\s+/',
'-',
$this->translator->transliterate($originalName)
(string) $this->translator->transliterate($originalName)
);
}

public function remove($name): bool
public function remove(string $name): bool
{
return $this->filesystem->delete($name);
}

public function getUrl($name): string
public function getUrl(string $name): string
{
return $this->getPath() . $name;
}
Expand Down Expand Up @@ -188,7 +196,8 @@ public function getPath(): string
return $this->path;
}

public function getAllowedTypes(): ?array
/** @return array<string> */
public function getAllowedTypes(): array
{
return $this->allowedTypes;
}
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "symfony-bundle",
"keywords": ["symfony2", "file upload"],
"license": "MIT",
"Description": "Symfony2 bundle which is intended to simplify file uploading",
"description": "Symfony2 bundle which is intended to simplify file uploading",
"authors": [
{
"name": "Intaro Soft",
Expand All @@ -18,5 +18,9 @@
"psr-4": {
"Intaro\\FileUploaderBundle\\": ""
}
},
"require-dev": {
"phpstan/phpstan": "^1.8",
"friendsofphp/php-cs-fixer": "^3.9"
}
}
Loading

0 comments on commit eec98e5

Please sign in to comment.