Skip to content

Commit

Permalink
fix(ci): new php-cs-fixer and phpstan rules (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Beru authored Sep 7, 2024
1 parent de08d60 commit 51ea807
Show file tree
Hide file tree
Showing 16 changed files with 26 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function saveFile(InputInterface $input, string $filename, string $contents): vo
$directory = __DIR__.'/'.$subDirectory;

if (!@mkdir($directory, recursive: true) && !is_dir($directory)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $directory));
throw new RuntimeException(\sprintf('Directory "%s" was not created', $directory));
}

$summary .= "## {$type}\n\n";
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/DefaultBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function encodeData(string $key, mixed $value): array
try {
$encodedValue = json_encode($value, \JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new JsonEncodingException(sprintf('Could not encode property "%s" into JSON', $key), previous: $exception);
throw new JsonEncodingException(\sprintf('Could not encode property "%s" into JSON', $key), previous: $exception);
}

return [$key => $encodedValue];
Expand Down Expand Up @@ -104,7 +104,7 @@ protected function assertFileExtension(string $path, array $validExtensions): vo
$extension = $file->getExtension();

if (!\in_array($extension, $validExtensions, true)) {
throw new \InvalidArgumentException(sprintf('The file extension "%s" is not valid in this context.', $extension));
throw new \InvalidArgumentException(\sprintf('The file extension "%s" is not valid in this context.', $extension));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Builder/Pdf/AbstractChromiumPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,13 @@ protected function withPdfPartFile(Part $pdfPart, string $path): static
protected function withRenderedPart(Part $pdfPart, string $template, array $context = []): static
{
if (!$this->twig instanceof Environment) {
throw new \LogicException(sprintf('Twig is required to use "%s" method. Try to run "composer require symfony/twig-bundle".', __METHOD__));
throw new \LogicException(\sprintf('Twig is required to use "%s" method. Try to run "composer require symfony/twig-bundle".', __METHOD__));
}

try {
$html = $this->twig->render($template, array_merge($context, ['_builder' => $this]));
} catch (\Throwable $error) {
throw new PdfPartRenderingException(sprintf('Could not render template "%s" into PDF part "%s". %s', $template, $pdfPart->value, $error->getMessage()), previous: $error);
throw new PdfPartRenderingException(\sprintf('Could not render template "%s" into PDF part "%s". %s', $template, $pdfPart->value, $error->getMessage()), previous: $error);
}

$this->formFields[$pdfPart->value] = new DataPart($html, $pdfPart->value, 'text/html');
Expand Down Expand Up @@ -586,7 +586,7 @@ protected function addConfiguration(string $configurationName, mixed $value): vo
'fail_on_console_exceptions' => $this->failOnConsoleExceptions($value),
'skip_network_idle_event' => $this->skipNetworkIdleEvent($value),
'metadata' => $this->metadata($value),
default => throw new InvalidBuilderConfiguration(sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, static::class)),
default => throw new InvalidBuilderConfiguration(\sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, static::class)),
};
}
}
2 changes: 1 addition & 1 deletion src/Builder/Pdf/ConvertPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private function addConfiguration(string $configurationName, mixed $value): void
match ($configurationName) {
'pdf_format' => $this->pdfFormat(PdfFormat::from($value)),
'pdf_universal_access' => $this->pdfUniversalAccess($value),
default => throw new InvalidBuilderConfiguration(sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, static::class)),
default => throw new InvalidBuilderConfiguration(\sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, static::class)),
};
}
}
2 changes: 1 addition & 1 deletion src/Builder/Pdf/LibreOfficePdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private function addConfiguration(string $configurationName, mixed $value): void
'quality' => $this->quality($value),
'reduce_image_resolution' => $this->reduceImageResolution($value),
'max_image_resolution' => $this->maxImageResolution(ImageResolutionDPI::from($value)),
default => throw new InvalidBuilderConfiguration(sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, static::class)),
default => throw new InvalidBuilderConfiguration(\sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, static::class)),
};
}
}
2 changes: 1 addition & 1 deletion src/Builder/Pdf/MergePdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private function addConfiguration(string $configurationName, mixed $value): void
'pdf_format' => $this->pdfFormat(PdfFormat::from($value)),
'pdf_universal_access' => $this->pdfUniversalAccess($value),
'metadata' => $this->metadata($value),
default => throw new InvalidBuilderConfiguration(sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, self::class)),
default => throw new InvalidBuilderConfiguration(\sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, self::class)),
};
}
}
2 changes: 1 addition & 1 deletion src/Builder/Pdf/UrlPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function url(string $url): self
public function route(string $name, array $parameters = []): self
{
if (null === $this->urlGenerator) {
throw new \LogicException(sprintf('Router is required to use "%s" method. Try to run "composer require symfony/routing".', __METHOD__));
throw new \LogicException(\sprintf('Router is required to use "%s" method. Try to run "composer require symfony/routing".', __METHOD__));
}

$this->formFields['route'] = [$name, $parameters];
Expand Down
6 changes: 3 additions & 3 deletions src/Builder/Screenshot/AbstractChromiumScreenshotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,13 @@ protected function withScreenshotPartFile(Part $screenshotPart, string $path): s
protected function withRenderedPart(Part $screenshotPart, string $template, array $context = []): static
{
if (!$this->twig instanceof Environment) {
throw new \LogicException(sprintf('Twig is required to use "%s" method. Try to run "composer require symfony/twig-bundle".', __METHOD__));
throw new \LogicException(\sprintf('Twig is required to use "%s" method. Try to run "composer require symfony/twig-bundle".', __METHOD__));
}

try {
$html = $this->twig->render($template, array_merge($context, ['_builder' => $this]));
} catch (\Throwable $error) {
throw new ScreenshotPartRenderingException(sprintf('Could not render template "%s" into Screenshot part "%s". %s', $template, $screenshotPart->value, $error->getMessage()), previous: $error);
throw new ScreenshotPartRenderingException(\sprintf('Could not render template "%s" into Screenshot part "%s". %s', $template, $screenshotPart->value, $error->getMessage()), previous: $error);
}

$this->formFields[$screenshotPart->value] = new DataPart($html, $screenshotPart->value, 'text/html');
Expand All @@ -383,7 +383,7 @@ private function addConfiguration(string $configurationName, mixed $value): void
'fail_on_http_status_codes' => $this->failOnHttpStatusCodes($value),
'fail_on_console_exceptions' => $this->failOnConsoleExceptions($value),
'skip_network_idle_event' => $this->skipNetworkIdleEvent($value),
default => throw new InvalidBuilderConfiguration(sprintf('Invalid option "%s": no method exists in class "%s" to configured it.', $configurationName, static::class)),
default => throw new InvalidBuilderConfiguration(\sprintf('Invalid option "%s": no method exists in class "%s" to configured it.', $configurationName, static::class)),
};
}
}
2 changes: 1 addition & 1 deletion src/Builder/Screenshot/UrlScreenshotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function url(string $url): self
public function route(string $name, array $parameters = []): self
{
if (null === $this->urlGenerator) {
throw new \LogicException(sprintf('Router is required to use "%s" method. Try to run "composer require symfony/routing".', __METHOD__));
throw new \LogicException(\sprintf('Router is required to use "%s" method. Try to run "composer require symfony/routing".', __METHOD__));
}

$this->formFields['route'] = [$name, $parameters];
Expand Down
6 changes: 2 additions & 4 deletions src/Client/GotenbergResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ public function getHeaders(): ResponseHeaderBag
public function getFileName(): string|null
{
$disposition = $this->headers->get('content-disposition', '');
if ('' !== $disposition) {
/* @see https://onlinephp.io/c/c2606 */
preg_match('#[^;]*;\sfilename="?(?P<fileName>[^"]*)"?#', $disposition, $matches);

/* @see https://onlinephp.io/c/c2606 */
if (1 === preg_match('#[^;]*;\sfilename="?(?P<fileName>[^"]*)"?#', $disposition, $matches)) {
return $matches['fileName'];
}

Expand Down
22 changes: 4 additions & 18 deletions src/Debug/Builder/TraceableScreenshotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class TraceableScreenshotBuilder implements ScreenshotBuilderInterface
{
/**
* @var list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string, 'calls': list<array{'method': string, 'class': class-string<ScreenshotBuilderInterface>, 'arguments': array<mixed>}>}>
* @var list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string|null, 'calls': list<array{'method': string, 'class': class-string<ScreenshotBuilderInterface>, 'arguments': array<mixed>}>}>
*/
private array $screenshots = [];

Expand Down Expand Up @@ -37,27 +37,13 @@ public function generate(): GotenbergFileResult
$response = $this->inner->generate();
$swEvent->stop();

$fileName = 'Unknown';
if ($response->getHeaders()->has('Content-Disposition')) {
$matches = [];

/* @see https://onlinephp.io/c/c2606 */
preg_match('#[^;]*;\sfilename="?(?P<fileName>[^"]*)"?#', $response->getHeaders()->get('Content-Disposition', ''), $matches);
$fileName = $matches['fileName'];
}

$lengthInBytes = null;
if ($response->getHeaders()->has('Content-Length')) {
$lengthInBytes = abs((int) $response->getHeaders()->get('Content-Length'));
}

$this->screenshots[] = [
'calls' => $this->calls,
'time' => $swEvent->getDuration(),
'memory' => $swEvent->getMemory(),
'status' => $response->getStatusCode(),
'size' => $lengthInBytes,
'fileName' => $fileName,
'size' => $response->getContentLength(),
'fileName' => $response->getFileName(),
];

++$this->totalGenerated;
Expand Down Expand Up @@ -86,7 +72,7 @@ public function __call(string $name, array $arguments): mixed
}

/**
* @return list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string, 'calls': list<array{'class': class-string<ScreenshotBuilderInterface>, 'method': string, 'arguments': array<mixed>}>}>
* @return list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string|null, 'calls': list<array{'class': class-string<ScreenshotBuilderInterface>, 'method': string, 'arguments': array<mixed>}>}>
*/
public function getFiles(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Enumeration/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum Unit: string
*/
public static function parse(string|int|float $raw, self $defaultUnit = self::Inches): array
{
[$value, $unit] = sscanf((string) $raw, '%f%s') ?? throw new \InvalidArgumentException(sprintf('Unexpected value "%s", expected format is "%%f%%s"', $raw));
[$value, $unit] = sscanf((string) $raw, '%f%s') ?? throw new \InvalidArgumentException(\sprintf('Unexpected value "%s", expected format is "%%f%%s"', $raw));

return [(float) $value, self::tryFrom((string) $unit) ?? $defaultUnit];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __invoke(string|null $fileName): \Generator

return new \SplFileInfo($path);
} catch (\Throwable $t) {
throw new ProcessorException(sprintf('Unable to write to "%s".', $fileName), previous : $t);
throw new ProcessorException(\sprintf('Unable to write to "%s".', $fileName), previous : $t);
} finally {
fclose($resource);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Builder/DefaultBuilderTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function canConvertToMultiPartProvider(): \Generator

yield 'any BackedEnum' => [PdfFormat::Pdf2b, [['key' => 'PDF/A-2b']]];

yield 'any Stringable' => [new class() implements \Stringable {
yield 'any Stringable' => [new class implements \Stringable {
public function __toString(): string
{
return __FUNCTION__;
Expand Down Expand Up @@ -109,7 +109,7 @@ public function testCanAssertFileExtensions(): void

private function getBuilder(): object
{
return new class() {
return new class {
use DefaultBuilderTrait {
addNormalizer as public;
encodeData as public;
Expand Down
2 changes: 1 addition & 1 deletion tests/Builder/Pdf/AbstractChromiumPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function testPaperSizeAppliesWidthAndHeight(): void

public function testPaperStandardSizeAppliesCorrectly(): void
{
$paperStandardSize = new class() implements PaperSizeInterface {
$paperStandardSize = new class implements PaperSizeInterface {
public function width(): float
{
return 10.0;
Expand Down
2 changes: 1 addition & 1 deletion tests/Client/GotenbergClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testCallIsCorrectlyFormatted(): void

/* @see https://onlinephp.io/c/e8233 */
preg_match('#^multipart/form-data; boundary=(?P<boundary>.*)$#', $requestContentType, $matches);
$boundary = $matches['boundary'];
$boundary = $matches['boundary'] ?? '';

$requestBody = $mockResponse->getRequestOptions()['body'];
self::assertSame(
Expand Down

0 comments on commit 51ea807

Please sign in to comment.