Skip to content

Commit

Permalink
Add PHPStan and fix level 1 errors (#307)
Browse files Browse the repository at this point in the history
* Add PHPStan level 1

Co-authored-by: Nicola Asuni <[email protected]>
  • Loading branch information
mvorisek and nicolaasuni authored Dec 12, 2022
1 parent 154bba4 commit 37aa6ee
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.gitattributes export-ignore
/tests export-ignore
/scripts export-ignore
/phpstan.neon.dist export-ignore
20 changes: 20 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,23 @@ jobs:
with:
flags: php-${{ matrix.php-version }}-${{ matrix.os }}
name: php-${{ matrix.php-version }}-${{ matrix.os }}

analyse-php:
name: Static Analysis
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use php 8.0
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
tools: composer:v2
- name: Cache module
uses: actions/cache@v2
with:
path: ~/.composer/cache/
key: composer-cache
- name: Install phpstan
run: composer require --dev phpstan/phpstan
- name: Analyse files
run: ./vendor/bin/phpstan --memory-limit=2G
2 changes: 1 addition & 1 deletion include/barcodes/datamatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ protected function getHighLevelEncoding($data) {
if (isset($this->chset['SH1'][$chr])) {
$temp_cw[] = 0; // shift 1
$shiftset = $this->chset['SH1'];
} elseif (isset($chr, $this->chset['SH2'][$chr])) {
} elseif (isset($this->chset['SH2'][$chr])) {
$temp_cw[] = 1; // shift 2
$shiftset = $this->chset['SH2'];
} elseif (($enc == ENC_C40) AND isset($this->chset['S3C'][$chr])) {
Expand Down
6 changes: 3 additions & 3 deletions include/barcodes/qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ protected function generateMaskNo($maskNo, $width, $frame) {
protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly=false) {
$b = 0;
$bitMask = array();
$bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
$bitMask = $this->generateMaskNo($maskNo, $width, $s);
if ($maskGenOnly) {
return;
}
Expand Down Expand Up @@ -1460,7 +1460,7 @@ protected function toUpper() {
$stringLen = strlen($this->dataStr);
$p = 0;
while ($p < $stringLen) {
$mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint);
$mode = $this->identifyMode(substr($this->dataStr, $p));
if ($mode == QR_MODE_KJ) {
$p += 2;
} else {
Expand Down Expand Up @@ -1692,7 +1692,7 @@ protected function insertStructuredAppendHeader($items, $size, $index, $parity)
return -1;
}
$buf = array($size, $index, $parity);
$entry = $this->newInputItem(QR_MODE_ST, 3, buf);
$entry = $this->newInputItem(QR_MODE_ST, 3, $buf);
array_unshift($items, $entry);
return $items;
}
Expand Down
37 changes: 1 addition & 36 deletions include/tcpdf_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,39 +128,6 @@ public static function getTCPDFProducer() {
return "\x54\x43\x50\x44\x46\x20".self::getTCPDFVersion()."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
}

/**
* Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
* @param boolean $mqr FALSE for off, TRUE for on.
* @since 4.6.025 (2009-08-17)
* @public static
*/
public static function set_mqr($mqr) {
if (!defined('PHP_VERSION_ID')) {
$version = PHP_VERSION;
define('PHP_VERSION_ID', (($version[0] * 10000) + ($version[2] * 100) + $version[4]));
}
if (PHP_VERSION_ID < 50300) {
@set_magic_quotes_runtime($mqr);
}
}

/**
* Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
* @return int Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
* @since 4.6.025 (2009-08-17)
* @public static
*/
public static function get_mqr() {
if (!defined('PHP_VERSION_ID')) {
$version = PHP_VERSION;
define('PHP_VERSION_ID', (($version[0] * 10000) + ($version[2] * 100) + $version[4]));
}
if (PHP_VERSION_ID < 50300) {
return @get_magic_quotes_runtime();
}
return 0;
}

/**
* Check if the URL exist.
* @param string $url URL to check.
Expand Down Expand Up @@ -852,9 +819,7 @@ public static function getAnnotOptFromJSProp($prop, &$spot_colors, $rtl=false) {
if (isset($prop['charLimit'])) {
$opt['maxlen'] = intval($prop['charLimit']);
}
if (!isset($ff)) {
$ff = 0; // default value
}
$ff = 0;
// readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
$ff += 1 << 0;
Expand Down
32 changes: 32 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
parameters:
level: 1
paths:
- ./
excludePaths:
- vendor/
# remove once https://github.com/phpstan/phpstan/issues/7955 is fixed
- fonts/

scanFiles:
- tcpdf_autoconfig.php

ignoreErrors:
- '~^Constant (PDF_HEADER_LOGO|K_PATH_CACHE|K_PATH_FONTS|K_PATH_IMAGES|K_PATH_URL) not found\.$~'
- '~^Constructor of class TCPDF has an unused parameter \$diskcache\.$~'
- '~^Variable \$\w+ might not be defined\.$~'
- '~^Method TCPDF(_FILTERS)?::\w+\(\) should return .+ but return statement is missing\.$~'

# mcrypt does not support PHP 7.2 or later
-
message: '~^(Constant MCRYPT_RIJNDAEL_128 not found\.|Function mcrypt_get_cipher_name not found\.)$~'
path: tcpdf.php
count: 2
-
message: '~^(Constant (MCRYPT_ARCFOUR|MCRYPT_MODE_CBC|MCRYPT_MODE_STREAM|MCRYPT_RAND|MCRYPT_RIJNDAEL_128) not found\.|Function (mcrypt_create_iv|mcrypt_encrypt|mcrypt_get_iv_size) not found\.)$~'
path: include/tcpdf_static.php
count: 16

-
message: '~^(Call to static method create\(\) on an unknown class Symfony\\Component\\Finder\\Finder\.|Instantiated class Doctum\\(Doctum|RemoteRepository\\GitHubRemoteRepository) not found\.)$~'
path: scripts/doctum.php
count: 3
55 changes: 31 additions & 24 deletions tcpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4347,6 +4347,22 @@ public function AddFont($family, $style='', $fontfile='', $subset='default') {
}
// include font file
if (!TCPDF_STATIC::empty_string($fontfile) AND (@TCPDF_STATIC::file_exists($fontfile))) {
$type=null;
$name=null;
$desc=null;
$up=-null;
$ut=null;
$cw=null;
$cbbox=null;
$dw=null;
$enc=null;
$cidinfo=null;
$file=null;
$ctg=null;
$diff=null;
$originalsize=null;
$size1=null;
$size2=null;
include($fontfile);
} else {
$this->Error('Could not include font definition file: '.$family.'');
Expand Down Expand Up @@ -5524,12 +5540,12 @@ protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $
$xdk = $xdx * $k;
// print text
$s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
if (isset($uniblock)) {
if (isset($uniblock)) { // @phpstan-ignore-line
// print overlapping characters as separate string
$xshift = 0; // horizontal shift
$ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
$spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
foreach ($uniblock as $uk => $uniarr) {
foreach ($uniblock as $uk => $uniarr) { // @phpstan-ignore-line
if (($uk % 2) == 0) {
// x space to skip
if ($spacewidth != 0) {
Expand Down Expand Up @@ -7101,8 +7117,6 @@ public function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='', $
} elseif ($type == 'jpg') {
$type = 'jpeg';
}
$mqr = TCPDF_STATIC::get_mqr();
TCPDF_STATIC::set_mqr(false);
// Specific image handlers (defined on TCPDF_IMAGES CLASS)
$mtd = '_parse'.$type;
// GD image handler function
Expand Down Expand Up @@ -7204,7 +7218,6 @@ public function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='', $
// unable to process image
return false;
}
TCPDF_STATIC::set_mqr($mqr);
if ($ismask) {
// force grayscale
$info['cs'] = 'DeviceGray';
Expand Down Expand Up @@ -8868,8 +8881,6 @@ protected function _putfonts() {
$this->_newobj();
$this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
}
$mqr = TCPDF_STATIC::get_mqr();
TCPDF_STATIC::set_mqr(false);
foreach ($this->FontFiles as $file => $info) {
// search and get font file to embedd
$fontfile = TCPDF_FONTS::getFontFullPath($file, $info['fontdir']);
Expand Down Expand Up @@ -8923,7 +8934,6 @@ protected function _putfonts() {
$this->_out($out);
}
}
TCPDF_STATIC::set_mqr($mqr);
foreach ($this->fontkeys as $k) {
//Font objects
$font = $this->getFontBuffer($k);
Expand Down Expand Up @@ -9192,6 +9202,7 @@ protected function _putimages() {
foreach ($this->imagekeys as $file) {
$info = $this->getImageBuffer($file);
// set object for alternate images array
$altoid = null;
if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
$altoid = $this->_newobj();
$out = '[';
Expand Down Expand Up @@ -9239,7 +9250,7 @@ protected function _putimages() {
$out .= ' /Decode [1 0 1 0 1 0 1 0]';
}
$out .= ' /BitsPerComponent '.$info['bpc'];
if (isset($altoid) AND ($altoid > 0)) {
if ($altoid > 0) {
// reference to alternate images dictionary
$out .= ' /Alternates '.$altoid.' 0 R';
}
Expand Down Expand Up @@ -11699,7 +11710,7 @@ public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_s
if ($this->state != 2) {
return;
}
if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
$this->setFillColorArray($fill_color);
}
$op = TCPDF_STATIC::getPathPaintOperator($style);
Expand Down Expand Up @@ -11729,7 +11740,7 @@ public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $
if ($this->state != 2) {
return;
}
if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
$this->setFillColorArray($fill_color);
}
$op = TCPDF_STATIC::getPathPaintOperator($style);
Expand Down Expand Up @@ -11772,7 +11783,7 @@ public function Ellipse($x0, $y0, $rx, $ry=0, $angle=0, $astart=0, $afinish=360,
if (TCPDF_STATIC::empty_string($ry) OR ($ry == 0)) {
$ry = $rx;
}
if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
$this->setFillColorArray($fill_color);
}
$op = TCPDF_STATIC::getPathPaintOperator($style);
Expand Down Expand Up @@ -11991,7 +12002,7 @@ public function Polygon($p, $style='', $line_style=array(), $fill_color=array(),
}
$nc += 4;
}
if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
$this->setFillColorArray($fill_color);
}
$op = TCPDF_STATIC::getPathPaintOperator($style);
Expand Down Expand Up @@ -12194,7 +12205,7 @@ public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $s
return;
}
// Rounded
if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
$this->setFillColorArray($fill_color);
}
$op = TCPDF_STATIC::getPathPaintOperator($style);
Expand Down Expand Up @@ -15073,9 +15084,7 @@ public function ImageEps($file, $x=null, $y=null, $w=0, $h=0, $link='', $useBoun
// translate
$this->_out(sprintf('%F %F %F %F %F %F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
// scale
if (isset($scale_x)) {
$this->_out(sprintf('%F %F %F %F %F %F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
}
$this->_out(sprintf('%F %F %F %F %F %F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
// handle pc/unix/mac line endings
$lines = preg_split('/[\r\n]+/si', $data, -1, PREG_SPLIT_NO_EMPTY);
$u=0;
Expand Down Expand Up @@ -17741,7 +17750,7 @@ public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=fal
$pend = '';
}
}
if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
if ((((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
// calculate shifting amount
$tw = $w;
if (($plalign == 'J') AND $this->isRTLTextDir() AND ($this->num_columns > 1)) {
Expand Down Expand Up @@ -18347,7 +18356,7 @@ public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=fal
$dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
}
// store border info
if (isset($tdborder) AND !empty($tdborder)) {
if (!empty($tdborder)) {
$dom[$trid]['cellpos'][($cellid - 1)]['border'] = $tdborder;
}
$prevLastH = $this->lasth;
Expand Down Expand Up @@ -18583,7 +18592,7 @@ public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=fal
$strrest = $this->Write($this->lasth, $dom[$key]['value'], '', $wfill, '', false, 0, true, $firstblock, 0, $wadj);
// restore default direction
if ($reverse_dir AND ($wadj == 0)) {
$this->x = $xws;
$this->x = $xws; // @phpstan-ignore-line
$this->rtl = !$this->rtl;
$reverse_dir = false;
}
Expand Down Expand Up @@ -18692,7 +18701,7 @@ public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=fal
$pend = '';
}
}
if ((isset($plalign) AND ((($plalign == 'C') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
if ((((($plalign == 'C') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
// calculate shifting amount
$tw = $w;
if ($this->lMargin != $prevlMargin) {
Expand Down Expand Up @@ -23895,9 +23904,7 @@ protected function SVGPath($d, $style='') {
}
$firstcmd = false;
} // end foreach
if (!empty($op)) {
$this->_out($op);
}
$this->_out($op);
return array($xmin, $ymin, ($xmax - $xmin), ($ymax - $ymin));
}

Expand Down
2 changes: 1 addition & 1 deletion tcpdf_import.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TCPDF_IMPORT extends TCPDF {
/**
* Import an existing PDF document
* @param string $filename Filename of the PDF document to import.
* @return true in case of success, false otherwise
* @return void
* @public
* @since 1.0.000 (2011-05-24)
*/
Expand Down

0 comments on commit 37aa6ee

Please sign in to comment.