Skip to content

Commit 5bbba9a

Browse files
committed
Passes ShapeType instead of int and adds a test case for ShapeType::Unknown
1 parent ebfc8fd commit 5bbba9a

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ parameters:
127127

128128
-
129129
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
130-
count: 17
130+
count: 18
131131
path: tests/ShapeFileTest.php
132132

133133
-
@@ -159,4 +159,3 @@ parameters:
159159
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
160160
count: 1
161161
path: tests/UtilTest.php
162-

src/ShapeFile.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ private function loadHeaders(): bool
374374
$this->readSHP(4);
375375

376376
$shapeType = Util::loadData('V', $this->readSHP(4));
377-
$this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown;
377+
if ($shapeType === false) {
378+
$this->shapeType = ShapeType::Unknown;
379+
} else {
380+
$this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown;
381+
}
378382

379383
$this->boundingBox = [];
380384
$this->boundingBox['xmin'] = Util::loadData('d', $this->readSHP(8));

tests/ShapeFileTest.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ public function testShapeName(): void
305305
$this->assertEquals('Point', $obj->getShapeName());
306306
$obj = new ShapeRecord(ShapeType::Null);
307307
$this->assertEquals('Null Shape', $obj->getShapeName());
308+
$obj = new ShapeRecord(ShapeType::Unknown);
309+
$this->assertEquals('Unknown Shape', $obj->getShapeName());
308310
}
309311

310312
/**
@@ -314,14 +316,13 @@ public function testShapeName(): void
314316
*
315317
* @dataProvider shapesProvider
316318
*/
317-
public function testShapeSaveLoad(int $shapeType, array $points): void
319+
public function testShapeSaveLoad(ShapeType $shapeType, array $points): void
318320
{
319-
$shapeEnum = ShapeType::tryFrom($shapeType) ?? ShapeType::Unknown;
320-
$filename = './data/test_shape-' . $shapeType . '.*';
321-
$shp = new ShapeFile($shapeEnum);
321+
$filename = './data/test_shape-' . $shapeType->value . '.*';
322+
$shp = new ShapeFile($shapeType);
322323
$shp->setDBFHeader([['ID', 'N', 19, 0], ['DESC', 'C', 14, 0]]);
323324

324-
$record0 = new ShapeRecord($shapeEnum);
325+
$record0 = new ShapeRecord($shapeType);
325326

326327
foreach ($points as $point) {
327328
$record0->addPoint($point[0], $point[1]);
@@ -331,7 +332,7 @@ public function testShapeSaveLoad(int $shapeType, array $points): void
331332

332333
$shp->saveToFile($filename);
333334

334-
$shp2 = new ShapeFile($shapeEnum);
335+
$shp2 = new ShapeFile($shapeType);
335336
$shp2->loadFromFile($filename);
336337

337338
$this->assertEquals(count($shp->records), count($shp2->records));
@@ -355,7 +356,7 @@ public function testShapeSaveLoad(int $shapeType, array $points): void
355356
/**
356357
* Data provider for save/load testing.
357358
*
358-
* @psalm-return list<array{int, list<array{mixed[], int}>}>
359+
* @psalm-return list<array{ShapeType, list<array{mixed[], int}>}>
359360
*/
360361
public static function shapesProvider(): array
361362
{
@@ -384,18 +385,18 @@ public static function shapesProvider(): array
384385
];
385386

386387
return [
387-
[ShapeType::Point->value, $pointsForPointType],
388-
[ShapeType::PolyLine->value, $pointsForPolyLineType],
389-
[ShapeType::Polygon->value, $pointsForPolygonType],
390-
[ShapeType::MultiPoint->value, $pointsForMultiPointType],
391-
[ShapeType::PointZ->value, $pointsForPointType],
392-
[ShapeType::PolyLineZ->value, $pointsForPolyLineType],
393-
[ShapeType::PolygonZ->value, $pointsForPolygonType],
394-
[ShapeType::MultiPointZ->value, $pointsForMultiPointType],
395-
[ShapeType::PointM->value, $pointsForPointType],
396-
[ShapeType::PolyLineM->value, $pointsForPolyLineType],
397-
[ShapeType::PolygonM->value, $pointsForPolygonType],
398-
[ShapeType::MultiPointM->value, $pointsForMultiPointType],
388+
[ShapeType::Point, $pointsForPointType],
389+
[ShapeType::PolyLine, $pointsForPolyLineType],
390+
[ShapeType::Polygon, $pointsForPolygonType],
391+
[ShapeType::MultiPoint, $pointsForMultiPointType],
392+
[ShapeType::PointZ, $pointsForPointType],
393+
[ShapeType::PolyLineZ, $pointsForPolyLineType],
394+
[ShapeType::PolygonZ, $pointsForPolygonType],
395+
[ShapeType::MultiPointZ, $pointsForMultiPointType],
396+
[ShapeType::PointM, $pointsForPointType],
397+
[ShapeType::PolyLineM, $pointsForPolyLineType],
398+
[ShapeType::PolygonM, $pointsForPolygonType],
399+
[ShapeType::MultiPointM, $pointsForMultiPointType],
399400
];
400401
}
401402

0 commit comments

Comments
 (0)