diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 844df1d..a02bd39 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -128,7 +128,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.2', '7.3', '7.4', '8.0'] + php: ['7.1','7.2', '7.3', '7.4', '8.0'] fail-fast: false diff --git a/composer.json b/composer.json index 41839da..86cfbd4 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ } ], "require": { - "php": ">=7.2", + "php": ">=7.1", "mpdf/mpdf": "^8.0" }, "require-dev": { @@ -31,7 +31,7 @@ "nette/application": "~3.0.0", "nette/di": "^3.0", "ninjify/nunjuck": "^0.3.0", - "ninjify/qa": "^0.12.1", + "ninjify/qa": "^0.12", "phpstan/phpstan": "^0.12", "phpstan/phpstan-deprecation-rules": "^0.12", "phpstan/phpstan-nette": "^0.12", diff --git a/phpstan.neon b/phpstan.neon index bca4d7c..c118e25 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,4 +9,3 @@ parameters: ignoreErrors: - '#Variable property access on Mpdf\\Mpdf#' treatPhpDocTypesAsCertain: false - checkMissingIterableValueType: false diff --git a/src/PdfResponse.php b/src/PdfResponse.php index f9a4112..0c5e4f8 100644 --- a/src/PdfResponse.php +++ b/src/PdfResponse.php @@ -89,10 +89,10 @@ class PdfResponse implements Nette\Application\IResponse public const LAYOUT_DEFAULT = 'default'; // User’s default setting in Adobe Reader - /** @var array */ + /** @var array */ public $mpdfConfig = []; - /** @var array onBeforeComplete event */ + /** @var array onBeforeComplete event */ public $onBeforeComplete = []; /** @var string Additional stylesheet as a html string */ @@ -128,7 +128,7 @@ class PdfResponse implements Nette\Application\IResponse /** @var string ORIENTATION_PORTRAIT or ORIENTATION_LANDSCAPE */ private $pageOrientation = self::ORIENTATION_PORTRAIT; - /** @var string|array see second parameter ($format) at https://mpdf.github.io/reference/mpdf-functions/mpdf.html */ + /** @var string|array see second parameter ($format) at https://mpdf.github.io/reference/mpdf-functions/mpdf.html */ private $pageFormat = 'A4'; /** @var string margins: top, right, bottom, left, header, footer */ @@ -163,33 +163,21 @@ public function setTemplate($source): self return $this; } - /** - * @return string - */ public function getDocumentAuthor(): string { return $this->documentAuthor; } - /** - * @param string $documentAuthor - */ public function setDocumentAuthor(string $documentAuthor): void { $this->documentAuthor = $documentAuthor; } - /** - * @return string - */ public function getDocumentTitle(): string { return $this->documentTitle; } - /** - * @param string $documentTitle - */ public function setDocumentTitle(string $documentTitle): void { $this->documentTitle = $documentTitle; @@ -220,9 +208,6 @@ public function setDisplayZoom($displayZoom): void $this->displayZoom = $displayZoom; } - /** - * @return string - */ public function getDisplayLayout(): string { return $this->displayLayout; @@ -252,41 +237,26 @@ public function setDisplayLayout(string $displayLayout): void $this->displayLayout = $displayLayout; } - /** - * @return bool - */ public function isMultiLanguage(): bool { return $this->multiLanguage; } - /** - * @param bool $multiLanguage - */ public function setMultiLanguage(bool $multiLanguage): void { $this->multiLanguage = $multiLanguage; } - /** - * @return bool - */ public function isIgnoreStylesInHTMLDocument(): bool { return $this->ignoreStylesInHTMLDocument; } - /** - * @param bool $ignoreStylesInHTMLDocument - */ public function setIgnoreStylesInHTMLDocument(bool $ignoreStylesInHTMLDocument): void { $this->ignoreStylesInHTMLDocument = $ignoreStylesInHTMLDocument; } - /** - * @return string - */ public function getSaveMode(): string { return $this->saveMode; @@ -296,7 +266,6 @@ public function getSaveMode(): string * To force download, use PdfResponse::DOWNLOAD * To show pdf in browser, use PdfResponse::INLINE * - * @param string $saveMode * @throws InvalidArgumentException */ public function setSaveMode(string $saveMode): void @@ -308,16 +277,12 @@ public function setSaveMode(string $saveMode): void $this->saveMode = $saveMode; } - /** - * @return string - */ public function getPageOrientation(): string { return $this->pageOrientation; } /** - * @param string $pageOrientation * @throws InvalidStateException * @throws InvalidArgumentException */ @@ -335,7 +300,7 @@ public function setPageOrientation(string $pageOrientation): void } /** - * @return string|array + * @return string|array */ public function getPageFormat() { @@ -343,7 +308,7 @@ public function getPageFormat() } /** - * @param string|array $pageFormat + * @param string|array $pageFormat * @throws InvalidStateException */ public function setPageFormat($pageFormat): void @@ -355,9 +320,6 @@ public function setPageFormat($pageFormat): void $this->pageFormat = $pageFormat; } - /** - * @return string - */ public function getPageMargins(): string { return $this->pageMargins; @@ -366,7 +328,7 @@ public function getPageMargins(): string /** * Gets margins as array * - * @return array + * @return array */ public function getMargins(): array { @@ -383,7 +345,6 @@ public function getMargins(): array } /** - * @param string $pageMargins * @throws InvalidStateException * @throws InvalidArgumentException */ @@ -412,7 +373,6 @@ public function setPageMargins(string $pageMargins): void * WARNING: internally creates mPDF instance, setting some properties after calling this method * may cause an Exception * - * @param string $pathToBackgroundTemplate * @throws FileNotFoundException * @throws PdfParserExceptionAlias */ @@ -431,9 +391,11 @@ public function setBackgroundTemplate(string $pathToBackgroundTemplate): void $tplId = $mpdf->importPage($i); $mpdf->useTemplate($tplId); - if ($i < $pagecount) { - $mpdf->AddPage(); + if ($i >= $pagecount) { + continue; } + + $mpdf->AddPage(); } $mpdf->page = 1; @@ -462,7 +424,6 @@ protected function getMPDFConfig(): array } /** - * @return Mpdf * @throws InvalidStateException */ public function getMPDF(): Mpdf @@ -494,9 +455,11 @@ public function getMPDF(): Mpdf */ public function __construct($source = null) { - if ($source !== null) { - $this->setTemplate($source); + if ($source === null) { + return; } + + $this->setTemplate($source); } @@ -507,7 +470,6 @@ public function __construct($source = null) /** * Builds final pdf * - * @return Mpdf * @throws InvalidStateException * @throws MissingServiceException * @throws MpdfException @@ -620,9 +582,6 @@ private function build(): Mpdf /** * Sends response to output * - * @param IRequest $httpRequest - * @param IResponse $httpResponse - * @return void * @throws MpdfException */ public function send(IRequest $httpRequest, IResponse $httpResponse): void @@ -636,8 +595,6 @@ public function send(IRequest $httpRequest, IResponse $httpResponse): void * Note: $name overrides property $documentTitle * * @param string $dir path to directory - * @param string|null $filename - * @return string * @throws MpdfException */ public function save(string $dir, ?string $filename = null): string @@ -658,7 +615,6 @@ public function save(string $dir, ?string $filename = null): string } /** - * @return string * @throws MpdfException */ public function toString(): string @@ -669,8 +625,6 @@ public function toString(): string /** * Return generated PDF as a string - * - * @return string */ public function __toString(): string { diff --git a/src/PdfResponseFactory.php b/src/PdfResponseFactory.php index abd3408..f4e1bdb 100644 --- a/src/PdfResponseFactory.php +++ b/src/PdfResponseFactory.php @@ -5,9 +5,10 @@ class PdfResponseFactory { - /** @var array */ + /** @var array */ public $mpdfConfig; + /** @param array $mpdfConfig */ public function __construct(array $mpdfConfig) { $this->mpdfConfig = $mpdfConfig; diff --git a/tests/PdfResponse/PdfResponse.full.phpt b/tests/PdfResponse/PdfResponse.full.phpt index 78b24fa..84fd16b 100644 --- a/tests/PdfResponse/PdfResponse.full.phpt +++ b/tests/PdfResponse/PdfResponse.full.phpt @@ -13,7 +13,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; test( - function () { + function (): void { $origData = file_get_contents(__DIR__ . '/templates/example1.htm'); $fileResponse = new PdfResponse($origData); $fileResponse->setSaveMode(PdfResponse::INLINE); diff --git a/tests/PdfResponse/PdfResponse.page.format.phpt b/tests/PdfResponse/PdfResponse.page.format.phpt index 70ef900..2c9d714 100644 --- a/tests/PdfResponse/PdfResponse.page.format.phpt +++ b/tests/PdfResponse/PdfResponse.page.format.phpt @@ -17,7 +17,7 @@ require __DIR__ . '/../bootstrap.php'; $origData = file_get_contents(__DIR__ . '/templates/example1.htm'); test( - function () use ($origData) { + function () use ($origData): void { $fileResponse = new PdfResponse($origData); $fileResponse->setSaveMode(PdfResponse::INLINE); $fileResponse->pageOrientation = PdfResponse::ORIENTATION_LANDSCAPE; @@ -33,12 +33,12 @@ test( ); test( - function () use ($origData) { + function () use ($origData): void { $fileResponse = new PdfResponse($origData); $fileResponse->getMPDF(); Assert::exception( - function () use ($fileResponse) { + function () use ($fileResponse): void { $fileResponse->pageOrientation = PdfResponse::ORIENTATION_LANDSCAPE; }, InvalidStateException::class, @@ -48,12 +48,12 @@ test( ); test( - function () use ($origData) { + function () use ($origData): void { $fileResponse = new PdfResponse($origData); $fileResponse->getMPDF(); Assert::exception( - function () use ($fileResponse) { + function () use ($fileResponse): void { $fileResponse->pageFormat = 'A4-L'; }, InvalidStateException::class, @@ -63,12 +63,12 @@ test( ); test( - function () use ($origData) { + function () use ($origData): void { $fileResponse = new PdfResponse($origData); $fileResponse->getMPDF(); Assert::exception( - function () use ($fileResponse) { + function () use ($fileResponse): void { $fileResponse->pageMargins = $fileResponse->getPageMargins(); }, InvalidStateException::class, diff --git a/tests/PdfResponse/PdfResponse.save.phpt b/tests/PdfResponse/PdfResponse.save.phpt index 16ab16c..d3ab6db 100644 --- a/tests/PdfResponse/PdfResponse.save.phpt +++ b/tests/PdfResponse/PdfResponse.save.phpt @@ -10,7 +10,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; test( - function () { + function (): void { $origData = file_get_contents(__DIR__ . '/templates/example1.htm'); $fileResponse = new PdfResponse($origData); $fileResponse->setSaveMode(PdfResponse::DOWNLOAD); diff --git a/tests/PdfResponse/PdfResponse.setters.phpt b/tests/PdfResponse/PdfResponse.setters.phpt index 81f9474..2782be1 100644 --- a/tests/PdfResponse/PdfResponse.setters.phpt +++ b/tests/PdfResponse/PdfResponse.setters.phpt @@ -11,7 +11,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; test( - function () { + function (): void { $origData = file_get_contents(__DIR__ . '/templates/example1.htm'); $fileResponse = new PdfResponse($origData); @@ -19,7 +19,7 @@ test( $fileResponse->displayZoom = PdfResponse::ZOOM_REAL; $fileResponse->displayZoom = 90; Assert::exception( - function () use ($fileResponse) { + function () use ($fileResponse): void { $fileResponse->displayZoom = 'invalid'; }, InvalidArgumentException::class @@ -28,7 +28,7 @@ test( // layout $fileResponse->displayLayout = PdfResponse::LAYOUT_TWO; Assert::exception( - function () use ($fileResponse) { + function () use ($fileResponse): void { $fileResponse->displayLayout = 'invalid'; }, InvalidArgumentException::class diff --git a/tests/PdfResponse/PdfResponse.symfony.crawler.phpt b/tests/PdfResponse/PdfResponse.symfony.crawler.phpt index 4fcff39..0fa0105 100644 --- a/tests/PdfResponse/PdfResponse.symfony.crawler.phpt +++ b/tests/PdfResponse/PdfResponse.symfony.crawler.phpt @@ -13,7 +13,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; test( - function () { + function (): void { $origData = file_get_contents(__DIR__ . '/templates/example2.htm'); $fileResponse = new PdfResponse($origData); $fileResponse->setSaveMode(PdfResponse::INLINE); diff --git a/tests/PdfResponse/PdfResponseFactory.phpt b/tests/PdfResponse/PdfResponseFactory.phpt index ccf9918..5d84451 100644 --- a/tests/PdfResponse/PdfResponseFactory.phpt +++ b/tests/PdfResponse/PdfResponseFactory.phpt @@ -7,7 +7,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; test( - function () { + function (): void { $author = 'Jan Novák'; $mpdfTmpDir = 'tmp/mpdf'; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 5c8681c..01956c4 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -17,30 +17,6 @@ @mkdir(dirname(TEMP_DIR)); // @ - directory may already exist Tester\Helpers::purge(TEMP_DIR); -function test(Closure $function) -{ - $function(); -} - -class Notes -{ - - public static $notes = []; - - public static function add($message) - { - self::$notes[] = $message; - } - - public static function fetch() - { - $res = self::$notes; - self::$notes = []; - return $res; - } - -} - function getTempDir(): string { $dir = __DIR__ . '/tmp/' . getenv(Tester\Environment::THREAD); @@ -61,7 +37,10 @@ function getTempDir(): string return $dir; } -function createContainer($source, $config = null, $params = []): ?Nette\DI\Container +/** + * @param array $params + */ +function createContainer(Nette\DI\Compiler $source, ?string $config = null, array $params = []): ?Nette\DI\Container { $class = 'Container' . md5((string) lcg_value()); if ($source instanceof Nette\DI\ContainerBuilder) {