From f5e95666311aad454f4aacd867ce95fe62bf41a7 Mon Sep 17 00:00:00 2001 From: nicolaasuni Date: Tue, 10 Oct 2023 13:27:10 +0100 Subject: [PATCH] Add phpstan check and fix level 0 issues. --- .github/workflows/check.yml | 19 ++++ VERSION | 2 +- resources/debian/control | 2 +- resources/rpm/rpm.spec | 4 +- src/Box.php | 14 +++ src/Page.php | 188 ------------------------------------ src/Region.php | 97 +++++++++++++++++++ src/Settings.php | 84 ++++++++++++++++ test/TestUtil.php | 9 +- 9 files changed, 224 insertions(+), 195 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index e784d67..699127d 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -60,3 +60,22 @@ jobs: with: flags: php-${{ matrix.php-version }}-${{ matrix.os }} name: php-${{ matrix.php-version }}-${{ matrix.os }} + + static-analysis: + name: Static Analysis + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Use php 8.2 + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + - name: Cache module + uses: actions/cache@v3 + with: + path: ~/.composer/cache/ + key: composer-cache + - name: Install phpstan + run: composer require --dev phpstan/phpstan + - name: Analyse files + run: ./vendor/bin/phpstan analyse src test diff --git a/VERSION b/VERSION index e4604e3..b347b11 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.2.1 +3.2.3 diff --git a/resources/debian/control b/resources/debian/control index a3a98ec..8021872 100644 --- a/resources/debian/control +++ b/resources/debian/control @@ -10,6 +10,6 @@ Vcs-Git: https://github.com/~#VENDOR#~/~#PROJECT#~.git Package: ~#PKGNAME#~ Provides: php-~#PROJECT#~ Architecture: all -Depends: php (>= 5.4.0), php-date, php-zip, php-tecnickcom-tc-lib-color (<< 2.0.0), php-tecnickcom-tc-lib-color (>= 1.14.29), php-tecnickcom-tc-lib-pdf-encrypt (<< 2.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 1.6.25), ${misc:Depends} +Depends: php (>= 5.4.0), php-date, php-zip, php-tecnickcom-tc-lib-color (<< 2.0.0), php-tecnickcom-tc-lib-color (>= 1.14.31), php-tecnickcom-tc-lib-pdf-encrypt (<< 2.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 1.6.27), ${misc:Depends} Description: PHP PDF Page Library PHP library containing PDF page formats and definitions. diff --git a/resources/rpm/rpm.spec b/resources/rpm/rpm.spec index 4988308..aa560a6 100644 --- a/resources/rpm/rpm.spec +++ b/resources/rpm/rpm.spec @@ -20,9 +20,9 @@ Requires: php(language) >= 5.4.0 Requires: php-date Requires: php-zlib Requires: php-composer(%{c_vendor}/tc-lib-color) < 2.0.0 -Requires: php-composer(%{c_vendor}/tc-lib-color) >= 1.14.29 +Requires: php-composer(%{c_vendor}/tc-lib-color) >= 1.14.31 Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) < 2.0.0 -Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) >= 1.6.25 +Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) >= 1.6.27 Provides: php-composer(%{c_vendor}/%{gh_project}) = %{version} Provides: php-%{gh_project} = %{version} diff --git a/src/Box.php b/src/Box.php index cb8a676..65f6289 100644 --- a/src/Box.php +++ b/src/Box.php @@ -31,6 +31,20 @@ */ abstract class Box extends \Com\Tecnick\Pdf\Page\Mode { + /** + * Unit of measure conversion ratio. + * + * @var float + */ + protected $kunit = 1.0; + + /** + * Color object. + * + * @var Color + */ + protected $col; + /** * Array of page box names. * diff --git a/src/Page.php b/src/Page.php index 9e13628..8c704ab 100644 --- a/src/Page.php +++ b/src/Page.php @@ -35,97 +35,6 @@ */ class Page extends \Com\Tecnick\Pdf\Page\Region { - /** - * Alias for total number of pages in a group. - * - * @var string - */ - const PAGE_TOT = '~#PT'; - - /** - * Alias for page number. - * - * @var string - */ - const PAGE_NUM = '~#PN'; - - /** - * Array of pages (stack). - * - * @var array - */ - protected $page = array(); - - /** - * Current page ID. - * - * @var int - */ - protected $pid = -1; - - /** - * Maximum page ID. - * - * @var int - */ - protected $pmaxid = -1; - - /** - * Count pages in each group. - * - * @var array - */ - protected $group = array(0 => 0); - - /** - * Unit of measure conversion ratio. - * - * @var float - */ - protected $kunit = 1.0; - - /** - * Color object. - * - * @var Color - */ - protected $col; - - /** - * Encrypt object. - * - * @var Encrypt - */ - protected $enc; - - /** - * True if we are in PDF/A mode. - * - * @var bool - */ - protected $pdfa = false; - - /** - * Enable stream compression. - * - * @var int - */ - protected $compress = true; - - /** - * True if the signature approval is enabled (for incremental updates). - * - * @var bool - */ - protected $sigapp = false; - - /** - * Reserved Object ID for the resource dictionary. - * - * @var int - */ - protected $rdoid = 1; - /** * Initialize page data. * @@ -173,78 +82,6 @@ public function enableSignatureApproval($sigapp) return $this; } - /** - * Add a new page. - * - * @param array $data Page data: - * time : UTC page modification time in seconds; - * group : page group number; - * num : if set overwrites the page number; - * content : string containing the raw page content; - * annotrefs : array containing the annotation object references; - * format : page format name, or alternatively you can set width and height as below; - * width : page width; - * height : page height; - * orientation : page orientation ('P' or 'L'); - * rotation : the number of degrees by which the page shall be rotated clockwise when displayed or printed; - * box : array containing page box boundaries and settings (@see setBox); - * transition : array containing page transition data (@see getPageTransition); - * zoom : preferred zoom (magnification) factor; - * margin : page margins: - * PL : page left margin measured from the left page edge - * PR : page right margin measured from the right page edge - * PT : page top or header top measured distance from the top page edge - * HB : header bottom measured from the top page edge - * CT : content top measured from the top page edge - * CB : content bottom (page breaking point) measured from the top page edge - * FT : footer top measured from the bottom page edge - * PB : page bottom (footer bottom) measured from the bottom page edge - * columns : number of equal vertical columns, if set it will automatically populate the region array - * region : array containing the ordered list of rectangular areas where it is allowed to write, - * each region is defined by: - * RX : horizontal coordinate of top-left corner - * RY : vertical coordinate of top-left corner - * RW : region width - * RH : region height - * autobreak : true to automatically add a page when the content reaches the breaking point. - * - * NOTE: if $data is empty, then the last page format will be cloned. - * - * @return array Page data with additional Page ID property 'pid'. - */ - public function add(array $data = array()) - { - if (empty($data) && ($this->pmaxid >= 0)) { - // clone last page data - $data = $this->page[$this->pmaxid]; - unset($data['time'], $data['content'], $data['annotrefs'], $data['pagenum']); - } else { - $this->sanitizeGroup($data); - $this->sanitizeRotation($data); - $this->sanitizeZoom($data); - $this->sanitizePageFormat($data); - $this->sanitizeBoxData($data); - $this->sanitizeTransitions($data); - $this->sanitizeMargins($data); - $this->sanitizeRegions($data); - } - $this->sanitizeTime($data); - $this->sanitizeContent($data); - $this->sanitizeAnnotRefs($data); - $this->sanitizePageNumber($data); - $data['content_mark'] = array(0); - $data['currentRegion'] = 0; - $data['pid'] = ++$this->pmaxid; - $this->pid = $data['pid']; - $this->page[$this->pid] = $data; - if (isset($this->group[$data['group']])) { - $this->group[$data['group']] += 1; - } else { - $this->group[$data['group']] = 1; - } - return $this->page[$this->pid]; - } - /** * Remove the specified page. * @@ -304,31 +141,6 @@ public function getPages() return $this->page; } - /** - * Set the current page number (move to the specified page). - * - * @param int $pid page index. Omit or set it to -1 for the current page ID. - */ - public function setCurrentPage($pid = -1) - { - $pid = $this->sanitizePageID($pid); - $this->pid = $pid; - return $this->page[$this->pid]; - } - - /** - * Returns the specified page data. - * - * @param int $pid page index. Omit or set it to -1 for the current page ID. - * - * return array - */ - public function getPage($pid = -1) - { - $pid = $this->sanitizePageID($pid); - return $this->page[$pid]; - } - /** * Add Annotation references. * diff --git a/src/Region.php b/src/Region.php index 2923035..22c7537 100644 --- a/src/Region.php +++ b/src/Region.php @@ -34,6 +34,103 @@ */ abstract class Region extends \Com\Tecnick\Pdf\Page\Settings { + /** + * Add a new page. + * + * @param array $data Page data: + * time : UTC page modification time in seconds; + * group : page group number; + * num : if set overwrites the page number; + * content : string containing the raw page content; + * annotrefs : array containing the annotation object references; + * format : page format name, or alternatively you can set width and height as below; + * width : page width; + * height : page height; + * orientation : page orientation ('P' or 'L'); + * rotation : the number of degrees by which the page shall be rotated clockwise when displayed or printed; + * box : array containing page box boundaries and settings (@see setBox); + * transition : array containing page transition data (@see getPageTransition); + * zoom : preferred zoom (magnification) factor; + * margin : page margins: + * PL : page left margin measured from the left page edge + * PR : page right margin measured from the right page edge + * PT : page top or header top measured distance from the top page edge + * HB : header bottom measured from the top page edge + * CT : content top measured from the top page edge + * CB : content bottom (page breaking point) measured from the top page edge + * FT : footer top measured from the bottom page edge + * PB : page bottom (footer bottom) measured from the bottom page edge + * columns : number of equal vertical columns, if set it will automatically populate the region array + * region : array containing the ordered list of rectangular areas where it is allowed to write, + * each region is defined by: + * RX : horizontal coordinate of top-left corner + * RY : vertical coordinate of top-left corner + * RW : region width + * RH : region height + * autobreak : true to automatically add a page when the content reaches the breaking point. + * + * NOTE: if $data is empty, then the last page format will be cloned. + * + * @return array Page data with additional Page ID property 'pid'. + */ + public function add(array $data = array()) + { + if (empty($data) && ($this->pmaxid >= 0)) { + // clone last page data + $data = $this->page[$this->pmaxid]; + unset($data['time'], $data['content'], $data['annotrefs'], $data['pagenum']); + } else { + $this->sanitizeGroup($data); + $this->sanitizeRotation($data); + $this->sanitizeZoom($data); + $this->sanitizePageFormat($data); + $this->sanitizeBoxData($data); + $this->sanitizeTransitions($data); + $this->sanitizeMargins($data); + $this->sanitizeRegions($data); + } + $this->sanitizeTime($data); + $this->sanitizeContent($data); + $this->sanitizeAnnotRefs($data); + $this->sanitizePageNumber($data); + $data['content_mark'] = array(0); + $data['currentRegion'] = 0; + $data['pid'] = ++$this->pmaxid; + $this->pid = $data['pid']; + $this->page[$this->pid] = $data; + if (isset($this->group[$data['group']])) { + $this->group[$data['group']] += 1; + } else { + $this->group[$data['group']] = 1; + } + return $this->page[$this->pid]; + } + + /** + * Set the current page number (move to the specified page). + * + * @param int $pid page index. Omit or set it to -1 for the current page ID. + */ + public function setCurrentPage($pid = -1) + { + $pid = $this->sanitizePageID($pid); + $this->pid = $pid; + return $this->page[$this->pid]; + } + + /** + * Returns the specified page data. + * + * @param int $pid page index. Omit or set it to -1 for the current page ID. + * + * return array + */ + public function getPage($pid = -1) + { + $pid = $this->sanitizePageID($pid); + return $this->page[$pid]; + } + /** * Check if the specified page ID exist. * diff --git a/src/Settings.php b/src/Settings.php index e5c6787..8b1796c 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -39,6 +39,90 @@ abstract class Settings extends \Com\Tecnick\Pdf\Page\Box */ const EPS = 0.0001; + /** + * Alias for total number of pages in a group. + * + * @var string + */ + const PAGE_TOT = '~#PT'; + + /** + * Alias for page number. + * + * @var string + */ + const PAGE_NUM = '~#PN'; + + /** + * Array of pages (stack). + * + * @var array + */ + protected $page = array(); + + /** + * Current page ID. + * + * @var int + */ + protected $pid = -1; + + /** + * Maximum page ID. + * + * @var int + */ + protected $pmaxid = -1; + + /** + * Count pages in each group. + * + * @var array + */ + protected $group = array(0 => 0); + + /** + * Encrypt object. + * + * @var Encrypt + */ + protected $enc; + + /** + * True if we are in PDF/A mode. + * + * @var bool + */ + protected $pdfa = false; + + /** + * Enable stream compression. + * + * @var int + */ + protected $compress = true; + + /** + * True if the signature approval is enabled (for incremental updates). + * + * @var bool + */ + protected $sigapp = false; + + /** + * Reserved Object ID for the resource dictionary. + * + * @var int + */ + protected $rdoid = 1; + + /** + * Root object ID. + * + * @var int + */ + protected $rootoid = 0; + /** * Sanitize or set the page modification time. * diff --git a/test/TestUtil.php b/test/TestUtil.php index a91ec25..f8d1b7a 100644 --- a/test/TestUtil.php +++ b/test/TestUtil.php @@ -34,7 +34,8 @@ class TestUtil extends TestCase public function bcAssertEqualsWithDelta($expected, $actual, $delta = 0.01, $message = '') { if (\is_callable([self::class, 'assertEqualsWithDelta'])) { - return parent::assertEqualsWithDelta($expected, $actual, $delta, $message); + parent::assertEqualsWithDelta($expected, $actual, $delta, $message); + return; } return $this->assertEquals($expected, $actual, $message, $delta); } @@ -42,7 +43,8 @@ public function bcAssertEqualsWithDelta($expected, $actual, $delta = 0.01, $mess public function bcExpectException($exception) { if (\is_callable([self::class, 'expectException'])) { - return parent::expectException($exception); + parent::expectException($exception); + return; } return parent::setExpectedException($exception); } @@ -50,7 +52,8 @@ public function bcExpectException($exception) public function bcAssertStringContainsString($needle, $haystack) { if (\is_callable([self::class, 'assertStringContainsString'])) { - return parent::assertStringContainsString($needle, $haystack); + parent::assertStringContainsString($needle, $haystack); + return; } return parent::assertContains($needle, $haystack); }