• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / geo / 13350035722

16 Feb 2025 12:31AM UTC coverage: 82.19% (+34.9%) from 47.314%
13350035722

push

github

BenMorel
Use PHP 8.4 lazy proxies

30 of 49 new or added lines in 2 files covered. (61.22%)

55 existing lines in 4 files now uncovered.

1486 of 1808 relevant lines covered (82.19%)

1750.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.43
/src/IO/WKBReader.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\IO;
6

7
use Brick\Geo\CircularString;
8
use Brick\Geo\CompoundCurve;
9
use Brick\Geo\CurvePolygon;
10
use Brick\Geo\Geometry;
11
use Brick\Geo\Exception\GeometryIOException;
12
use Brick\Geo\GeometryCollection;
13
use Brick\Geo\LineString;
14
use Brick\Geo\MultiLineString;
15
use Brick\Geo\MultiPoint;
16
use Brick\Geo\MultiPolygon;
17
use Brick\Geo\Point;
18
use Brick\Geo\Polygon;
19
use Brick\Geo\PolyhedralSurface;
20
use Brick\Geo\TIN;
21
use Brick\Geo\Triangle;
22

23
/**
24
 * Builds geometries out of Well-Known Binary strings.
25
 */
26
class WKBReader extends AbstractWKBReader
27
{
28
    /**
29
     * @param string $wkb  The WKB to read.
30
     * @param int    $srid The optional SRID of the geometry.
31
     *
32
     * @throws GeometryIOException
33
     */
34
    public function read(string $wkb, int $srid = 0) : Geometry
35
    {
36
        $buffer = new WKBBuffer($wkb);
1,894✔
37
        $geometry = $this->readGeometry($buffer, $srid);
1,894✔
38

39
        if (! $buffer->isEndOfStream()) {
1,894✔
40
            throw GeometryIOException::invalidWKB('unexpected data at end of stream');
×
41
        }
42

43
        return $geometry;
1,894✔
44
    }
45

46
    /**
47
     * Introspects the given WKB and returns a proxy of the corresponding class.
48
     *
49
     * This prevents having to fully hydrate the underlying geometry object graph, while still returning an instance
50
     * of the correct geometry class.
51
     *
52
     * @throws GeometryIOException
53
     */
54
    public function readAsProxy(string $wkb, int $srid = 0) : Geometry
55
    {
56
        $buffer = new WKBBuffer($wkb);
×
57
        $buffer->readByteOrder();
×
58
        $geometryHeader = $this->readGeometryHeader($buffer);
×
59

NEW
60
        $geometryClass = $this->getGeometryClass($geometryHeader->geometryType);
×
NEW
61
        $reflectionClass = new \ReflectionClass($geometryClass);
×
62

63
        /** @var Geometry */
NEW
64
        return $reflectionClass->newLazyProxy(fn() => $geometryClass::fromBinary($wkb, $srid));
×
65
    }
66

67
    /**
68
     * @return class-string<Geometry>
69
     *
70
     * @throws GeometryIOException
71
     */
72
    private function getGeometryClass(int $geometryType) : string
73
    {
NEW
74
        return match ($geometryType) {
×
NEW
75
            Geometry::POINT => Point::class,
×
NEW
76
            Geometry::LINESTRING => LineString::class,
×
NEW
77
            Geometry::CIRCULARSTRING => CircularString::class,
×
NEW
78
            Geometry::COMPOUNDCURVE => CompoundCurve::class,
×
NEW
79
            Geometry::POLYGON => Polygon::class,
×
NEW
80
            Geometry::CURVEPOLYGON => CurvePolygon::class,
×
NEW
81
            Geometry::MULTIPOINT => MultiPoint::class,
×
NEW
82
            Geometry::MULTILINESTRING => MultiLineString::class,
×
NEW
83
            Geometry::MULTIPOLYGON => MultiPolygon::class,
×
NEW
84
            Geometry::GEOMETRYCOLLECTION => GeometryCollection::class,
×
NEW
85
            Geometry::POLYHEDRALSURFACE => PolyhedralSurface::class,
×
NEW
86
            Geometry::TIN => TIN::class,
×
NEW
87
            Geometry::TRIANGLE => Triangle::class,
×
NEW
88
            default => throw GeometryIOException::unsupportedWKBType($geometryType),
×
UNCOV
89
        };
×
90
    }
91

92
    protected function readGeometryHeader(WKBBuffer $buffer) : WKBGeometryHeader
93
    {
94
        $wkbType = $buffer->readUnsignedLong();
1,894✔
95

96
        if ($wkbType < 0 || $wkbType >= 4000) {
1,894✔
97
            throw GeometryIOException::unsupportedWKBType($wkbType);
×
98
        }
99

100
        $geometryType = $wkbType % 1000;
1,894✔
101
        $dimension = ($wkbType - $geometryType) / 1000;
1,894✔
102

103
        $hasZ = ($dimension === 1 || $dimension === 3);
1,894✔
104
        $hasM = ($dimension === 2 || $dimension === 3);
1,894✔
105

106
        return new WKBGeometryHeader($geometryType, $hasZ, $hasM);
1,894✔
107
    }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc