Skip to content

Commit

Permalink
Merge pull request #439 from KnpLabs/fix/function-signature-incompati…
Browse files Browse the repository at this point in the history
…bilities

fix: function signatures incompatibilities with previous version
  • Loading branch information
alexpozzi authored Oct 22, 2021
2 parents ae8846a + d14b108 commit 349c2e8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 44 deletions.
88 changes: 56 additions & 32 deletions src/Knp/Snappy/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ abstract class AbstractGenerator implements GeneratorInterface, LoggerAwareInter
* @param array $options
* @param null|array $env
*/
public function __construct(string $binary = null, array $options = [], array $env = null)
public function __construct($binary, array $options = [], array $env = null)
{
$this->configure();

Expand All @@ -88,8 +88,10 @@ public function __destruct()
* Set the logger to use to log debugging data.
*
* @param LoggerInterface $logger
*
* @return $this
*/
public function setLogger(LoggerInterface $logger): self
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;

Expand All @@ -101,8 +103,10 @@ public function setLogger(LoggerInterface $logger): self
* Useful when letting Snappy deal with file creation.
*
* @param string $defaultExtension
*
* @return $this
*/
public function setDefaultExtension(string $defaultExtension): self
public function setDefaultExtension($defaultExtension)
{
$this->defaultExtension = $defaultExtension;

Expand All @@ -127,8 +131,10 @@ public function getDefaultExtension(): string
* @param mixed $value The value (NULL to unset)
*
* @throws InvalidArgumentException
*
* @return $this
*/
public function setOption(string $name, $value): self
public function setOption($name, $value)
{
if (!\array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(\sprintf('The option \'%s\' does not exist.', $name));
Expand All @@ -145,8 +151,10 @@ public function setOption(string $name, $value): self
* Sets the timeout.
*
* @param null|int $timeout The timeout to set
*
* @return $this
*/
public function setTimeout(?int $timeout): self
public function setTimeout($timeout)
{
$this->timeout = $timeout;

Expand All @@ -157,8 +165,10 @@ public function setTimeout(?int $timeout): self
* Sets an array of options.
*
* @param array $options An associative array of options as name/value
*
* @return $this
*/
public function setOptions(array $options): self
public function setOptions(array $options)
{
foreach ($options as $name => $value) {
$this->setOption($name, $value);
Expand All @@ -172,7 +182,7 @@ public function setOptions(array $options): self
*
* @return array
*/
public function getOptions(): array
public function getOptions()
{
return $this->options;
}
Expand Down Expand Up @@ -266,8 +276,10 @@ public function getOutputFromHtml($html, array $options = [])
* Defines the binary.
*
* @param null|string $binary The path/name of the binary
*
* @return $this
*/
public function setBinary(?string $binary): self
public function setBinary($binary)
{
$this->binary = $binary;

Expand All @@ -279,7 +291,7 @@ public function setBinary(?string $binary): self
*
* @return null|string
*/
public function getBinary(): ?string
public function getBinary()
{
return $this->binary;
}
Expand All @@ -294,7 +306,7 @@ public function getBinary(): ?string
*
* @return string
*/
public function getCommand($input, string $output, array $options = []): string
public function getCommand($input, $output, array $options = [])
{
if (null === $this->binary) {
throw new LogicException('You must define a binary prior to conversion.');
Expand All @@ -307,8 +319,10 @@ public function getCommand($input, string $output, array $options = []): string

/**
* Removes all temporary files.
*
* @return void
*/
public function removeTemporaryFiles(): void
public function removeTemporaryFiles()
{
foreach ($this->temporaryFiles as $file) {
$this->unlink($file);
Expand All @@ -320,7 +334,7 @@ public function removeTemporaryFiles(): void
*
* @return string
*/
public function getTemporaryFolder(): string
public function getTemporaryFolder()
{
if ($this->temporaryFolder === null) {
return \sys_get_temp_dir();
Expand All @@ -336,7 +350,7 @@ public function getTemporaryFolder(): string
*
* @return $this
*/
public function setTemporaryFolder(string $temporaryFolder): self
public function setTemporaryFolder($temporaryFolder)
{
$this->temporaryFolder = $temporaryFolder;

Expand All @@ -348,7 +362,7 @@ public function setTemporaryFolder(string $temporaryFolder): self
*
* @return void
*/
public function resetOptions(): void
public function resetOptions()
{
$this->options = [];
$this->configure();
Expand All @@ -361,7 +375,7 @@ public function resetOptions(): void
*
* @see AbstractGenerator::addOption()
*/
abstract protected function configure(): void;
abstract protected function configure();

/**
* Adds an option.
Expand All @@ -370,8 +384,10 @@ abstract protected function configure(): void;
* @param mixed $default An optional default value
*
* @throws InvalidArgumentException
*
* @return $this
*/
protected function addOption(string $name, $default = null): self
protected function addOption($name, $default = null)
{
if (\array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(\sprintf('The option \'%s\' already exists.', $name));
Expand All @@ -386,8 +402,10 @@ protected function addOption(string $name, $default = null): self
* Adds an array of options.
*
* @param array $options
*
* @return $this
*/
protected function addOptions(array $options): self
protected function addOptions(array $options)
{
foreach ($options as $name => $default) {
$this->addOption($name, $default);
Expand All @@ -406,7 +424,7 @@ protected function addOptions(array $options): self
*
* @return array
*/
protected function mergeOptions(array $options): array
protected function mergeOptions(array $options)
{
$mergedOptions = $this->options;

Expand All @@ -428,8 +446,10 @@ protected function mergeOptions(array $options): array
* @param string $command The generation command
*
* @throws RuntimeException if the output file generation failed
*
* @return void
*/
protected function checkOutput(string $output, string $command): void
protected function checkOutput($output, $command)
{
// the output file must exist
if (!$this->fileExists($output)) {
Expand All @@ -451,8 +471,10 @@ protected function checkOutput(string $output, string $command): void
* @param string $command The run command
*
* @throws RuntimeException if the output file generation failed
*
* @return void
*/
protected function checkProcessStatus(int $status, string $stdout, string $stderr, string $command): void
protected function checkProcessStatus($status, $stdout, $stderr, $command)
{
if (0 !== $status && '' !== $stderr) {
throw new RuntimeException(\sprintf('The exit status code \'%s\' says something went wrong:' . "\n" . 'stderr: "%s"' . "\n" . 'stdout: "%s"' . "\n" . 'command: %s.', $status, $stderr, $stdout, $command), $status);
Expand All @@ -468,7 +490,7 @@ protected function checkProcessStatus(int $status, string $stdout, string $stder
*
* @return string The filename
*/
protected function createTemporaryFile(?string $content = null, ?string $extension = null): string
protected function createTemporaryFile($content = null, $extension = null)
{
$dir = \rtrim($this->getTemporaryFolder(), \DIRECTORY_SEPARATOR);

Expand Down Expand Up @@ -505,7 +527,7 @@ protected function createTemporaryFile(?string $content = null, ?string $extensi
*
* @return string
*/
protected function buildCommand(string $binary, $input, string $output, array $options = []): string
protected function buildCommand($binary, $input, $output, array $options = [])
{
$command = $binary;
$escapedBinary = \escapeshellarg($binary);
Expand Down Expand Up @@ -565,7 +587,7 @@ protected function buildCommand(string $binary, $input, string $output, array $o
*
* @return bool
*/
protected function isAssociativeArray(array $array): bool
protected function isAssociativeArray(array $array)
{
return (bool) \count(\array_filter(\array_keys($array), 'is_string'));
}
Expand All @@ -578,7 +600,7 @@ protected function isAssociativeArray(array $array): bool
*
* @return array [status, stdout, stderr]
*/
protected function executeCommand(string $command): array
protected function executeCommand($command)
{
if (\method_exists(Process::class, 'fromShellCommandline')) {
$process = Process::fromShellCommandline($command, null, $this->env);
Expand Down Expand Up @@ -609,8 +631,10 @@ protected function executeCommand(string $command): array
* @throws FileAlreadyExistsException
* @throws RuntimeException
* @throws InvalidArgumentException
*
* @return void
*/
protected function prepareOutput(string $filename, bool $overwrite): void
protected function prepareOutput($filename, $overwrite)
{
$directory = \dirname($filename);

Expand All @@ -636,7 +660,7 @@ protected function prepareOutput(string $filename, bool $overwrite): void
*
* @return string
*/
protected function getFileContents(string $filename): string
protected function getFileContents($filename)
{
$fileContent = \file_get_contents($filename);

Expand All @@ -654,7 +678,7 @@ protected function getFileContents(string $filename): string
*
* @return bool
*/
protected function fileExists(string $filename): bool
protected function fileExists($filename)
{
return \file_exists($filename);
}
Expand All @@ -666,7 +690,7 @@ protected function fileExists(string $filename): bool
*
* @return bool
*/
protected function isFile(string $filename): bool
protected function isFile($filename)
{
return \strlen($filename) <= \PHP_MAXPATHLEN && \is_file($filename);
}
Expand All @@ -678,7 +702,7 @@ protected function isFile(string $filename): bool
*
* @return int
*/
protected function filesize(string $filename): int
protected function filesize($filename)
{
$filesize = \filesize($filename);

Expand All @@ -696,7 +720,7 @@ protected function filesize(string $filename): int
*
* @return bool
*/
protected function unlink(string $filename): bool
protected function unlink($filename)
{
return $this->fileExists($filename) ? \unlink($filename) : false;
}
Expand All @@ -708,7 +732,7 @@ protected function unlink(string $filename): bool
*
* @return bool
*/
protected function isDir(string $filename): bool
protected function isDir($filename)
{
return \is_dir($filename);
}
Expand All @@ -720,7 +744,7 @@ protected function isDir(string $filename): bool
*
* @return bool
*/
protected function mkdir(string $pathname): bool
protected function mkdir($pathname)
{
return \mkdir($pathname, 0777, true);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Knp/Snappy/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Image extends AbstractGenerator
/**
* {@inheritdoc}
*/
public function __construct(string $binary = null, array $options = [], array $env = null)
public function __construct($binary = null, array $options = [], array $env = null)
{
$this->setDefaultExtension('jpg');

Expand All @@ -23,7 +23,7 @@ public function __construct(string $binary = null, array $options = [], array $e
/**
* {@inheritdoc}
*/
protected function configure(): void
protected function configure()
{
$this->addOptions([
'allow' => null, // Allow the file or files from the specified folder to be loaded (repeatable)
Expand Down
12 changes: 7 additions & 5 deletions src/Knp/Snappy/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Pdf extends AbstractGenerator
/**
* {@inheritdoc}
*/
public function __construct(string $binary = null, array $options = [], array $env = null)
public function __construct($binary = null, array $options = [], array $env = null)
{
$this->setDefaultExtension('pdf');
$this->setOptionsWithContentCheck();
Expand All @@ -43,7 +43,7 @@ public function generate($input, $output, array $options = [], $overwrite = fals
*
* @return array $options Transformed options
*/
protected function handleOptions(array $options = []): array
protected function handleOptions(array $options = [])
{
foreach ($options as $option => $value) {
if (null === $value) {
Expand Down Expand Up @@ -73,15 +73,15 @@ protected function handleOptions(array $options = []): array
*
* @return bool
*/
protected function isOptionUrl($option): bool
protected function isOptionUrl($option)
{
return (bool) \filter_var($option, \FILTER_VALIDATE_URL);
}

/**
* {@inheritdoc}
*/
protected function configure(): void
protected function configure()
{
$this->addOptions([
// Global options
Expand Down Expand Up @@ -228,8 +228,10 @@ protected function configure(): void

/**
* Array with options which require to store the content of the option before passing it to wkhtmltopdf.
*
* @return $this
*/
protected function setOptionsWithContentCheck(): self
protected function setOptionsWithContentCheck()
{
$this->optionsWithContentCheck = [
'header-html' => 'html',
Expand Down
Loading

0 comments on commit 349c2e8

Please sign in to comment.