From 86980478544a7be1441394bee3690c2f33eb570b Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Sun, 7 Apr 2024 14:50:05 +0200 Subject: [PATCH 1/2] feat: update psalm 4 => 5 chore: migrate to PHPUnit11 chore: fix formatting --- composer.json | 4 ++-- lib/Common/BitMatrix.php | 2 +- lib/Common/Detector/MathUtils.php | 4 ++-- .../Detector/MonochromeRectangleDetector.php | 4 ++-- lib/Common/Reedsolomon/GenericGF.php | 2 ++ lib/IMagickLuminanceSource.php | 8 +++++--- lib/Qrcode/Decoder/Decoder.php | 17 ++++++----------- .../Detector/AlignmentPatternFinder.php | 6 +++--- lib/Qrcode/Detector/Detector.php | 7 ++----- lib/Qrcode/Detector/FinderPattern.php | 2 +- lib/Qrcode/Detector/FinderPatternFinder.php | 4 ++-- lib/Qrcode/QRCodeReader.php | 2 +- lib/ResultPoint.php | 3 +-- phpunit.xml.dist | 19 ++++++++++++------- psalm.xml | 1 - tests/QrReaderTest.php | 6 +++--- 16 files changed, 45 insertions(+), 46 deletions(-) diff --git a/composer.json b/composer.json index 26a03c7..dbdc355 100644 --- a/composer.json +++ b/composer.json @@ -24,10 +24,10 @@ "php": ">=8.1" }, "require-dev": { - "phpunit/phpunit": "^7.5 | ^8.0 | ^9.0", + "phpunit/phpunit": "^10.5.17", "rector/rector": "^0.13.6", "symplify/easy-coding-standard": "^11.0", - "vimeo/psalm": "^4.24" + "vimeo/psalm": "^5.23.1" }, "autoload": { "psr-4": { diff --git a/lib/Common/BitMatrix.php b/lib/Common/BitMatrix.php index 442cd0c..e82e879 100644 --- a/lib/Common/BitMatrix.php +++ b/lib/Common/BitMatrix.php @@ -113,7 +113,7 @@ public function set(int|float $x, int|float $y): void //$this->bits[$offset] = intval32bits($this->bits[$offset]); //} -//16777216 + //16777216 } public function _unset($x, $y): void diff --git a/lib/Common/Detector/MathUtils.php b/lib/Common/Detector/MathUtils.php index 54bdbbe..379e162 100644 --- a/lib/Common/Detector/MathUtils.php +++ b/lib/Common/Detector/MathUtils.php @@ -30,8 +30,8 @@ private function __construct() * values. -2.5 rounds to -3, not -2. For purposes here it makes no difference. * * @param float $d real value to round - * - * @return int {@code int} + * + * @return int */ public static function round(float $d) { diff --git a/lib/Common/Detector/MonochromeRectangleDetector.php b/lib/Common/Detector/MonochromeRectangleDetector.php index d7f5be2..14094b6 100644 --- a/lib/Common/Detector/MonochromeRectangleDetector.php +++ b/lib/Common/Detector/MonochromeRectangleDetector.php @@ -165,8 +165,8 @@ private function findCornerFromCenter( ): \Zxing\ResultPoint { $lastRange = null; for ($y = $centerY, $x = $centerX; - $y < $bottom && $y >= $top && $x < $right && $x >= $left; - $y += $deltaY, $x += $deltaX) { + $y < $bottom && $y >= $top && $x < $right && $x >= $left; + $y += $deltaY, $x += $deltaX) { $range = 0; if ($deltaX == 0) { // horizontal slices, up and down diff --git a/lib/Common/Reedsolomon/GenericGF.php b/lib/Common/Reedsolomon/GenericGF.php index 38e75a8..21612b7 100644 --- a/lib/Common/Reedsolomon/GenericGF.php +++ b/lib/Common/Reedsolomon/GenericGF.php @@ -172,11 +172,13 @@ public function multiply(int|float|null $a, int|float|null $b) return $this->expTable[($this->logTable[$a] + $this->logTable[$b]) % ($this->size - 1)]; } + /** @return int */ public function getSize() { return $this->size; } + /** @return int */ public function getGeneratorBase() { return $this->generatorBase; diff --git a/lib/IMagickLuminanceSource.php b/lib/IMagickLuminanceSource.php index f4ef0dc..e7a6acd 100644 --- a/lib/IMagickLuminanceSource.php +++ b/lib/IMagickLuminanceSource.php @@ -61,15 +61,17 @@ public function rotateCounterClockwise45(): void * Converts shorthand memory notation value to bytes * From http://php.net/manual/en/function.ini-get.php * - * @param int $val Memory size shorthand notation string + * @param string $val Memory size shorthand notation string + * + * @return float|int|string */ - protected static function kmgStringToBytes(string $val) + protected static function kmgStringToBytes(string $val): int|float|string { $val = trim($val); $last = strtolower($val[strlen($val) - 1]); $val = substr($val, 0, -1); switch ($last) { - // The 'G' modifier is available since PHP 5.1.0 + // The 'G' modifier is available since PHP 5.1.0 case 'g': $val *= 1024; // no break diff --git a/lib/Qrcode/Decoder/Decoder.php b/lib/Qrcode/Decoder/Decoder.php index c88279e..23cb11c 100644 --- a/lib/Qrcode/Decoder/Decoder.php +++ b/lib/Qrcode/Decoder/Decoder.php @@ -40,16 +40,13 @@ public function __construct() $this->rsDecoder = new ReedSolomonDecoder(GenericGF::$QR_CODE_FIELD_256); } - public function decode(BitMatrix|BitMatrixParser $variable, array|null $hints = null): string|DecoderResult + /** + * @throws FormatException + * @throws ChecksumException + */ + public function decode(BitMatrix|BitMatrixParser $variable, array|null $hints = null): string|DecoderResult { - if (is_array($variable)) { - return $this->decodeImage($variable, $hints); - } elseif ($variable instanceof BitMatrix) { - return $this->decodeBits($variable, $hints); - } elseif ($variable instanceof BitMatrixParser) { - return $this->decodeParser($variable, $hints); - } - die('decode error Decoder.php'); + return $this->decodeBits($variable, $hints); } /** @@ -93,7 +90,6 @@ public function decodeImage(array $image, $hints = null): string|DecoderResult */ public function decodeBits(\Zxing\Common\BitMatrix $bits, $hints = null): string|DecoderResult { - // Construct a parser and read version, error-correction level $parser = new BitMatrixParser($bits); $fe = null; @@ -107,7 +103,6 @@ public function decodeBits(\Zxing\Common\BitMatrix $bits, $hints = null): string } try { - // Revert the bit matrix $parser->remask(); diff --git a/lib/Qrcode/Detector/AlignmentPatternFinder.php b/lib/Qrcode/Detector/AlignmentPatternFinder.php index 07b1014..129af94 100644 --- a/lib/Qrcode/Detector/AlignmentPatternFinder.php +++ b/lib/Qrcode/Detector/AlignmentPatternFinder.php @@ -126,7 +126,7 @@ public function find() return $this->possibleCenters[0]; } - throw new NotFoundException("Bottom right alignment pattern not found"); + throw new NotFoundException("Bottom right alignment pattern not found"); } /** @@ -198,9 +198,9 @@ private static function centerFromEnd(array $stateCount, int $end) * alignment pattern to see if the same proportion is detected.

* * @param int $startI row where an alignment pattern was detected - * @param float $centerJ center of the section that appears to cross an alignment pattern + * @param int $centerJ center of the section that appears to cross an alignment pattern * @param int $maxCount maximum reasonable number of modules that should be - * observed in any reading state, based on the results of the horizontal scan + * observed in any reading state, based on the results of the horizontal scan * * @return float vertical center of alignment pattern, or {@link Float#NaN} if not found */ diff --git a/lib/Qrcode/Detector/Detector.php b/lib/Qrcode/Detector/Detector.php index f3dac17..e3d81c9 100644 --- a/lib/Qrcode/Detector/Detector.php +++ b/lib/Qrcode/Detector/Detector.php @@ -25,7 +25,6 @@ use Zxing\DecodeHintType; use Zxing\FormatException; use Zxing\NotFoundException; -use Zxing\Qrcode\Decoder\Version; use Zxing\ResultPoint; use Zxing\ResultPointCallback; @@ -54,7 +53,6 @@ public function __construct(private BitMatrix $image) */ final public function detect(array $hints = null): DetectorResult {/*Map*/ - $resultPointCallback = ($hints !== null && array_key_exists('NEED_RESULT_POINT_CALLBACK', $hints)) ? $hints['NEED_RESULT_POINT_CALLBACK'] : null; /* resultPointCallback = hints == null ? null : @@ -82,7 +80,6 @@ final protected function processFinderPatternInfo(FinderPatternInfo $info): \Zxi $alignmentPattern = null; // Anything above version 1 has an alignment pattern if ((is_countable($provisionalVersion->getAlignmentPatternCenters()) ? count($provisionalVersion->getAlignmentPatternCenters()) : 0) > 0) { - // Guess where a "bottom right" finder pattern would have been $bottomRightX = $topRight->getX() - $topLeft->getX() + $bottomLeft->getX(); $bottomRightY = $topRight->getY() - $topLeft->getY() + $bottomLeft->getY(); @@ -405,7 +402,7 @@ private static function createTransform( } private static function sampleGrid( - $image, + BitMatrix $image, PerspectiveTransform $transform, int $dimension ): \Zxing\Common\BitMatrix { @@ -414,7 +411,7 @@ private static function sampleGrid( return $sampler->sampleGrid_($image, $dimension, $dimension, $transform); } - final protected function getImage() + final protected function getImage(): BitMatrix { return $this->image; } diff --git a/lib/Qrcode/Detector/FinderPattern.php b/lib/Qrcode/Detector/FinderPattern.php index 7665302..ad6a4f2 100644 --- a/lib/Qrcode/Detector/FinderPattern.php +++ b/lib/Qrcode/Detector/FinderPattern.php @@ -17,7 +17,7 @@ namespace Zxing\Qrcode\Detector; -use Zxing\ResultPoint; +use Zxing\ResultPoint; /** *

Encapsulates a finder pattern, which are the three square patterns found in diff --git a/lib/Qrcode/Detector/FinderPatternFinder.php b/lib/Qrcode/Detector/FinderPatternFinder.php index f6b3b35..1a917a1 100644 --- a/lib/Qrcode/Detector/FinderPatternFinder.php +++ b/lib/Qrcode/Detector/FinderPatternFinder.php @@ -168,8 +168,8 @@ final public function find(array|null $hints): \Zxing\Qrcode\Detector\FinderPatt } /** - * @param $stateCount ; count of black/white/black/white/black pixels just read - * @param int[] $stateCount + * @param $maxVariance ; count of black/white/black/white/black pixels just read + * @param int[] $maxVariance * * @return bool iff the proportions of the counts is close enough to the 1/1/3/1/1 ratios used by finder patterns to be considered a match * diff --git a/lib/Qrcode/QRCodeReader.php b/lib/Qrcode/QRCodeReader.php index 6d65741..1c549ed 100644 --- a/lib/Qrcode/QRCodeReader.php +++ b/lib/Qrcode/QRCodeReader.php @@ -43,7 +43,7 @@ public function __construct() } /** - * @param null $hints + * @param array|null $hints * * @return Result * @throws \Zxing\FormatException diff --git a/lib/ResultPoint.php b/lib/ResultPoint.php index 986f9dc..8533cd5 100644 --- a/lib/ResultPoint.php +++ b/lib/ResultPoint.php @@ -44,8 +44,7 @@ public function __construct($x, $y) */ public static function orderBestPatterns(array $patterns): array { - -// Find distances between pattern centers + // Find distances between pattern centers $zeroOneDistance = self::distance($patterns[0], $patterns[1]); $oneTwoDistance = self::distance($patterns[1], $patterns[2]); $zeroTwoDistance = self::distance($patterns[0], $patterns[2]); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a23d614..bd3a81f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,11 +1,16 @@ - - - - lib - - + + - tests + tests + + + + lib + + + diff --git a/psalm.xml b/psalm.xml index 52a75ee..1c9b4d3 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,7 +1,6 @@ assertSame(false, $qrcode->text()); + $this->assertFalse($qrcode->text()); } public function testText2() @@ -38,7 +38,7 @@ public function testText2() 'NR_ALLOW_SKIP_ROWS' => 0 ]; $qrcode->decode($hints); - $this->assertSame(null, $qrcode->getError()); + $this->assertNull($qrcode->getError()); $this->assertInstanceOf(Result::class, $qrcode->getResult()); $this->assertEquals("https://www.gosuslugi.ru/covid-cert/verify/9770000014233333?lang=ru&ck=733a9d218d312fe134f1c2cc06e1a800", $qrcode->getResult()->getText()); $this->assertSame("https://www.gosuslugi.ru/covid-cert/verify/9770000014233333?lang=ru&ck=733a9d218d312fe134f1c2cc06e1a800", $qrcode->text($hints)); @@ -51,7 +51,7 @@ public function testText3() $qrcode->decode([ 'TRY_HARDER' => true ]); - $this->assertSame(null, $qrcode->getError()); + $this->assertNull($qrcode->getError()); $this->assertSame("https://www.gosuslugi.ru/covid-cert/verify/9770000014233333?lang=ru&ck=733a9d218d312fe134f1c2cc06e1a800", $qrcode->text()); } From 51cb0ac61402eaa5e6b48630014f29d149ccf9c9 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Sun, 7 Apr 2024 14:52:21 +0200 Subject: [PATCH 2/2] feat: update psalm 4 => 5 chore: migrate to PHPUnit11 chore: fix formatting --- lib/Qrcode/Detector/Detector.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/Qrcode/Detector/Detector.php b/lib/Qrcode/Detector/Detector.php index e3d81c9..87881af 100644 --- a/lib/Qrcode/Detector/Detector.php +++ b/lib/Qrcode/Detector/Detector.php @@ -401,8 +401,11 @@ private static function createTransform( ); } + /** + * @param BitMatrix $image + */ private static function sampleGrid( - BitMatrix $image, + $image, PerspectiveTransform $transform, int $dimension ): \Zxing\Common\BitMatrix { @@ -411,7 +414,10 @@ private static function sampleGrid( return $sampler->sampleGrid_($image, $dimension, $dimension, $transform); } - final protected function getImage(): BitMatrix + /** + * @return BitMatrix + */ + final protected function getImage() { return $this->image; }