From 5bbba9a4cf531cdce9f2138116f3976c7d556f61 Mon Sep 17 00:00:00 2001 From: Julien Dephix Date: Thu, 10 Oct 2024 11:43:58 +0200 Subject: [PATCH] Passes ShapeType instead of int and adds a test case for ShapeType::Unknown --- phpstan-baseline.neon | 3 +-- src/ShapeFile.php | 6 +++++- tests/ShapeFileTest.php | 39 ++++++++++++++++++++------------------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 19a9a22..139959d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -127,7 +127,7 @@ parameters: - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#" - count: 17 + count: 18 path: tests/ShapeFileTest.php - @@ -159,4 +159,3 @@ parameters: message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#" count: 1 path: tests/UtilTest.php - diff --git a/src/ShapeFile.php b/src/ShapeFile.php index 01ea678..851e398 100644 --- a/src/ShapeFile.php +++ b/src/ShapeFile.php @@ -374,7 +374,11 @@ private function loadHeaders(): bool $this->readSHP(4); $shapeType = Util::loadData('V', $this->readSHP(4)); - $this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown; + if ($shapeType === false) { + $this->shapeType = ShapeType::Unknown; + } else { + $this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown; + } $this->boundingBox = []; $this->boundingBox['xmin'] = Util::loadData('d', $this->readSHP(8)); diff --git a/tests/ShapeFileTest.php b/tests/ShapeFileTest.php index d254f9f..6878503 100644 --- a/tests/ShapeFileTest.php +++ b/tests/ShapeFileTest.php @@ -305,6 +305,8 @@ public function testShapeName(): void $this->assertEquals('Point', $obj->getShapeName()); $obj = new ShapeRecord(ShapeType::Null); $this->assertEquals('Null Shape', $obj->getShapeName()); + $obj = new ShapeRecord(ShapeType::Unknown); + $this->assertEquals('Unknown Shape', $obj->getShapeName()); } /** @@ -314,14 +316,13 @@ public function testShapeName(): void * * @dataProvider shapesProvider */ - public function testShapeSaveLoad(int $shapeType, array $points): void + public function testShapeSaveLoad(ShapeType $shapeType, array $points): void { - $shapeEnum = ShapeType::tryFrom($shapeType) ?? ShapeType::Unknown; - $filename = './data/test_shape-' . $shapeType . '.*'; - $shp = new ShapeFile($shapeEnum); + $filename = './data/test_shape-' . $shapeType->value . '.*'; + $shp = new ShapeFile($shapeType); $shp->setDBFHeader([['ID', 'N', 19, 0], ['DESC', 'C', 14, 0]]); - $record0 = new ShapeRecord($shapeEnum); + $record0 = new ShapeRecord($shapeType); foreach ($points as $point) { $record0->addPoint($point[0], $point[1]); @@ -331,7 +332,7 @@ public function testShapeSaveLoad(int $shapeType, array $points): void $shp->saveToFile($filename); - $shp2 = new ShapeFile($shapeEnum); + $shp2 = new ShapeFile($shapeType); $shp2->loadFromFile($filename); $this->assertEquals(count($shp->records), count($shp2->records)); @@ -355,7 +356,7 @@ public function testShapeSaveLoad(int $shapeType, array $points): void /** * Data provider for save/load testing. * - * @psalm-return list}> + * @psalm-return list}> */ public static function shapesProvider(): array { @@ -384,18 +385,18 @@ public static function shapesProvider(): array ]; return [ - [ShapeType::Point->value, $pointsForPointType], - [ShapeType::PolyLine->value, $pointsForPolyLineType], - [ShapeType::Polygon->value, $pointsForPolygonType], - [ShapeType::MultiPoint->value, $pointsForMultiPointType], - [ShapeType::PointZ->value, $pointsForPointType], - [ShapeType::PolyLineZ->value, $pointsForPolyLineType], - [ShapeType::PolygonZ->value, $pointsForPolygonType], - [ShapeType::MultiPointZ->value, $pointsForMultiPointType], - [ShapeType::PointM->value, $pointsForPointType], - [ShapeType::PolyLineM->value, $pointsForPolyLineType], - [ShapeType::PolygonM->value, $pointsForPolygonType], - [ShapeType::MultiPointM->value, $pointsForMultiPointType], + [ShapeType::Point, $pointsForPointType], + [ShapeType::PolyLine, $pointsForPolyLineType], + [ShapeType::Polygon, $pointsForPolygonType], + [ShapeType::MultiPoint, $pointsForMultiPointType], + [ShapeType::PointZ, $pointsForPointType], + [ShapeType::PolyLineZ, $pointsForPolyLineType], + [ShapeType::PolygonZ, $pointsForPolygonType], + [ShapeType::MultiPointZ, $pointsForMultiPointType], + [ShapeType::PointM, $pointsForPointType], + [ShapeType::PolyLineM, $pointsForPolyLineType], + [ShapeType::PolygonM, $pointsForPolygonType], + [ShapeType::MultiPointM, $pointsForMultiPointType], ]; }