Skip to content

Commit 55cc5e4

Browse files
committed
Create the PhpMyAdmin\ShapeFile\ShapeType class
Replaces the hard-coded shape type integers with class constants. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent ab4a803 commit 55cc5e4

9 files changed

Lines changed: 192 additions & 148 deletions

File tree

examples/create_shapefile.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,30 @@
2525

2626
use PhpMyAdmin\ShapeFile\ShapeFile;
2727
use PhpMyAdmin\ShapeFile\ShapeRecord;
28+
use PhpMyAdmin\ShapeFile\ShapeType;
2829

2930
require_once __DIR__ . '/../vendor/autoload.php';
3031

31-
$shp = new ShapeFile(1, [
32+
$shp = new ShapeFile(ShapeType::POINT, [
3233
'xmin' => 464079.002268,
3334
'ymin' => 2120153.74792,
3435
'xmax' => 505213.52849,
3536
'ymax' => 2163205.70036,
3637
]);
3738

38-
$record0 = new ShapeRecord(1);
39+
$record0 = new ShapeRecord(ShapeType::POINT);
3940
$record0->addPoint([
4041
'x' => 482131.764567,
4142
'y' => 2143634.39608,
4243
]);
4344

44-
$record1 = new ShapeRecord(1);
45+
$record1 = new ShapeRecord(ShapeType::POINT);
4546
$record1->addPoint([
4647
'x' => 472131.764567,
4748
'y' => 2143634.39608,
4849
]);
4950

50-
$record2 = new ShapeRecord(1);
51+
$record2 = new ShapeRecord(ShapeType::POINT);
5152
$record2->addPoint([
5253
'x' => 492131.764567,
5354
'y' => 2143634.39608,

examples/read.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
use PhpMyAdmin\ShapeFile\ShapeFile;
27+
use PhpMyAdmin\ShapeFile\ShapeType;
2728

2829
/**
2930
* Displays content of given file.
@@ -33,7 +34,7 @@
3334
// phpcs:ignore Squiz.Functions.GlobalFunction.Found
3435
function display_file(string $filename): void
3536
{
36-
$shp = new ShapeFile(1);
37+
$shp = new ShapeFile(ShapeType::POINT);
3738
$shp->loadFromFile($filename);
3839

3940
$i = 1;

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
5-
count: 2
6-
path: src/ShapeFile.php
7-
83
-
94
message: "#^Cannot cast mixed to int\\.$#"
105
count: 1
@@ -55,11 +50,6 @@ parameters:
5550
count: 4
5651
path: src/ShapeRecord.php
5752

58-
-
59-
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
60-
count: 1
61-
path: src/ShapeRecord.php
62-
6353
-
6454
message: "#^Cannot access an offset on mixed\\.$#"
6555
count: 4

psalm-baseline.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@
117117
<code>$pointData</code>
118118
<code>$pointData</code>
119119
<code><![CDATA[$point['m']]]></code>
120-
<code><![CDATA[$point['m']]]></code>
121120
<code><![CDATA[$point['z']]]></code>
122121
<code>$recordNumber</code>
123122
<code>$shapeType</code>

src/ShapeFile.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ public function addRecord(ShapeRecord $record): int
222222
$this->updateBBox('x', $record->shpData);
223223
$this->updateBBox('y', $record->shpData);
224224

225-
if (in_array($this->shapeType, [11, 13, 15, 18, 21, 23, 25, 28])) {
225+
if (in_array($this->shapeType, ShapeType::MEASURED_TYPES, true)) {
226226
$this->updateBBox('m', $record->shpData);
227227
}
228228

229-
if (in_array($this->shapeType, [11, 13, 15, 18])) {
229+
if (in_array($this->shapeType, ShapeType::TYPES_WITH_Z, true)) {
230230
$this->updateBBox('z', $record->shpData);
231231
}
232232

@@ -669,10 +669,12 @@ public function eofSHP(): bool
669669

670670
/**
671671
* Returns shape name.
672+
*
673+
* @psalm-return non-empty-string
672674
*/
673675
public function getShapeName(): string
674676
{
675-
return Util::nameShape($this->shapeType);
677+
return ShapeType::name($this->shapeType);
676678
}
677679

678680
/**

src/ShapeRecord.php

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ public function loadFromFile(ShapeFile $shapeFile, $dbfFile): void
7979
}
8080

8181
match ($this->shapeType) {
82-
0 => $this->loadNullRecord(),
83-
1 => $this->loadPointRecord(),
84-
21 => $this->loadPointMRecord(),
85-
11 => $this->loadPointZRecord(),
86-
3 => $this->loadPolyLineRecord(),
87-
23 => $this->loadPolyLineMRecord(),
88-
13 => $this->loadPolyLineZRecord(),
89-
5 => $this->loadPolygonRecord(),
90-
25 => $this->loadPolygonMRecord(),
91-
15 => $this->loadPolygonZRecord(),
92-
8 => $this->loadMultiPointRecord(),
93-
28 => $this->loadMultiPointMRecord(),
94-
18 => $this->loadMultiPointZRecord(),
82+
ShapeType::NULL => $this->loadNullRecord(),
83+
ShapeType::POINT => $this->loadPointRecord(),
84+
ShapeType::POINT_M => $this->loadPointMRecord(),
85+
ShapeType::POINT_Z => $this->loadPointZRecord(),
86+
ShapeType::POLY_LINE => $this->loadPolyLineRecord(),
87+
ShapeType::POLY_LINE_M => $this->loadPolyLineMRecord(),
88+
ShapeType::POLY_LINE_Z => $this->loadPolyLineZRecord(),
89+
ShapeType::POLYGON => $this->loadPolygonRecord(),
90+
ShapeType::POLYGON_M => $this->loadPolygonMRecord(),
91+
ShapeType::POLYGON_Z => $this->loadPolygonZRecord(),
92+
ShapeType::MULTI_POINT => $this->loadMultiPointRecord(),
93+
ShapeType::MULTI_POINT_M => $this->loadMultiPointMRecord(),
94+
ShapeType::MULTI_POINT_Z => $this->loadMultiPointZRecord(),
9595
default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)),
9696
};
9797

@@ -126,19 +126,19 @@ public function saveToFile($shpFile, $dbfFile, int $recordNumber): void
126126
$this->saveHeaders();
127127

128128
match ($this->shapeType) {
129-
0 => null, // Nothing to save
130-
1 => $this->savePointRecord(),
131-
21 => $this->savePointMRecord(),
132-
11 => $this->savePointZRecord(),
133-
3 => $this->savePolyLineRecord(),
134-
23 => $this->savePolyLineMRecord(),
135-
13 => $this->savePolyLineZRecord(),
136-
5 => $this->savePolygonRecord(),
137-
25 => $this->savePolygonMRecord(),
138-
15 => $this->savePolygonZRecord(),
139-
8 => $this->saveMultiPointRecord(),
140-
28 => $this->saveMultiPointMRecord(),
141-
18 => $this->saveMultiPointZRecord(),
129+
ShapeType::NULL => null, // Nothing to save
130+
ShapeType::POINT => $this->savePointRecord(),
131+
ShapeType::POINT_M => $this->savePointMRecord(),
132+
ShapeType::POINT_Z => $this->savePointZRecord(),
133+
ShapeType::POLY_LINE => $this->savePolyLineRecord(),
134+
ShapeType::POLY_LINE_M => $this->savePolyLineMRecord(),
135+
ShapeType::POLY_LINE_Z => $this->savePolyLineZRecord(),
136+
ShapeType::POLYGON => $this->savePolygonRecord(),
137+
ShapeType::POLYGON_M => $this->savePolygonMRecord(),
138+
ShapeType::POLYGON_Z => $this->savePolygonZRecord(),
139+
ShapeType::MULTI_POINT => $this->saveMultiPointRecord(),
140+
ShapeType::MULTI_POINT_M => $this->saveMultiPointMRecord(),
141+
ShapeType::MULTI_POINT_Z => $this->saveMultiPointZRecord(),
142142
default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)),
143143
};
144144

@@ -584,12 +584,12 @@ private function adjustBBox(array $point): void
584584
*/
585585
private function adjustPoint(array $point): array
586586
{
587-
$type = $this->shapeType / 10;
588-
if ($type >= 2) {
587+
if (in_array($this->shapeType, ShapeType::MEASURED_TYPES, true)) {
589588
$point['m'] ??= 0.0;
590-
} elseif ($type >= 1) {
589+
}
590+
591+
if (in_array($this->shapeType, ShapeType::TYPES_WITH_Z, true)) {
591592
$point['z'] ??= 0.0;
592-
$point['m'] ??= 0.0;
593593
}
594594

595595
return $point;
@@ -605,30 +605,30 @@ public function addPoint(array $point, int $partIndex = 0): void
605605
{
606606
$point = $this->adjustPoint($point);
607607
switch ($this->shapeType) {
608-
case 0:
608+
case ShapeType::NULL:
609609
//Don't add anything
610610
return;
611611

612-
case 1:
613-
case 11:
614-
case 21:
612+
case ShapeType::POINT:
613+
case ShapeType::POINT_Z:
614+
case ShapeType::POINT_M:
615615
//Substitutes the value of the current point
616616
$this->shpData = $point;
617617
break;
618-
case 3:
619-
case 5:
620-
case 13:
621-
case 15:
622-
case 23:
623-
case 25:
618+
case ShapeType::POLY_LINE:
619+
case ShapeType::POLYGON:
620+
case ShapeType::POLY_LINE_Z:
621+
case ShapeType::POLYGON_Z:
622+
case ShapeType::POLY_LINE_M:
623+
case ShapeType::POLYGON_M:
624624
//Adds a new point to the selected part
625625
$this->shpData['parts'][$partIndex]['points'][] = $point;
626626
$this->shpData['numparts'] = count($this->shpData['parts']);
627627
$this->shpData['numpoints'] = 1 + ($this->shpData['numpoints'] ?? 0);
628628
break;
629-
case 8:
630-
case 18:
631-
case 28:
629+
case ShapeType::MULTI_POINT:
630+
case ShapeType::MULTI_POINT_Z:
631+
case ShapeType::MULTI_POINT_M:
632632
//Adds a new point
633633
$this->shpData['points'][] = $point;
634634
$this->shpData['numpoints'] = 1 + ($this->shpData['numpoints'] ?? 0);
@@ -651,30 +651,30 @@ public function addPoint(array $point, int $partIndex = 0): void
651651
public function deletePoint(int $pointIndex = 0, int $partIndex = 0): void
652652
{
653653
switch ($this->shapeType) {
654-
case 0:
654+
case ShapeType::NULL:
655655
//Don't delete anything
656656
break;
657-
case 1:
658-
case 11:
659-
case 21:
657+
case ShapeType::POINT:
658+
case ShapeType::POINT_Z:
659+
case ShapeType::POINT_M:
660660
//Sets the value of the point to zero
661661
$this->shpData['x'] = 0.0;
662662
$this->shpData['y'] = 0.0;
663-
if (in_array($this->shapeType, [11, 21])) {
663+
if (in_array($this->shapeType, [ShapeType::POINT_Z, ShapeType::POINT_M], true)) {
664664
$this->shpData['m'] = 0.0;
665665
}
666666

667-
if ($this->shapeType === 11) {
667+
if ($this->shapeType === ShapeType::POINT_Z) {
668668
$this->shpData['z'] = 0.0;
669669
}
670670

671671
break;
672-
case 3:
673-
case 5:
674-
case 13:
675-
case 15:
676-
case 23:
677-
case 25:
672+
case ShapeType::POLY_LINE:
673+
case ShapeType::POLYGON:
674+
case ShapeType::POLY_LINE_Z:
675+
case ShapeType::POLYGON_Z:
676+
case ShapeType::POLY_LINE_M:
677+
case ShapeType::POLYGON_M:
678678
//Deletes the point from the selected part, if exists
679679
if (
680680
isset($this->shpData['parts'][$partIndex])
@@ -694,9 +694,9 @@ public function deletePoint(int $pointIndex = 0, int $partIndex = 0): void
694694
}
695695

696696
break;
697-
case 8:
698-
case 18:
699-
case 28:
697+
case ShapeType::MULTI_POINT:
698+
case ShapeType::MULTI_POINT_Z:
699+
case ShapeType::MULTI_POINT_M:
700700
//Deletes the point, if exists
701701
if (isset($this->shpData['points'][$pointIndex])) {
702702
$count = count($this->shpData['points']) - 1;
@@ -724,52 +724,52 @@ public function getContentLength(): int|null
724724
// The content length for a record is the length of the record contents section measured in 16-bit words.
725725
// one coordinate makes 4 16-bit words (64 bit double)
726726
switch ($this->shapeType) {
727-
case 0:
727+
case ShapeType::NULL:
728728
$result = 0;
729729
break;
730-
case 1:
730+
case ShapeType::POINT:
731731
$result = 10;
732732
break;
733-
case 21:
733+
case ShapeType::POINT_M:
734734
$result = 10 + 4;
735735
break;
736-
case 11:
736+
case ShapeType::POINT_Z:
737737
$result = 10 + 8;
738738
break;
739-
case 3:
740-
case 5:
739+
case ShapeType::POLY_LINE:
740+
case ShapeType::POLYGON:
741741
$count = count($this->shpData['parts']);
742742
$result = 22 + 2 * $count;
743743
for ($i = 0; $i < $count; ++$i) {
744744
$result += 8 * count($this->shpData['parts'][$i]['points']);
745745
}
746746

747747
break;
748-
case 23:
749-
case 25:
748+
case ShapeType::POLY_LINE_M:
749+
case ShapeType::POLYGON_M:
750750
$count = count($this->shpData['parts']);
751751
$result = 22 + (2 * 4) + 2 * $count;
752752
for ($i = 0; $i < $count; ++$i) {
753753
$result += (8 + 4) * count($this->shpData['parts'][$i]['points']);
754754
}
755755

756756
break;
757-
case 13:
758-
case 15:
757+
case ShapeType::POLY_LINE_Z:
758+
case ShapeType::POLYGON_Z:
759759
$count = count($this->shpData['parts']);
760760
$result = 22 + (4 * 4) + 2 * $count;
761761
for ($i = 0; $i < $count; ++$i) {
762762
$result += (8 + 8) * count($this->shpData['parts'][$i]['points']);
763763
}
764764

765765
break;
766-
case 8:
766+
case ShapeType::MULTI_POINT:
767767
$result = 20 + 8 * count($this->shpData['points']);
768768
break;
769-
case 28:
769+
case ShapeType::MULTI_POINT_M:
770770
$result = 20 + (2 * 4) + (8 + 4) * count($this->shpData['points']);
771771
break;
772-
case 18:
772+
case ShapeType::MULTI_POINT_Z:
773773
$result = 20 + (4 * 4) + (8 + 8) * count($this->shpData['points']);
774774
break;
775775
default:
@@ -815,9 +815,11 @@ public function setError(string $error): void
815815

816816
/**
817817
* Returns shape name.
818+
*
819+
* @psalm-return non-empty-string
818820
*/
819821
public function getShapeName(): string
820822
{
821-
return Util::nameShape($this->shapeType);
823+
return ShapeType::name($this->shapeType);
822824
}
823825
}

0 commit comments

Comments
 (0)