From 15d05f8d2edcb50e1c1b37786a48cb656c6268ac Mon Sep 17 00:00:00 2001 From: alexpozzi Date: Fri, 7 Feb 2020 15:45:37 +0100 Subject: [PATCH] phpstan level 8 CI check --- phpstan.neon | 4 +- src/Knp/Snappy/AbstractGenerator.php | 171 +++++++++++++-------- src/Knp/Snappy/GeneratorInterface.php | 8 +- src/Knp/Snappy/Image.php | 4 +- src/Knp/Snappy/Pdf.php | 17 +- tests/Knp/Snappy/AbstractGeneratorTest.php | 58 +++---- tests/Knp/Snappy/ImageTest.php | 2 +- tests/Knp/Snappy/PdfTest.php | 27 ++-- 8 files changed, 177 insertions(+), 114 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index c51e18d0..f99e8107 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,11 +2,13 @@ includes: - vendor/phpstan/phpstan-phpunit/extension.neon parameters: - level: 4 + level: 8 paths: - src/ - tests/ inferPrivatePropertyTypeFromConstructor: true reportUnmatchedIgnoredErrors: false + checkMissingIterableValueType: false ignoreErrors: - "#^Call to an undefined static method #" + - "#^Parameter \\#1 \\$command of class Symfony\\\\Component\\\\Process\\\\Process constructor expects array, string given\\.$#" diff --git a/src/Knp/Snappy/AbstractGenerator.php b/src/Knp/Snappy/AbstractGenerator.php index 8f878e14..e647f182 100644 --- a/src/Knp/Snappy/AbstractGenerator.php +++ b/src/Knp/Snappy/AbstractGenerator.php @@ -17,10 +17,29 @@ */ abstract class AbstractGenerator implements GeneratorInterface, LoggerAwareInterface { + /** + * @var string|null + */ private $binary; + + /** + * @var array + */ private $options = []; + + /** + * @var array|null + */ private $env; - private $timeout = false; + + /** + * @var int|null + */ + private $timeout; + + /** + * @var string + */ private $defaultExtension; /** @@ -39,11 +58,11 @@ abstract class AbstractGenerator implements GeneratorInterface, LoggerAwareInter private $logger; /** - * @param string $binary - * @param array $options - * @param array $env + * @param string|null $binary + * @param array $options + * @param array|null $env */ - public function __construct($binary, array $options = [], array $env = null) + public function __construct(string $binary = null, array $options = [], array $env = null) { $this->configure(); @@ -67,9 +86,11 @@ public function __destruct() * * @param LoggerInterface $logger */ - public function setLogger(LoggerInterface $logger) + public function setLogger(LoggerInterface $logger): self { $this->logger = $logger; + + return $this; } /** @@ -77,7 +98,7 @@ public function setLogger(LoggerInterface $logger) * * @see AbstractGenerator::addOption() */ - abstract protected function configure(); + abstract protected function configure(): void; /** * Sets the default extension. @@ -85,9 +106,11 @@ abstract protected function configure(); * * @param string $defaultExtension */ - public function setDefaultExtension($defaultExtension) + public function setDefaultExtension(string $defaultExtension): self { $this->defaultExtension = $defaultExtension; + + return $this; } /** @@ -95,7 +118,7 @@ public function setDefaultExtension($defaultExtension) * * @return string */ - public function getDefaultExtension() + public function getDefaultExtension(): string { return $this->defaultExtension; } @@ -109,7 +132,7 @@ public function getDefaultExtension() * * @throws \InvalidArgumentException */ - public function setOption($name, $value) + public function setOption(string $name, $value): self { if (!array_key_exists($name, $this->options)) { throw new \InvalidArgumentException(sprintf('The option \'%s\' does not exist.', $name)); @@ -118,16 +141,20 @@ public function setOption($name, $value) $this->options[$name] = $value; $this->logger->debug(sprintf('Set option "%s".', $name), ['value' => $value]); + + return $this; } /** * Sets the timeout. * - * @param int $timeout The timeout to set + * @param int|null $timeout The timeout to set */ - public function setTimeout($timeout) + public function setTimeout(?int $timeout): self { $this->timeout = $timeout; + + return $this; } /** @@ -135,11 +162,13 @@ public function setTimeout($timeout) * * @param array $options An associative array of options as name/value */ - public function setOptions(array $options) + public function setOptions(array $options): self { foreach ($options as $name => $value) { $this->setOption($name, $value); } + + return $this; } /** @@ -147,7 +176,7 @@ public function setOptions(array $options) * * @return array */ - public function getOptions() + public function getOptions(): array { return $this->options; } @@ -155,14 +184,8 @@ public function getOptions() /** * {@inheritdoc} */ - public function generate($input, $output, array $options = [], $overwrite = false) + public function generate($input, string $output, array $options = [], bool $overwrite = false): void { - if (null === $this->binary) { - throw new \LogicException( - 'You must define a binary prior to conversion.' - ); - } - $this->prepareOutput($output, $overwrite); $command = $this->getCommand($input, $output, $options); @@ -200,7 +223,7 @@ public function generate($input, $output, array $options = [], $overwrite = fals /** * {@inheritdoc} */ - public function generateFromHtml($html, $output, array $options = [], $overwrite = false) + public function generateFromHtml($html, string $output, array $options = [], bool $overwrite = false): void { $fileNames = []; if (is_array($html)) { @@ -217,21 +240,19 @@ public function generateFromHtml($html, $output, array $options = [], $overwrite /** * {@inheritdoc} */ - public function getOutput($input, array $options = []) + public function getOutput($input, array $options = []): string { $filename = $this->createTemporaryFile(null, $this->getDefaultExtension()); $this->generate($input, $filename, $options); - $result = $this->getFileContents($filename); - - return $result; + return $this->getFileContents($filename); } /** * {@inheritdoc} */ - public function getOutputFromHtml($html, array $options = []) + public function getOutputFromHtml($html, array $options = []): string { $fileNames = []; if (is_array($html)) { @@ -242,27 +263,27 @@ public function getOutputFromHtml($html, array $options = []) $fileNames[] = $this->createTemporaryFile($html, 'html'); } - $result = $this->getOutput($fileNames, $options); - - return $result; + return $this->getOutput($fileNames, $options); } /** * Defines the binary. * - * @param string $binary The path/name of the binary + * @param string|null $binary The path/name of the binary */ - public function setBinary($binary) + public function setBinary(?string $binary): self { $this->binary = $binary; + + return $this; } /** * Returns the binary. * - * @return string + * @return string|null */ - public function getBinary() + public function getBinary(): ?string { return $this->binary; } @@ -277,8 +298,14 @@ public function getBinary() * * @return string */ - public function getCommand($input, $output, array $options = []) + public function getCommand($input, string $output, array $options = []): string { + if (null === $this->binary) { + throw new \LogicException( + 'You must define a binary prior to conversion.' + ); + } + $options = $this->mergeOptions($options); return $this->buildCommand($this->binary, $input, $output, $options); @@ -292,13 +319,15 @@ public function getCommand($input, $output, array $options = []) * * @throws \InvalidArgumentException */ - protected function addOption($name, $default = null) + protected function addOption(string $name, $default = null): self { if (array_key_exists($name, $this->options)) { throw new \InvalidArgumentException(sprintf('The option \'%s\' already exists.', $name)); } $this->options[$name] = $default; + + return $this; } /** @@ -306,11 +335,13 @@ protected function addOption($name, $default = null) * * @param array $options */ - protected function addOptions(array $options) + protected function addOptions(array $options): self { foreach ($options as $name => $default) { $this->addOption($name, $default); } + + return $this; } /** @@ -323,7 +354,7 @@ protected function addOptions(array $options) * * @return array */ - protected function mergeOptions(array $options) + protected function mergeOptions(array $options): array { $mergedOptions = $this->options; @@ -346,7 +377,7 @@ protected function mergeOptions(array $options) * * @throws \RuntimeException if the output file generation failed */ - protected function checkOutput($output, $command) + protected function checkOutput(string $output, string $command): void { // the output file must exist if (!$this->fileExists($output)) { @@ -377,7 +408,7 @@ protected function checkOutput($output, $command) * * @throws \RuntimeException if the output file generation failed */ - protected function checkProcessStatus($status, $stdout, $stderr, $command) + protected function checkProcessStatus(int $status, string $stdout, string $stderr, string $command): void { if (0 !== $status and '' !== $stderr) { throw new \RuntimeException(sprintf( @@ -397,12 +428,12 @@ protected function checkProcessStatus($status, $stdout, $stderr, $command) * Creates a temporary file. * The file is not created if the $content argument is null. * - * @param string $content Optional content for the temporary file - * @param string $extension An optional extension for the filename + * @param string|null $content Optional content for the temporary file + * @param string|null $extension An optional extension for the filename * * @return string The filename */ - protected function createTemporaryFile($content = null, $extension = null) + protected function createTemporaryFile(?string $content = null, ?string $extension = null): string { $dir = rtrim($this->getTemporaryFolder(), DIRECTORY_SEPARATOR); @@ -449,7 +480,7 @@ public function removeTemporaryFiles(): void * * @return string */ - protected function buildCommand($binary, $input, $output, array $options = []) + protected function buildCommand(string $binary, $input, string $output, array $options = []): string { $command = $binary; $escapedBinary = escapeshellarg($binary); @@ -509,7 +540,7 @@ protected function buildCommand($binary, $input, $output, array $options = []) * * @return bool */ - protected function isAssociativeArray(array $array) + protected function isAssociativeArray(array $array): bool { return (bool) count(array_filter(array_keys($array), 'is_string')); } @@ -522,7 +553,7 @@ protected function isAssociativeArray(array $array) * * @return array(status, stdout, stderr) */ - protected function executeCommand($command) + protected function executeCommand(string $command): array { if (method_exists(Process::class, 'fromShellCommandline')) { $process = Process::fromShellCommandline($command, null, $this->env); @@ -530,7 +561,7 @@ protected function executeCommand($command) $process = new Process($command, null, $this->env); } - if (false !== $this->timeout) { + if (null !== $this->timeout) { $process->setTimeout($this->timeout); } @@ -554,7 +585,7 @@ protected function executeCommand($command) * @throws \RuntimeException * @throws \InvalidArgumentException */ - protected function prepareOutput($filename, $overwrite) + protected function prepareOutput(string $filename, bool $overwrite): void { $directory = dirname($filename); @@ -589,7 +620,7 @@ protected function prepareOutput($filename, $overwrite) * * @return string */ - public function getTemporaryFolder() + public function getTemporaryFolder(): string { if ($this->temporaryFolder === null) { return sys_get_temp_dir(); @@ -605,7 +636,7 @@ public function getTemporaryFolder() * * @return $this */ - public function setTemporaryFolder($temporaryFolder) + public function setTemporaryFolder(string $temporaryFolder): self { $this->temporaryFolder = $temporaryFolder; @@ -619,9 +650,18 @@ public function setTemporaryFolder($temporaryFolder) * * @return string */ - protected function getFileContents($filename) + protected function getFileContents(string $filename): string { - return file_get_contents($filename); + $fileContent = file_get_contents($filename); + + if (false === $fileContent) { + throw new \RuntimeException(sprintf( + 'Could not read file \'%s\' content.', + $filename + )); + } + + return $fileContent; } /** @@ -631,7 +671,7 @@ protected function getFileContents($filename) * * @return bool */ - protected function fileExists($filename) + protected function fileExists(string $filename): bool { return file_exists($filename); } @@ -643,7 +683,7 @@ protected function fileExists($filename) * * @return bool */ - protected function isFile($filename) + protected function isFile(string $filename): bool { return strlen($filename) <= PHP_MAXPATHLEN && is_file($filename); } @@ -653,11 +693,20 @@ protected function isFile($filename) * * @param string $filename * - * @return int or FALSE on failure + * @return int */ - protected function filesize($filename) + protected function filesize(string $filename): int { - return filesize($filename); + $filesize = filesize($filename); + + if (false === $filesize) { + throw new \RuntimeException(sprintf( + 'Could not read file \'%s\' size.', + $filename + )); + } + + return $filesize; } /** @@ -667,7 +716,7 @@ protected function filesize($filename) * * @return bool */ - protected function unlink($filename) + protected function unlink(string $filename): bool { return $this->fileExists($filename) ? unlink($filename) : false; } @@ -679,7 +728,7 @@ protected function unlink($filename) * * @return bool */ - protected function isDir($filename) + protected function isDir(string $filename): bool { return is_dir($filename); } @@ -691,7 +740,7 @@ protected function isDir($filename) * * @return bool */ - protected function mkdir($pathname) + protected function mkdir(string $pathname): bool { return mkdir($pathname, 0777, true); } @@ -701,7 +750,7 @@ protected function mkdir($pathname) * * @return void */ - public function resetOptions() + public function resetOptions(): void { $this->options = []; $this->configure(); diff --git a/src/Knp/Snappy/GeneratorInterface.php b/src/Knp/Snappy/GeneratorInterface.php index cd049dbe..5a65e7e8 100644 --- a/src/Knp/Snappy/GeneratorInterface.php +++ b/src/Knp/Snappy/GeneratorInterface.php @@ -19,7 +19,7 @@ interface GeneratorInterface * @param array $options An array of options for this generation only * @param bool $overwrite Overwrite the file if it exists. If not, throw a FileAlreadyExistsException */ - public function generate($input, $output, array $options = [], $overwrite = false); + public function generate($input, string $output, array $options = [], bool $overwrite = false): void; /** * Generates the output media file from the given HTML. @@ -29,7 +29,7 @@ public function generate($input, $output, array $options = [], $overwrite = fals * @param array $options An array of options for this generation only * @param bool $overwrite Overwrite the file if it exists. If not, throw a FileAlreadyExistsException */ - public function generateFromHtml($html, $output, array $options = [], $overwrite = false); + public function generateFromHtml($html, string $output, array $options = [], bool $overwrite = false): void; /** * Returns the output of the media generated from the specified input HTML @@ -40,7 +40,7 @@ public function generateFromHtml($html, $output, array $options = [], $overwrite * * @return string */ - public function getOutput($input, array $options = []); + public function getOutput($input, array $options = []): string; /** * Returns the output of the media generated from the given HTML. @@ -50,5 +50,5 @@ public function getOutput($input, array $options = []); * * @return string */ - public function getOutputFromHtml($html, array $options = []); + public function getOutputFromHtml($html, array $options = []): string; } diff --git a/src/Knp/Snappy/Image.php b/src/Knp/Snappy/Image.php index 60c422fe..78f41761 100644 --- a/src/Knp/Snappy/Image.php +++ b/src/Knp/Snappy/Image.php @@ -14,7 +14,7 @@ class Image extends AbstractGenerator /** * {@inheritdoc} */ - public function __construct($binary = null, array $options = [], array $env = null) + public function __construct(string $binary = null, array $options = [], array $env = null) { $this->setDefaultExtension('jpg'); @@ -24,7 +24,7 @@ public function __construct($binary = null, array $options = [], array $env = nu /** * {@inheritdoc} */ - protected function configure() + protected function configure(): void { $this->addOptions([ 'allow' => null, // Allow the file or files from the specified folder to be loaded (repeatable) diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index 90a5c743..9cc22861 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -11,12 +11,15 @@ */ class Pdf extends AbstractGenerator { + /** + * @var array + */ protected $optionsWithContentCheck = []; /** * {@inheritdoc} */ - public function __construct($binary = null, array $options = [], array $env = null) + public function __construct(string $binary = null, array $options = [], array $env = null) { $this->setDefaultExtension('pdf'); $this->setOptionsWithContentCheck(); @@ -31,7 +34,7 @@ public function __construct($binary = null, array $options = [], array $env = nu * * @return array $options Transformed options */ - protected function handleOptions(array $options = []) + protected function handleOptions(array $options = []): array { foreach ($options as $option => $value) { if (null === $value) { @@ -56,7 +59,7 @@ protected function handleOptions(array $options = []) /** * {@inheritdoc} */ - public function generate($input, $output, array $options = [], $overwrite = false) + public function generate($input, string $output, array $options = [], bool $overwrite = false): void { $options = $this->handleOptions($this->mergeOptions($options)); @@ -70,7 +73,7 @@ public function generate($input, $output, array $options = [], $overwrite = fals * * @return bool */ - protected function isOptionUrl($option) + protected function isOptionUrl($option): bool { return (bool) filter_var($option, FILTER_VALIDATE_URL); } @@ -78,7 +81,7 @@ protected function isOptionUrl($option) /** * {@inheritdoc} */ - protected function configure() + protected function configure(): void { $this->addOptions([ // Global options @@ -226,7 +229,7 @@ protected function configure() /** * Array with options which require to store the content of the option before passing it to wkhtmltopdf. */ - protected function setOptionsWithContentCheck() + protected function setOptionsWithContentCheck(): self { $this->optionsWithContentCheck = [ 'header-html' => 'html', @@ -234,5 +237,7 @@ protected function setOptionsWithContentCheck() 'cover' => 'html', 'xsl-style-sheet'=> 'xsl', ]; + + return $this; } } diff --git a/tests/Knp/Snappy/AbstractGeneratorTest.php b/tests/Knp/Snappy/AbstractGeneratorTest.php index a6a8989b..df8c28df 100644 --- a/tests/Knp/Snappy/AbstractGeneratorTest.php +++ b/tests/Knp/Snappy/AbstractGeneratorTest.php @@ -8,7 +8,7 @@ class AbstractGeneratorTest extends TestCase { - public function testAddOption() + public function testAddOption(): void { $media = $this->getMockForAbstractClass(AbstractGenerator::class, [], '', false); @@ -41,7 +41,7 @@ public function testAddOption() } } - public function testAddOptions() + public function testAddOptions(): void { $media = $this->getMockForAbstractClass(AbstractGenerator::class, [], '', false); @@ -83,7 +83,7 @@ public function testAddOptions() } } - public function testSetOption() + public function testSetOption(): void { $media = $this ->getMockBuilder(AbstractGenerator::class) @@ -122,7 +122,7 @@ public function testSetOption() } } - public function testSetOptions() + public function testSetOptions(): void { $media = $this ->getMockBuilder(AbstractGenerator::class) @@ -162,7 +162,7 @@ public function testSetOptions() } } - public function testGenerate() + public function testGenerate(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -235,7 +235,7 @@ public function testGenerate() $media->generate('the_input_file', 'the_output_file', ['foo' => 'bar']); } - public function testFailingGenerate() + public function testFailingGenerate(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -304,7 +304,7 @@ public function testFailingGenerate() $media->generate('the_input_file', 'the_output_file', ['foo' => 'bar']); } - public function testGenerateFromHtml() + public function testGenerateFromHtml(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -339,7 +339,7 @@ public function testGenerateFromHtml() $media->generateFromHtml('foo', 'the_output_file', ['foo' => 'bar']); } - public function testGenerateFromHtmlWithHtmlArray() + public function testGenerateFromHtmlWithHtmlArray(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -373,7 +373,7 @@ public function testGenerateFromHtmlWithHtmlArray() $media->generateFromHtml(['foo'], 'the_output_file', ['foo' => 'bar']); } - public function testGetOutput() + public function testGetOutput(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -426,7 +426,7 @@ public function testGetOutput() $this->assertEquals('the file contents', $media->getOutput('the_input_file', ['foo' => 'bar'])); } - public function testGetOutputFromHtml() + public function testGetOutputFromHtml(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -459,7 +459,7 @@ public function testGetOutputFromHtml() $this->assertEquals('the output', $media->getOutputFromHtml('foo', ['foo' => 'bar'])); } - public function testGetOutputFromHtmlWithHtmlArray() + public function testGetOutputFromHtmlWithHtmlArray(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -492,7 +492,7 @@ public function testGetOutputFromHtmlWithHtmlArray() $this->assertEquals('the output', $media->getOutputFromHtml(['foo'], ['foo' => 'bar'])); } - public function testMergeOptions() + public function testMergeOptions(): void { $media = $this->getMockForAbstractClass(AbstractGenerator::class, [], '', false); @@ -546,7 +546,7 @@ public function testMergeOptions() /** * @dataProvider dataForBuildCommand */ - public function testBuildCommand($binary, $url, $path, $options, $expected) + public function testBuildCommand(string $binary, string $url, string $path, array $options, string $expected): void { $media = $this->getMockForAbstractClass(AbstractGenerator::class, [], '', false); @@ -556,7 +556,7 @@ public function testBuildCommand($binary, $url, $path, $options, $expected) $this->assertEquals($expected, $r->invokeArgs($media, [$binary, $url, $path, $options])); } - private function getPHPExecutableFromPath() + private function getPHPExecutableFromPath(): ?string { if (isset($_SERVER['_'])) { return $_SERVER['_']; @@ -566,6 +566,10 @@ private function getPHPExecutableFromPath() return PHP_BINARY; } + if (false === getenv('PATH')) { + return null; + } + $paths = explode(PATH_SEPARATOR, getenv('PATH')); foreach ($paths as $path) { // we need this for XAMPP (Windows) @@ -579,10 +583,10 @@ private function getPHPExecutableFromPath() } } - return false; // not found + return null; // not found } - public function dataForBuildCommand() + public function dataForBuildCommand(): array { $theBinary = $this->getPHPExecutableFromPath() . ' -v'; // i.e.: '/usr/bin/php -v' @@ -649,7 +653,7 @@ public function dataForBuildCommand() ]; } - public function testCheckOutput() + public function testCheckOutput(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -686,7 +690,7 @@ public function testCheckOutput() } } - public function testCheckOutputWhenTheFileDoesNotExist() + public function testCheckOutputWhenTheFileDoesNotExist(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -717,7 +721,7 @@ public function testCheckOutputWhenTheFileDoesNotExist() } } - public function testCheckOutputWhenTheFileIsEmpty() + public function testCheckOutputWhenTheFileIsEmpty(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -755,7 +759,7 @@ public function testCheckOutputWhenTheFileIsEmpty() } } - public function testCheckProcessStatus() + public function testCheckProcessStatus(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods(['configure']) @@ -791,7 +795,7 @@ public function testCheckProcessStatus() /** * @dataProvider dataForIsAssociativeArray */ - public function testIsAssociativeArray($array, $isAssociativeArray) + public function testIsAssociativeArray(array $array, bool $isAssociativeArray): void { $generator = $this->getMockForAbstractClass(AbstractGenerator::class, [], '', false); @@ -803,7 +807,7 @@ public function testIsAssociativeArray($array, $isAssociativeArray) /** * @expectedException Knp\Snappy\Exception\FileAlreadyExistsException */ - public function testItThrowsTheProperExceptionWhenFileExistsAndNotOverwritting() + public function testItThrowsTheProperExceptionWhenFileExistsAndNotOverwritting(): void { $media = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -831,7 +835,7 @@ public function testItThrowsTheProperExceptionWhenFileExistsAndNotOverwritting() $r->invokeArgs($media, ['', false]); } - public function dataForIsAssociativeArray() + public function dataForIsAssociativeArray(): array { return [ [ @@ -869,7 +873,7 @@ public function dataForIsAssociativeArray() ]; } - public function testCleanupEmptyTemporaryFiles() + public function testCleanupEmptyTemporaryFiles(): void { $generator = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -897,7 +901,7 @@ public function testCleanupEmptyTemporaryFiles() $remove->invoke($generator); } - public function testleanupTemporaryFiles() + public function testleanupTemporaryFiles(): void { $generator = $this->getMockBuilder(AbstractGenerator::class) ->setMethods([ @@ -925,10 +929,10 @@ public function testleanupTemporaryFiles() $remove->invoke($generator); } - public function testResetOptions() + public function testResetOptions(): void { $media = new class('/usr/local/bin/wkhtmltopdf') extends AbstractGenerator { - protected function configure() + protected function configure(): void { $this->addOptions([ 'optionA' => null, diff --git a/tests/Knp/Snappy/ImageTest.php b/tests/Knp/Snappy/ImageTest.php index 68b64bc0..6c2bb383 100644 --- a/tests/Knp/Snappy/ImageTest.php +++ b/tests/Knp/Snappy/ImageTest.php @@ -7,7 +7,7 @@ class ImageTest extends TestCase { - public function testCreateInstance() + public function testCreateInstance(): void { $testObject = new Image(); $this->assertInstanceOf(Image::class, $testObject); diff --git a/tests/Knp/Snappy/PdfTest.php b/tests/Knp/Snappy/PdfTest.php index 39c844be..bcfe3c4f 100644 --- a/tests/Knp/Snappy/PdfTest.php +++ b/tests/Knp/Snappy/PdfTest.php @@ -10,7 +10,7 @@ class PdfTest extends TestCase { const SHELL_ARG_QUOTE_REGEX = '(?:"|\')'; // escapeshellarg produces double quotes on Windows, single quotes otherwise - public function tearDown() + public function tearDown(): void { $directory = __DIR__ . '/i-dont-exist'; @@ -39,13 +39,13 @@ function ($filename) { } } - public function testCreateInstance() + public function testCreateInstance(): void { $testObject = new Pdf(); $this->assertInstanceOf(Pdf::class, $testObject); } - public function testThatSomethingUsingTmpFolder() + public function testThatSomethingUsingTmpFolder(): void { $q = self::SHELL_ARG_QUOTE_REGEX; $testObject = new PdfSpy(); @@ -55,7 +55,7 @@ public function testThatSomethingUsingTmpFolder() $this->assertRegExp('/emptyBinary --lowquality --footer-html ' . $q . '.*' . $q . ' ' . $q . '.*' . $q . ' ' . $q . '.*' . $q . '/', $testObject->getLastCommand()); } - public function testThatSomethingUsingNonexistentTmpFolder() + public function testThatSomethingUsingNonexistentTmpFolder(): void { $temporaryFolder = sys_get_temp_dir() . '/i-dont-exist'; @@ -67,7 +67,7 @@ public function testThatSomethingUsingNonexistentTmpFolder() $this->assertDirectoryExists($temporaryFolder); } - public function testRemovesLocalFilesOnError() + public function testRemovesLocalFilesOnError(): void { $pdf = new PdfSpy(); $method = new \ReflectionMethod($pdf, 'createTemporaryFile'); @@ -82,14 +82,14 @@ public function testRemovesLocalFilesOnError() /** * @dataProvider dataOptions */ - public function testOptions(array $options, $expectedRegex) + public function testOptions(array $options, string $expectedRegex): void { $testObject = new PdfSpy(); $testObject->getOutputFromHtml('', $options); $this->assertRegExp($expectedRegex, $testObject->getLastCommand()); } - public function dataOptions() + public function dataOptions(): array { $q = self::SHELL_ARG_QUOTE_REGEX; @@ -131,7 +131,7 @@ public function dataOptions() ]; } - public function testRemovesLocalFilesOnDestruct() + public function testRemovesLocalFilesOnDestruct(): void { $pdf = new PdfSpy(); $method = new \ReflectionMethod($pdf, 'createTemporaryFile'); @@ -146,6 +146,9 @@ public function testRemovesLocalFilesOnDestruct() class PdfSpy extends Pdf { + /** + * @var string + */ private $lastCommand; public function __construct() @@ -153,24 +156,24 @@ public function __construct() parent::__construct('emptyBinary'); } - public function getLastCommand() + public function getLastCommand(): string { return $this->lastCommand; } - protected function executeCommand($command) + protected function executeCommand(string $command): array { $this->lastCommand = $command; return [0, 'output', 'errorOutput']; } - protected function checkOutput($output, $command) + protected function checkOutput(string $output, string $command): void { //let's say everything went right } - public function getOutput($input, array $options = []) + public function getOutput($input, array $options = []): string { $filename = $this->createTemporaryFile(null, $this->getDefaultExtension()); $this->generate($input, $filename, $options, true);