From 5995528f0960fd7eb55de15ee38b5412d720fb5a Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 13 Nov 2024 15:06:50 +0100 Subject: [PATCH] Fix --- composer.json | 1 + .../SonataExporterExtension.php | 2 +- src/Exporter.php | 4 +-- src/Handler.php | 2 +- src/Source/AbstractPropertySourceIterator.php | 2 +- src/Source/AbstractXmlSourceIterator.php | 4 +-- src/Source/CsvSourceIterator.php | 4 +-- .../DoctrineDBALConnectionSourceIterator.php | 2 +- src/Source/DoctrineODMQuerySourceIterator.php | 2 +- src/Source/IteratorCallbackSourceIterator.php | 2 +- src/Source/SymfonySitemapSourceIterator.php | 2 +- src/Source/XmlSourceIterator.php | 2 +- src/Writer/CsvWriter.php | 6 ++-- src/Writer/FormattedBoolWriter.php | 2 +- src/Writer/GsaFeedWriter.php | 14 ++++----- src/Writer/JsonWriter.php | 4 +-- src/Writer/SitemapWriter.php | 30 +++++++++---------- src/Writer/XlsWriter.php | 10 +++---- src/Writer/XlsxWriter.php | 6 ++-- src/Writer/XmlExcelWriter.php | 6 ++-- src/Writer/XmlWriter.php | 16 +++++----- tests/Writer/XlsxWriterTest.php | 4 +-- 22 files changed, 64 insertions(+), 63 deletions(-) diff --git a/composer.json b/composer.json index 302b4eca..b81d7da6 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "matthiasnoback/symfony-dependency-injection-test": "^4.0 || ^5.0", "phpoffice/phpspreadsheet": "^1.23", "phpstan/extension-installer": "^1.0", + "phpstan/phpdoc-parser": "^1.0", "phpstan/phpstan": "^1.0", "phpstan/phpstan-phpunit": "^1.0", "phpstan/phpstan-strict-rules": "^1.0", diff --git a/src/Bridge/Symfony/DependencyInjection/SonataExporterExtension.php b/src/Bridge/Symfony/DependencyInjection/SonataExporterExtension.php index 62fc9da5..50af9318 100644 --- a/src/Bridge/Symfony/DependencyInjection/SonataExporterExtension.php +++ b/src/Bridge/Symfony/DependencyInjection/SonataExporterExtension.php @@ -61,7 +61,7 @@ private function configureWriters(ContainerBuilder $container, array $config): v { foreach ($config as $format => $settings) { foreach ($settings as $key => $value) { - $container->setParameter(sprintf( + $container->setParameter(\sprintf( 'sonata.exporter.writer.%s.%s', $format, $key diff --git a/src/Exporter.php b/src/Exporter.php index 2f71cd36..5bd1b1ec 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -44,7 +44,7 @@ public function __construct(array $writers = []) public function getResponse(string $format, string $filename, \Iterator $source): StreamedResponse { if (!\array_key_exists($format, $this->writers)) { - throw new \RuntimeException(sprintf( + throw new \RuntimeException(\sprintf( 'Invalid "%s" format, supported formats are : "%s"', $format, implode(', ', array_keys($this->writers)) @@ -58,7 +58,7 @@ public function getResponse(string $format, string $filename, \Iterator $source) }; $headers = [ - 'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), + 'Content-Disposition' => \sprintf('attachment; filename="%s"', $filename), ]; $headers['Content-Type'] = $writer->getDefaultMimeType(); diff --git a/src/Handler.php b/src/Handler.php index 645171c7..0a4ef9ef 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -19,7 +19,7 @@ final class Handler { public function __construct( private \Iterator $source, - private WriterInterface $writer + private WriterInterface $writer, ) { } diff --git a/src/Source/AbstractPropertySourceIterator.php b/src/Source/AbstractPropertySourceIterator.php index 830be23e..6a7fbf3e 100644 --- a/src/Source/AbstractPropertySourceIterator.php +++ b/src/Source/AbstractPropertySourceIterator.php @@ -44,7 +44,7 @@ abstract class AbstractPropertySourceIterator implements \Iterator public function __construct( protected array $fields, protected string $dateTimeFormat = 'r', - protected bool $useBackedEnumValue = true + protected bool $useBackedEnumValue = true, ) { $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); } diff --git a/src/Source/AbstractXmlSourceIterator.php b/src/Source/AbstractXmlSourceIterator.php index f599b686..72434c3b 100644 --- a/src/Source/AbstractXmlSourceIterator.php +++ b/src/Source/AbstractXmlSourceIterator.php @@ -57,7 +57,7 @@ abstract class AbstractXmlSourceIterator implements \Iterator public function __construct( protected string $filename, - protected bool $hasHeaders = true + protected bool $hasHeaders = true, ) { } @@ -111,7 +111,7 @@ final public function rewind(): void $file = fopen($this->filename, 'r'); if (false === $file) { - throw new \Exception(sprintf('Cannot open file %s.', $this->filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $this->filename)); } $this->file = $file; diff --git a/src/Source/CsvSourceIterator.php b/src/Source/CsvSourceIterator.php index b72e8df8..d296e915 100644 --- a/src/Source/CsvSourceIterator.php +++ b/src/Source/CsvSourceIterator.php @@ -47,7 +47,7 @@ public function __construct( private string $delimiter = ',', private string $enclosure = '"', private string $escape = '\\', - private bool $hasHeaders = true + private bool $hasHeaders = true, ) { } @@ -86,7 +86,7 @@ public function rewind(): void { $file = fopen($this->filename, 'r'); if (false === $file) { - throw new \Exception(sprintf('Cannot open file %s.', $this->filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $this->filename)); } $this->file = $file; diff --git a/src/Source/DoctrineDBALConnectionSourceIterator.php b/src/Source/DoctrineDBALConnectionSourceIterator.php index d2398c7b..d6d16965 100644 --- a/src/Source/DoctrineDBALConnectionSourceIterator.php +++ b/src/Source/DoctrineDBALConnectionSourceIterator.php @@ -36,7 +36,7 @@ final class DoctrineDBALConnectionSourceIterator implements \Iterator public function __construct( private Connection $connection, private string $query, - private array $parameters = [] + private array $parameters = [], ) { } diff --git a/src/Source/DoctrineODMQuerySourceIterator.php b/src/Source/DoctrineODMQuerySourceIterator.php index 8d06c698..1f24a9f0 100644 --- a/src/Source/DoctrineODMQuerySourceIterator.php +++ b/src/Source/DoctrineODMQuerySourceIterator.php @@ -27,7 +27,7 @@ public function __construct( array $fields, string $dateTimeFormat = \DateTimeInterface::ATOM, private int $batchSize = 100, - bool $useBackedEnumValue = true + bool $useBackedEnumValue = true, ) { $this->query = clone $query; diff --git a/src/Source/IteratorCallbackSourceIterator.php b/src/Source/IteratorCallbackSourceIterator.php index 7d4f40d0..a0d96c8c 100644 --- a/src/Source/IteratorCallbackSourceIterator.php +++ b/src/Source/IteratorCallbackSourceIterator.php @@ -26,7 +26,7 @@ final class IteratorCallbackSourceIterator extends IteratorSourceIterator */ public function __construct( \Iterator $iterator, - private \Closure $transformer + private \Closure $transformer, ) { parent::__construct($iterator); } diff --git a/src/Source/SymfonySitemapSourceIterator.php b/src/Source/SymfonySitemapSourceIterator.php index ac09a1ef..fe220b09 100644 --- a/src/Source/SymfonySitemapSourceIterator.php +++ b/src/Source/SymfonySitemapSourceIterator.php @@ -28,7 +28,7 @@ public function __construct( private \Iterator $source, private RouterInterface $router, private string $routeName, - private array $parameters = [] + private array $parameters = [], ) { } diff --git a/src/Source/XmlSourceIterator.php b/src/Source/XmlSourceIterator.php index bef58206..3cd2aeb8 100644 --- a/src/Source/XmlSourceIterator.php +++ b/src/Source/XmlSourceIterator.php @@ -23,7 +23,7 @@ final class XmlSourceIterator extends AbstractXmlSourceIterator public function __construct( string $filename, private string $mainTag = 'datas', - private string $dataTag = 'data' + private string $dataTag = 'data', ) { parent::__construct($filename, false); } diff --git a/src/Writer/CsvWriter.php b/src/Writer/CsvWriter.php index 2093ea52..afacd09d 100644 --- a/src/Writer/CsvWriter.php +++ b/src/Writer/CsvWriter.php @@ -40,12 +40,12 @@ public function __construct( private string $escape = '\\', private bool $showHeaders = true, private bool $withBom = false, - private string $terminate = "\n" + private string $terminate = "\n", ) { $this->position = 0; if (is_file($filename)) { - throw new \RuntimeException(sprintf('The file %s already exist', $filename)); + throw new \RuntimeException(\sprintf('The file %s already exist', $filename)); } } @@ -63,7 +63,7 @@ public function open(): void { $file = fopen($this->filename, 'w', false); if (false === $file) { - throw new \Exception(sprintf('Cannot open file %s.', $this->filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $this->filename)); } $this->file = $file; diff --git a/src/Writer/FormattedBoolWriter.php b/src/Writer/FormattedBoolWriter.php index a1f65220..aab2bf23 100644 --- a/src/Writer/FormattedBoolWriter.php +++ b/src/Writer/FormattedBoolWriter.php @@ -23,7 +23,7 @@ final class FormattedBoolWriter implements WriterInterface public function __construct( private WriterInterface $writer, private string $trueLabel = 'yes', - private string $falseLabel = 'no' + private string $falseLabel = 'no', ) { } diff --git a/src/Writer/GsaFeedWriter.php b/src/Writer/GsaFeedWriter.php index 1a35aa18..2e69c0bc 100644 --- a/src/Writer/GsaFeedWriter.php +++ b/src/Writer/GsaFeedWriter.php @@ -44,7 +44,7 @@ public function __construct( private \SplFileInfo $folder, private string $dtd, private string $datasource, - private string $feedtype + private string $feedtype, ) { $this->bufferPart = 0; $this->bufferSize = 0; @@ -57,7 +57,7 @@ public function open(): void public function write(array $data): void { - $line = sprintf( + $line = \sprintf( " \n", $data['url'], $data['mime_type'], @@ -71,7 +71,7 @@ public function write(array $data): void $written = fwrite($this->getBuffer(), $line); if (false === $written) { - throw new \Exception(sprintf('Cannot write line %s in the buffer.', $line)); + throw new \Exception(\sprintf('Cannot write line %s in the buffer.', $line)); } $this->bufferSize += $written; @@ -99,13 +99,13 @@ private function generateNewPart(): void ++$this->bufferPart; if (!$this->folder->isWritable()) { - throw new \RuntimeException(sprintf('Unable to write to folder: %s', (string) $this->folder)); + throw new \RuntimeException(\sprintf('Unable to write to folder: %s', (string) $this->folder)); } - $filename = sprintf('%s/feed_%05d.xml', (string) $this->folder, $this->bufferPart); + $filename = \sprintf('%s/feed_%05d.xml', (string) $this->folder, $this->bufferPart); $buffer = fopen($filename, 'w'); if (false === $buffer) { - throw new \Exception(sprintf('Cannot open file %s.', $filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $filename)); } $this->buffer = $buffer; @@ -125,7 +125,7 @@ private function generateNewPart(): void XML ); if (false === $written) { - throw new \Exception(sprintf('Cannot write file %s.', $filename)); + throw new \Exception(\sprintf('Cannot write file %s.', $filename)); } $this->bufferSize += $written; diff --git a/src/Writer/JsonWriter.php b/src/Writer/JsonWriter.php index 5980c2bc..fa41a70f 100644 --- a/src/Writer/JsonWriter.php +++ b/src/Writer/JsonWriter.php @@ -34,7 +34,7 @@ final class JsonWriter implements TypedWriterInterface public function __construct(private string $filename) { if (is_file($filename)) { - throw new \RuntimeException(sprintf('The file %s already exist', $filename)); + throw new \RuntimeException(\sprintf('The file %s already exist', $filename)); } } @@ -52,7 +52,7 @@ public function open(): void { $file = fopen($this->filename, 'w', false); if (false === $file) { - throw new \Exception(sprintf('Cannot open file %s.', $this->filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $this->filename)); } $this->file = $file; diff --git a/src/Writer/SitemapWriter.php b/src/Writer/SitemapWriter.php index 4c88759a..8138e85e 100644 --- a/src/Writer/SitemapWriter.php +++ b/src/Writer/SitemapWriter.php @@ -49,7 +49,7 @@ public function __construct( private string $folder, private string $groupName = '', private array $headers = [], - private bool $autoIndex = true + private bool $autoIndex = true, ) { $this->pattern = 'sitemap_'.('' !== $this->groupName ? $this->groupName.'_' : '').'%05d.xml'; } @@ -120,7 +120,7 @@ public function close(): void public static function generateSitemapIndex(string $folder, string $baseUrl, string $pattern = 'sitemap*.xml', string $filename = 'sitemap.xml'): void { $content = "\n\n"; - $files = glob(sprintf('%s/%s', $folder, $pattern)); + $files = glob(\sprintf('%s/%s', $folder, $pattern)); if (false === $files) { throw new \RuntimeException('Cannot find sitemap files.'); } @@ -128,10 +128,10 @@ public static function generateSitemapIndex(string $folder, string $baseUrl, str foreach ($files as $file) { $stat = stat($file); if (false === $stat) { - throw new \RuntimeException(sprintf('Cannot access to stats of the file %s.', $file)); + throw new \RuntimeException(\sprintf('Cannot access to stats of the file %s.', $file)); } - $content .= sprintf( + $content .= \sprintf( "\t".'%s/%s%s'."\n", $baseUrl, basename($file), @@ -141,7 +141,7 @@ public static function generateSitemapIndex(string $folder, string $baseUrl, str $content .= ''; - file_put_contents(sprintf('%s/%s', $folder, $filename), $content); + file_put_contents(\sprintf('%s/%s', $folder, $filename), $content); } /** @@ -160,20 +160,20 @@ private function generateNewPart(): void ++$this->bufferPart; if (!is_writable($this->folder)) { - throw new \RuntimeException(sprintf('Unable to write to folder: %s', $this->folder)); + throw new \RuntimeException(\sprintf('Unable to write to folder: %s', $this->folder)); } - $filename = sprintf($this->pattern, $this->bufferPart); + $filename = \sprintf($this->pattern, $this->bufferPart); $buffer = fopen($this->folder.'/'.$filename, 'w'); if (false === $buffer) { - throw new \Exception(sprintf('Cannot open file %s.', $this->folder.'/'.$filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $this->folder.'/'.$filename)); } $this->buffer = $buffer; $written = fwrite($this->buffer, ''."\n".'getHeaderByFlag().'>'."\n"); if (false === $written) { - throw new \Exception(sprintf('Cannot write file %s.', $this->folder.'/'.$filename)); + throw new \Exception(\sprintf('Cannot write file %s.', $this->folder.'/'.$filename)); } $this->bufferSize += $written; @@ -196,7 +196,7 @@ private function addSitemapLine(string $line): void $written = fwrite($this->getBuffer(), $line); if (false === $written) { - throw new \Exception(sprintf('Cannot write line %s in the buffer.', $line)); + throw new \Exception(\sprintf('Cannot write line %s in the buffer.', $line)); } $this->bufferSize += $written; @@ -258,7 +258,7 @@ private function fixDataType(array &$data): void */ private function generateDefaultLine(array $data): string { - return sprintf( + return \sprintf( ' %s%s%s%s'."\n", $data['url'], (new \DateTime($data['lastmod']))->format('Y-m-d'), @@ -289,13 +289,13 @@ private function generateImageLine(array $data): string $images .= ''; foreach ($image as $key => $element) { - $images .= sprintf('%2$s', $builder[$key] ?? $key, $element); + $images .= \sprintf('%2$s', $builder[$key] ?? $key, $element); } $images .= ''; } - return sprintf(' %s%s'."\n", $data['url'], $images); + return \sprintf(' %s%s'."\n", $data['url'], $images); } /** @@ -311,10 +311,10 @@ private function generateVideoLine(array $data): string ]; foreach ($data['video'] as $key => $video) { - $videos .= sprintf('%2$s', $builder[$key] ?? $key, $video); + $videos .= \sprintf('%2$s', $builder[$key] ?? $key, $video); } - return sprintf(' %s%s'."\n", $data['url'], $videos); + return \sprintf(' %s%s'."\n", $data['url'], $videos); } /** diff --git a/src/Writer/XlsWriter.php b/src/Writer/XlsWriter.php index 40d3b9f4..3cb29cac 100644 --- a/src/Writer/XlsWriter.php +++ b/src/Writer/XlsWriter.php @@ -33,10 +33,10 @@ final class XlsWriter implements TypedWriterInterface */ public function __construct( private string $filename, - private bool $showHeaders = true + private bool $showHeaders = true, ) { if (is_file($filename)) { - throw new \RuntimeException(sprintf('The file %s already exists', $filename)); + throw new \RuntimeException(\sprintf('The file %s already exists', $filename)); } } @@ -54,7 +54,7 @@ public function open(): void { $file = fopen($this->filename, 'w', false); if (false === $file) { - throw new \Exception(sprintf('Cannot open file %s.', $this->filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $this->filename)); } $this->file = $file; @@ -78,7 +78,7 @@ public function write(array $data): void fwrite($this->getFile(), ''); foreach ($data as $value) { - fwrite($this->getFile(), sprintf('%s', $value)); + fwrite($this->getFile(), \sprintf('%s', $value)); } fwrite($this->getFile(), ''); @@ -97,7 +97,7 @@ private function init(array $data): void if ($this->showHeaders) { fwrite($this->getFile(), ''); foreach ($data as $header => $value) { - fwrite($this->getFile(), sprintf('%s', (string) $header)); + fwrite($this->getFile(), \sprintf('%s', (string) $header)); } fwrite($this->getFile(), ''); ++$this->position; diff --git a/src/Writer/XlsxWriter.php b/src/Writer/XlsxWriter.php index cad04e17..270606e1 100644 --- a/src/Writer/XlsxWriter.php +++ b/src/Writer/XlsxWriter.php @@ -42,14 +42,14 @@ final class XlsxWriter implements TypedWriterInterface public function __construct( string $filename, private bool $showHeaders = true, - private bool $showFilters = true + private bool $showFilters = true, ) { if (!class_exists(Spreadsheet::class)) { throw new \LogicException('You need the "phpoffice/spreadsheet" package in order to use the XLSX export.'); } if (is_file($filename)) { - throw new \InvalidArgumentException(sprintf('The file "%s" already exists.', $filename)); + throw new \InvalidArgumentException(\sprintf('The file "%s" already exists.', $filename)); } $this->filename = $filename; @@ -189,7 +189,7 @@ private function getDataFormat(mixed $value): ?string $dateTime = $this->getDateTime($value); if ($dateTime instanceof \DateTimeInterface) { - return sprintf('%s hh:mm:ss', NumberFormat::FORMAT_DATE_DDMMYYYY); + return \sprintf('%s hh:mm:ss', NumberFormat::FORMAT_DATE_DDMMYYYY); } return null; diff --git a/src/Writer/XmlExcelWriter.php b/src/Writer/XmlExcelWriter.php index 6b5693c0..7418e7b4 100644 --- a/src/Writer/XmlExcelWriter.php +++ b/src/Writer/XmlExcelWriter.php @@ -45,10 +45,10 @@ final class XmlExcelWriter implements WriterInterface public function __construct( private string $filename, private bool $showHeaders = true, - private mixed $columnsType = null + private mixed $columnsType = null, ) { if (is_file($filename)) { - throw new \RuntimeException(sprintf('The file %s already exist', $filename)); + throw new \RuntimeException(\sprintf('The file %s already exist', $filename)); } } @@ -56,7 +56,7 @@ public function open(): void { $file = fopen($this->filename, 'w', false); if (false === $file) { - throw new \Exception(sprintf('Cannot open file %s.', $this->filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $this->filename)); } $this->file = $file; diff --git a/src/Writer/XmlWriter.php b/src/Writer/XmlWriter.php index f24a32fd..7d969bff 100644 --- a/src/Writer/XmlWriter.php +++ b/src/Writer/XmlWriter.php @@ -35,10 +35,10 @@ final class XmlWriter implements TypedWriterInterface public function __construct( private string $filename, private string $mainElement = 'datas', - private string $childElement = 'data' + private string $childElement = 'data', ) { if (is_file($filename)) { - throw new \RuntimeException(sprintf('The file %s already exist', $filename)); + throw new \RuntimeException(\sprintf('The file %s already exist', $filename)); } } @@ -56,11 +56,11 @@ public function open(): void { $file = fopen($this->filename, 'w', false); if (false === $file) { - throw new \Exception(sprintf('Cannot open file %s.', $this->filename)); + throw new \Exception(\sprintf('Cannot open file %s.', $this->filename)); } $this->file = $file; - fwrite($this->file, sprintf("\n<%s>\n", $this->mainElement)); + fwrite($this->file, \sprintf("\n<%s>\n", $this->mainElement)); } /** @@ -70,19 +70,19 @@ public function open(): void */ public function close(): void { - fwrite($this->getFile(), sprintf('', $this->mainElement)); + fwrite($this->getFile(), \sprintf('', $this->mainElement)); fclose($this->getFile()); } public function write(array $data): void { - fwrite($this->getFile(), sprintf("<%s>\n", $this->childElement)); + fwrite($this->getFile(), \sprintf("<%s>\n", $this->childElement)); foreach ($data as $k => $v) { $this->generateNode($k, $v); } - fwrite($this->getFile(), sprintf("\n", $this->childElement)); + fwrite($this->getFile(), \sprintf("\n", $this->childElement)); } /** @@ -94,7 +94,7 @@ private function generateNode(string $name, mixed $value): void throw new RuntimeException('Not implemented'); } if (\is_scalar($value) || null === $value) { - fwrite($this->getFile(), sprintf("<%s>\n", $name, (string) $value, $name)); + fwrite($this->getFile(), \sprintf("<%s>\n", $name, (string) $value, $name)); } else { throw new InvalidDataFormatException('Invalid data'); } diff --git a/tests/Writer/XlsxWriterTest.php b/tests/Writer/XlsxWriterTest.php index 6e3caa79..c60e8f4d 100644 --- a/tests/Writer/XlsxWriterTest.php +++ b/tests/Writer/XlsxWriterTest.php @@ -65,7 +65,7 @@ public function testValidDataFormat(): void $excelWriter = IOFactory::createWriter($spreadsheet, 'Csv'); $excelWriter->save($this->filenameCsv); - $expected = sprintf('"john ""2","doe","1"%s', \PHP_EOL); + $expected = \sprintf('"john ""2","doe","1"%s', \PHP_EOL); static::assertSame($expected, file_get_contents($this->filenameCsv)); } @@ -86,7 +86,7 @@ public function testWithHeaders(): void $excelWriter = IOFactory::createWriter($spreadsheet, 'Csv'); $excelWriter->save($this->filenameCsv); - $expected = sprintf('"firtname","surname","year"%s"john ""2","doe","1"%s', \PHP_EOL, \PHP_EOL); + $expected = \sprintf('"firtname","surname","year"%s"john ""2","doe","1"%s', \PHP_EOL, \PHP_EOL); static::assertSame($expected, file_get_contents($this->filenameCsv)); }