Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Tomsgu/PDFMerger
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomsgu committed Apr 6, 2022
2 parents 79e1b0a + 872caf4 commit 95fac07
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 9 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: "Static analysis"

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"
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"setasign/fpdi": "^2.0"
},
"require-dev": {
"phpstan/phpstan": "^1.5",
"phpunit/phpunit": "^8"
},
"config": {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: max
paths:
- src
5 changes: 4 additions & 1 deletion src/Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
*/
class FileNotFoundException extends \Exception implements PdfMergerExceptionInterface
{
/**
* @return self
*/
public static function create(string $message)
{
return new self($message);
}
}
}
5 changes: 4 additions & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
*/
class InvalidArgumentException extends \Exception implements PdfMergerExceptionInterface
{
/**
* @return self
*/
public static function create(string $message)
{
return new self($message);
}
}
}
4 changes: 3 additions & 1 deletion src/PagesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PagesParser
{
/**
* @throws InvalidArgumentException
* @return array|int[]
*/
public static function parsePages(string $pages): array
{
Expand All @@ -25,6 +26,7 @@ public static function parsePages(string $pages): array

/**
* @throws InvalidArgumentException
* @return array|int[]
*/
private static function getPages(string $pages): array
{
Expand Down Expand Up @@ -71,4 +73,4 @@ private static function checkPages(string $pages): bool

return false;
}
}
}
1 change: 0 additions & 1 deletion src/PdfCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function __construct()
}

/**
* @var string|resource $filePath
* @throws FileNotFoundException
* @throws InvalidArgumentException
*/
Expand Down
8 changes: 5 additions & 3 deletions src/PdfCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -31,4 +33,4 @@ public function getPdfs() : array;
* @return bool
*/
public function hasPdfs() : bool;
}
}
18 changes: 17 additions & 1 deletion src/PdfFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -50,6 +63,9 @@ public function __construct(
$this->orientation = $orientation;
}

/**
* @return array|int[]
*/
public function getPages(): array
{
return $this->pages;
Expand Down
11 changes: 10 additions & 1 deletion src/PdfMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class PdfMerger
self::MODE_STRING,
];

/**
* @var Fpdi
*/
private $fpdi;

public function __construct(Fpdi $fpdi)
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}

0 comments on commit 95fac07

Please sign in to comment.