Skip to content

Commit

Permalink
refactor: Use friendsofphp/php-cs-fixer instead of squizlabs/php_code…
Browse files Browse the repository at this point in the history
…sniffer (#22)
  • Loading branch information
roadiz-ci committed Nov 6, 2024
1 parent 44ee451 commit 19561ee
Show file tree
Hide file tree
Showing 87 changed files with 766 additions and 1,491 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ jobs:
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
- name: Run Unit tests
run: vendor/bin/phpunit -v --whitelist ./src tests
- name: Run PHP Code Sniffer
run: vendor/bin/phpcs -p ./src
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpstan-doctrine": "^1.3",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 0 additions & 13 deletions phpcs.xml.dist

This file was deleted.

58 changes: 18 additions & 40 deletions src/AbstractDocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class AbstractDocumentFactory
public function __construct(
FilesystemOperator $documentsStorage,
DocumentFinderInterface $documentFinder,
?LoggerInterface $logger = null
?LoggerInterface $logger = null,
) {
if (!$documentsStorage instanceof MountManager) {
trigger_error('Document Storage must be a MountManager to address public and private files.', E_USER_WARNING);
Expand All @@ -42,69 +42,56 @@ public function __construct(
$this->logger = $logger ?? new NullLogger();
}

/**
* @return File
*/
public function getFile(): File
{
if (null === $this->file) {
throw new \BadMethodCallException('File should be defined before using it.');
}

return $this->file;
}

/**
* @param File $file
* @return $this
*/
public function setFile(File $file): static
{
$this->file = $file;

return $this;
}

/**
* @return FolderInterface|null
*/
public function getFolder(): ?FolderInterface
{
return $this->folder;
}

/**
* @param FolderInterface|null $folder
* @return $this
*/
public function setFolder(?FolderInterface $folder = null): static
{
$this->folder = $folder;

return $this;
}

/**
* Special case for SVG without XML statement.
*
* @param DocumentInterface $document
*/
protected function parseSvgMimeType(DocumentInterface $document): void
{
if (
($document->getMimeType() === 'text/plain' || $document->getMimeType() === 'text/html') &&
preg_match('#\.svg$#', $document->getFilename())
('text/plain' === $document->getMimeType() || 'text/html' === $document->getMimeType())
&& preg_match('#\.svg$#', $document->getFilename())
) {
$this->logger->debug('Uploaded a SVG without xml declaration. Presuming it’s a valid SVG file.');
$document->setMimeType('image/svg+xml');
}
}

/**
* @return DocumentInterface
*/
abstract protected function createDocument(): DocumentInterface;

/**
* @param DocumentInterface $document
*/
abstract protected function persistDocument(DocumentInterface $document): void;

protected function getHashAlgorithm(): string
Expand All @@ -116,14 +103,14 @@ protected function getHashAlgorithm(): string
* Create a document from UploadedFile, Be careful, this method does not flush, only
* persists current Document.
*
* @param bool $allowEmpty Default false, requires a local file to create new document entity
* @param bool $allowEmpty Default false, requires a local file to create new document entity
* @param bool $allowDuplicates Default false, always import new document even if file already exists
* @return null|DocumentInterface
*
* @throws FilesystemException
*/
public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = false): ?DocumentInterface
{
if ($allowEmpty === false) {
if (false === $allowEmpty) {
// Getter throw exception on null file
$file = $this->getFile();
} else {
Expand All @@ -147,8 +134,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
$existingDocument = $this->documentFinder->findOneByHashAndAlgorithm($fileHash, $this->getHashAlgorithm());
if (null !== $existingDocument) {
if (
$existingDocument->isRaw() &&
null !== $existingDownscaledDocument = $existingDocument->getDownscaledDocument()
$existingDocument->isRaw()
&& null !== $existingDownscaledDocument = $existingDocument->getDownscaledDocument()
) {
$existingDocument = $existingDownscaledDocument;
}
Expand All @@ -160,6 +147,7 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
'File %s already exists with same checksum, do not upload it twice.',
$existingDocument->getFilename()
));

return $existingDocument;
}
}
Expand All @@ -175,8 +163,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
$this->parseSvgMimeType($document);

if (
$document instanceof FileHashInterface &&
false !== $fileHash
$document instanceof FileHashInterface
&& false !== $fileHash
) {
$document->setFileHash($fileHash);
$document->setFileHashAlgorithm($this->getHashAlgorithm());
Expand All @@ -196,8 +184,6 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
/**
* Updates a document from UploadedFile, Be careful, this method does not flush.
*
* @param DocumentInterface $document
* @return DocumentInterface
* @throws FilesystemException
*/
public function updateDocument(DocumentInterface $document): DocumentInterface
Expand Down Expand Up @@ -231,7 +217,7 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
}
}

$document->setFolder(\mb_substr(hash("crc32b", date('YmdHi')), 0, 12));
$document->setFolder(\mb_substr(hash('crc32b', date('YmdHi')), 0, 12));
}

