From c02b47f323616458cb8dfe790c0b6ff365f10578 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 6 Apr 2022 14:06:34 +0200 Subject: [PATCH 1/4] Ignore phpunit.result.cache file. --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0012a6e..fd834db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea composer.lock phpunit.xml -vendor/ \ No newline at end of file +vendor/ +.phpunit.result.cache From c76dfdd84c45773ba05cb5b4ef278d345016fb18 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 6 Apr 2022 15:01:29 +0200 Subject: [PATCH 2/4] Add phpstan. --- .github/workflows/static-analysis.yml | 38 +++++++++++++++++++++++++++ composer.json | 1 + phpstan.neon | 4 +++ 3 files changed, 43 insertions(+) create mode 100644 .github/workflows/static-analysis.yml create mode 100644 phpstan.neon diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..2c95527 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,38 @@ +name: "Continuous Integration" + +on: + pull_request: + paths-ignore: + - "*.md" + push: + branches-ignore: + - 'dependabot/**' + release: + types: [ created ] + +jobs: + static-analysis-phpstan: + runs-on: "ubuntu-latest" + + name: "Static analysis with PHPStan" + + strategy: + fail-fast: false + matrix: + php: ["8.1"] + + steps: + - name: "Checkout" + uses: "actions/checkout@v2" + + - name: "Setup PHP" + uses: "shivammathur/setup-php@v2" + with: + php-version: "${{ matrix.php }}" + coverage: "none" + + - name: "Install dependencies with Composer" + uses: "ramsey/composer-install@v1" + + - name: "Run a static analysis with phpstan" + run: "vendor/bin/phpstan analyse" diff --git a/composer.json b/composer.json index a90d902..65f0acf 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "setasign/fpdi": "^2.0" }, "require-dev": { + "phpstan/phpstan": "^1.5", "phpunit/phpunit": "^8" }, "config": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..309bd4f --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: max + paths: + - src From a9e1946895a30da020b3eb03d546679b75688453 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 6 Apr 2022 15:03:02 +0200 Subject: [PATCH 3/4] Workflow name change. --- .github/workflows/static-analysis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 2c95527..def82d7 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -1,4 +1,4 @@ -name: "Continuous Integration" +name: "Static analysis" on: pull_request: From 872caf48819ea2b44e009b40fbfedb26baf1fcef Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 6 Apr 2022 15:34:10 +0200 Subject: [PATCH 4/4] Fix phpstan errors. --- src/Exception/FileNotFoundException.php | 5 ++++- src/Exception/InvalidArgumentException.php | 5 ++++- src/PagesParser.php | 4 +++- src/PdfCollection.php | 1 - src/PdfCollectionInterface.php | 8 +++++--- src/PdfFile.php | 18 +++++++++++++++++- src/PdfMerger.php | 11 ++++++++++- 7 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/Exception/FileNotFoundException.php b/src/Exception/FileNotFoundException.php index b4cbd67..3e6ef65 100644 --- a/src/Exception/FileNotFoundException.php +++ b/src/Exception/FileNotFoundException.php @@ -9,8 +9,11 @@ */ class FileNotFoundException extends \Exception implements PdfMergerExceptionInterface { + /** + * @return self + */ public static function create(string $message) { return new self($message); } -} \ No newline at end of file +} diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 9fdacf1..d59e37c 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -9,8 +9,11 @@ */ class InvalidArgumentException extends \Exception implements PdfMergerExceptionInterface { + /** + * @return self + */ public static function create(string $message) { return new self($message); } -} \ No newline at end of file +} diff --git a/src/PagesParser.php b/src/PagesParser.php index 2676054..2d820b2 100644 --- a/src/PagesParser.php +++ b/src/PagesParser.php @@ -13,6 +13,7 @@ class PagesParser { /** * @throws InvalidArgumentException + * @return array|int[] */ public static function parsePages(string $pages): array { @@ -25,6 +26,7 @@ public static function parsePages(string $pages): array /** * @throws InvalidArgumentException + * @return array|int[] */ private static function getPages(string $pages): array { @@ -71,4 +73,4 @@ private static function checkPages(string $pages): bool return false; } -} \ No newline at end of file +} diff --git a/src/PdfCollection.php b/src/PdfCollection.php index eee24a8..717ba82 100644 --- a/src/PdfCollection.php +++ b/src/PdfCollection.php @@ -21,7 +21,6 @@ public function __construct() } /** - * @var string|resource $filePath * @throws FileNotFoundException * @throws InvalidArgumentException */ diff --git a/src/PdfCollectionInterface.php b/src/PdfCollectionInterface.php index ed9f73f..5584ca5 100644 --- a/src/PdfCollectionInterface.php +++ b/src/PdfCollectionInterface.php @@ -12,11 +12,13 @@ interface PdfCollectionInterface /** * Add pdf file to the collection. * - * @param string $filePath Path of the file. + * @param string|resource $filePath Path of the file. * @param string $pages String representation of pages to parse. * @param string $orientation Can be landscape or portrait. + * + * @return self */ - public function addPdf(string $filePath, string $pages = PdfFile::ALL_PAGES, string $orientation = ''); + public function addPdf($filePath, string $pages = PdfFile::ALL_PAGES, string $orientation = ''); /** * Returns all PdfFile objects. @@ -31,4 +33,4 @@ public function getPdfs() : array; * @return bool */ public function hasPdfs() : bool; -} \ No newline at end of file +} diff --git a/src/PdfFile.php b/src/PdfFile.php index ccf89f9..6048c61 100644 --- a/src/PdfFile.php +++ b/src/PdfFile.php @@ -24,14 +24,27 @@ class PdfFile '', ]; + /** + * @var resource|string + */ private $path; + + /** + * @var array|int[] + */ private $pages; + + /** + * @var string + */ private $orientation; /** * @throws FileNotFoundException * @throws InvalidArgumentException - * @var string|resource $file + * + * @param string|resource $file + * @param array|int[] $pages */ public function __construct( $file, @@ -50,6 +63,9 @@ public function __construct( $this->orientation = $orientation; } + /** + * @return array|int[] + */ public function getPages(): array { return $this->pages; diff --git a/src/PdfMerger.php b/src/PdfMerger.php index 21df7d0..e05c8d6 100644 --- a/src/PdfMerger.php +++ b/src/PdfMerger.php @@ -24,6 +24,9 @@ class PdfMerger self::MODE_STRING, ]; + /** + * @var Fpdi + */ private $fpdi; public function __construct(Fpdi $fpdi) @@ -76,7 +79,13 @@ private function addPage(int $pageNumber, PdfFile $pdfFile, string $defaultOrien $filename = $pdfFile->getPath(); throw InvalidArgumentException::create("Could not load a page number '$pageNumber' from '$filename' PDF. Does the page exist?"); } + /** + * @var array{width: int, height: int, orientation: string}|false $size + */ $size = $this->fpdi->getTemplateSize($template); + if ($size === false){ + throw InvalidArgumentException::create("Could not get size of the given page '$pageNumber'."); + } if (strtolower($fileOrientation) === PdfFile::ORIENTATION_AUTO_DETECT){ if ($size['width'] > $size['height']){ $fileOrientation = PdfFile::ORIENTATION_LANDSCAPE; @@ -87,4 +96,4 @@ private function addPage(int $pageNumber, PdfFile $pdfFile, string $defaultOrien $this->fpdi->AddPage($fileOrientation, [$size['width'], $size['height']]); $this->fpdi->useTemplate($template); } -} \ No newline at end of file +}