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

brick / geo / 13742387334

08 Mar 2025 11:16PM UTC coverage: 49.73%. Remained the same
13742387334

push

github

BenMorel
Use array_reduce() in getBoundingBox()

10 of 30 new or added lines in 6 files covered. (33.33%)

49 existing lines in 8 files now uncovered.

1748 of 3515 relevant lines covered (49.73%)

974.97 hits per line

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

96.67
/src/BoundingBox.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use Brick\Geo\Exception\CoordinateSystemException;
8
use Brick\Geo\Exception\EmptyGeometryException;
9

10
/**
11
 * Represents a 2D or 3D bounding box calculated from a set of points. M coordinates are ignored.
12
 * This class is immutable.
13
 */
14
final class BoundingBox
15
{
16
    /**
17
     * Private constructor. Use BoundingBox::new() to obtain an instance.
18
     */
19
    private function __construct(
20
        public readonly ?CoordinateSystem $cs,
21
        public readonly ?float $swX,
22
        public readonly ?float $swY,
23
        public readonly ?float $swZ,
24
        public readonly ?float $neX,
25
        public readonly ?float $neY,
26
        public readonly ?float $neZ,
27
    ) {
28
    }
49✔
29

30
    /**
31
     * Creates an empty BoundingBox instance.
32
     */
33
    public static function new(): BoundingBox
34
    {
35
        return new BoundingBox(
49✔
36
            null,
49✔
37
            null,
49✔
38
            null,
49✔
39
            null,
49✔
40
            null,
49✔
41
            null,
49✔
42
            null,
49✔
43
        );
49✔
44
    }
45

46
    /**
47
     * Returns a copy of this BoundingBox extended with the given Point.
48
     *
49
     * @throws CoordinateSystemException
50
     */
51
    public function extendedWithPoint(Point $point) : BoundingBox
52
    {
53
        if ($point->isEmpty()) {
35✔
UNCOV
54
            return $this;
×
55
        }
56

57
        $point = $point->withoutM();
35✔
58

59
        if ($this->cs === null) {
35✔
60
            $cs = $point->coordinateSystem();
35✔
61
        } else {
62
            $cs = $this->cs;
35✔
63
            if (! $cs->isEqualTo($point->coordinateSystem())) {
35✔
64
                throw CoordinateSystemException::dimensionalityMix($cs, $point->coordinateSystem());
14✔
65
            }
66
        }
67

68
        $x = $point->x();
35✔
69
        $y = $point->y();
35✔
70
        $z = $point->z();
35✔
71

72
        $swX = ($this->swX === null) ? $x : min($this->swX, $x);
35✔
73
        $swY = ($this->swY === null) ? $y : min($this->swY, $y);
35✔
74

75
        $neX = ($this->neX === null) ? $x : max($this->neX, $x);
35✔
76
        $neY = ($this->neY === null) ? $y : max($this->neY, $y);
35✔
77

78
        if ($z !== null) {
35✔
79
            $swZ = ($this->swZ === null) ? $z : min($this->swZ, $z);
7✔
80
            $neZ = ($this->neZ === null) ? $z : max($this->neZ, $z);
7✔
81
        } else {
82
            $swZ = null;
28✔
83
            $neZ = null;
28✔
84
        }
85

86
        if (
87
            $swX === $this->swX && $swY === $this->swY && $swZ === $this->swZ &&
35✔
88
            $neX === $this->neX && $neY === $this->neY && $neZ === $this->neZ
35✔
89
        ) {
90
            return $this;
14✔
91
        }
92

93
        return new BoundingBox(
35✔
94
            $cs,
35✔
95
            $swX,
35✔
96
            $swY,
35✔
97
            $swZ,
35✔
98
            $neX,
35✔
99
            $neY,
35✔
100
            $neZ,
35✔
101
        );
35✔
102
    }
103

104
    /**
105
     * Returns a copy of this BoundingBox extended with the given BoundingBox.
106
     *
107
     * @throws CoordinateSystemException
108
     */
109
    public function extendedWithBoundingBox(BoundingBox $boundingBox) : BoundingBox
110
    {
111
        if ($boundingBox->isEmpty()) {
7✔
UNCOV
112
            return $this;
×
113
        }
114

115
        return $this
7✔
116
            ->extendedWithPoint($boundingBox->getSouthWest())
7✔
117
            ->extendedWithPoint($boundingBox->getNorthEast());
7✔
118
    }
119

120
    public function isEmpty() : bool
121
    {
122
        return $this->cs === null;
7✔
123
    }
124

125
    /**
126
     * Returns the south-west XY or XYZ point.
127
     *
128
     * @throws EmptyGeometryException
129
     */
130
    public function getSouthWest() : Point
131
    {
132
        if ($this->cs === null) {
28✔
133
            throw new EmptyGeometryException('The bounding box is empty.');
7✔
134
        }
135

136
        assert($this->swX !== null);
137
        assert($this->swY !== null);
138

139
        if ($this->cs->hasZ()) {
21✔
140
            assert($this->swZ !== null);
141
            $coords = [$this->swX, $this->swY, $this->swZ];
7✔
142
        } else {
143
            $coords = [$this->swX, $this->swY];
14✔
144
        }
145

146
        return new Point($this->cs, ...$coords);
21✔
147
    }
148

149
    /**
150
     * Returns the north-east XY or XYZ point.
151
     *
152
     * @throws EmptyGeometryException
153
     */
154
    public function getNorthEast() : Point
155
    {
156
        if ($this->cs === null) {
28✔
157
            throw new EmptyGeometryException('The bounding box is empty.');
7✔
158
        }
159

160
        if ($this->cs->hasZ()) {
21✔
161
            $coords = [$this->neX, $this->neY, $this->neZ];
7✔
162
        } else {
163
            $coords = [$this->neX, $this->neY];
14✔
164
        }
165

166
        /** @var list<float> $coords */
167
        return new Point($this->cs, ...$coords);
21✔
168
    }
169
}
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