$document->setFilename($this->getFileName());
Expand All @@ -247,9 +233,6 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
}

/**
* @param File $localFile
* @param DocumentInterface $document
* @return void
* @throws FilesystemException
*/
public function moveFile(File $localFile, DocumentInterface $document): void
Expand All @@ -267,9 +250,6 @@ public function moveFile(File $localFile, DocumentInterface $document): void
}
}

/**
* @return string
*/
protected function getFileName(): string
{
$file = $this->getFile();
Expand All @@ -278,8 +258,8 @@ protected function getFileName(): string
$fileName = $file->getClientOriginalName();
} elseif (
$file instanceof DownloadedFile
&& $file->getOriginalFilename() !== null
&& $file->getOriginalFilename() !== ''
&& null !== $file->getOriginalFilename()
&& '' !== $file->getOriginalFilename()
) {
$fileName = $file->getOriginalFilename();
} else {
Expand All @@ -292,9 +272,6 @@ protected function getFileName(): string
/**
* Create a Document from an external URL.
*
* @param string $downloadUrl
*
* @return DocumentInterface|null
* @throws FilesystemException
*/
public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
Expand All @@ -303,6 +280,7 @@ public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
if (null !== $downloadedFile) {
return $this->setFile($downloadedFile)->getDocument();
}

return null;
}
}
35 changes: 13 additions & 22 deletions src/AbstractDocumentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,40 @@

abstract class AbstractDocumentFinder implements DocumentFinderInterface
{
/**
* @inheritDoc
*/
public function findVideosWithFilename(string $fileName): iterable
{
$basename = pathinfo($fileName);
$basename = $basename['filename'];

$sourcesDocsName = [
$basename . '.ogg',
$basename . '.ogv',
$basename . '.mp4',
$basename . '.mov',
$basename . '.avi',
$basename . '.webm',
$basename . '.mkv',
$basename.'.ogg',
$basename.'.ogv',
$basename.'.mp4',
$basename.'.mov',
$basename.'.avi',
$basename.'.webm',
$basename.'.mkv',
];

return $this->findAllByFilenames($sourcesDocsName);
}

/**
* @inheritDoc
*/
public function findAudiosWithFilename(string $fileName): iterable
{
$basename = pathinfo($fileName);
$basename = $basename['filename'];

$sourcesDocsName = [
$basename . '.mp3',
$basename . '.ogg',
$basename . '.wav',
$basename . '.m4a',
$basename . '.aac',
$basename.'.mp3',
$basename.'.ogg',
$basename.'.wav',
$basename.'.m4a',
$basename.'.aac',
];

return $this->findAllByFilenames($sourcesDocsName);
}

/**
* @inheritDoc
*/
public function findPicturesWithFilename(string $fileName): iterable
{
$pathInfo = pathinfo($fileName);
Expand All @@ -70,7 +61,7 @@ public function findPicturesWithFilename(string $fileName): iterable
$extensionsList = array_diff($extensionsList, [$currentExtension]);
// list sources paths for extensions
$sourcesDocsName = array_values(array_map(function ($extension) use ($basename) {
return $basename . '.' . $extension;
return $basename.'.'.$extension;
}, $extensionsList));

return $this->findAllByFilenames($sourcesDocsName);
Expand Down
4 changes: 2 additions & 2 deletions src/ArrayDocumentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct()

/**
* @param array<string> $fileNames
*
* @return ArrayCollection<int, DocumentInterface>
*/
public function findAllByFilenames(array $fileNames): ArrayCollection
Expand All @@ -49,16 +50,15 @@ public function findOneByHashAndAlgorithm(string $hash, string $algorithm): ?Doc
return null;
}


/**
* @param DocumentInterface $document
* @return $this
*/
public function addDocument(DocumentInterface $document): self
{
if (!$this->documents->contains($document)) {
$this->documents->add($document);
}

return $this;
}
}
8 changes: 3 additions & 5 deletions src/AverageColorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@ class AverageColorResolver
public function getAverageColor(Image $image): string
{
$colorArray = $this->getAverageColorAsArray($image);

return sprintf(
'#%02x%02x%02x',
$colorArray[0],
$colorArray[1],
$colorArray[2]
);
}
/**
* @param Image $image
*
* @return array
*/

public function getAverageColorAsArray(Image $image): array
{
$image->resize(1, 1);
/** @var array $array */
$array = $image->pickColor(0, 0);

return $array;
}
}
Loading

0 comments on commit 19561ee

Please sign in to comment.