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

brick / geo / 13731448456

07 Mar 2025 11:55PM UTC coverage: 49.73%. Remained the same
13731448456

push

github

BenMorel
Use array_reduce() in getBoundingBox()

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

8 existing lines in 4 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

70.21
/src/CompoundCurve.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use ArrayIterator;
8
use Brick\Geo\Attribute\NoProxy;
9
use Brick\Geo\Exception\CoordinateSystemException;
10
use Brick\Geo\Exception\EmptyGeometryException;
11
use Brick\Geo\Exception\InvalidGeometryException;
12
use Brick\Geo\Exception\NoSuchGeometryException;
13
use Brick\Geo\Projector\Projector;
14
use Override;
15

16
/**
17
 * A CompoundCurve is a collection of zero or more continuous CircularString or LineString instances.
18
 *
19
 * @template-implements \IteratorAggregate<int<0, max>, LineString|CircularString>
20
 * @final
21
 */
22
class CompoundCurve extends Curve implements \Countable, \IteratorAggregate
23
{
24
    /**
25
     * The Curves that compose this CompoundCurve.
26
     *
27
     * This array can be empty.
28
     *
29
     * @var list<LineString|CircularString>
30
     */
31
    protected array $curves = [];
32

33
    /**
34
     * The coordinate system of each of the curves must match the one of the CompoundCurve.
35
     *
36
     * @param CoordinateSystem             $cs     The coordinate system of the CompoundCurve.
37
     * @param LineString|CircularString ...$curves The curves that compose the CompoundCurve.
38
     *
39
     * @throws EmptyGeometryException    If any of the input curves is empty.
40
     * @throws InvalidGeometryException  If the compound curve is not continuous.
41
     * @throws CoordinateSystemException If different coordinate systems are used.
42
     */
43
    public function __construct(CoordinateSystem $cs, LineString|CircularString ...$curves)
44
    {
45
        parent::__construct($cs, ! $curves);
2,190✔
46

47
        if (! $curves) {
2,190✔
48
            return;
860✔
49
        }
50

51
        CoordinateSystem::check($this, ...$curves);
1,344✔
52

53
        $previousCurve = null;
1,344✔
54

55
        foreach ($curves as $curve) {
1,344✔
56
            if ($previousCurve) {
1,344✔
57
                $endPoint = $previousCurve->endPoint();
1,246✔
58
                $startPoint = $curve->startPoint();
1,246✔
59

60
                if ($endPoint != $startPoint) { // on purpose by-value comparison!
1,246✔
61
                    throw new InvalidGeometryException('Incontinuous compound curve.');
7✔
62
                }
63
            }
64

65
            $previousCurve = $curve;
1,344✔
66
        }
67

68
        $this->curves = array_values($curves);
1,337✔
69
    }
70

71
    /**
72
     * Creates a non-empty CompoundCurve composed of the given curves.
73
     *
74
     * @param LineString|CircularString    $curve1 The first curve.
75
     * @param LineString|CircularString ...$curveN The subsequent curves, if any.
76
     *
77
     * @throws EmptyGeometryException    If any of the input curves is empty.
78
     * @throws InvalidGeometryException  If the compound curve is not continuous.
79
     * @throws CoordinateSystemException If the curves use different coordinate systems.
80
     */
81
    public static function of(LineString|CircularString $curve1, LineString|CircularString ...$curveN) : CompoundCurve
82
    {
83
        return new CompoundCurve($curve1->coordinateSystem(), $curve1, ...$curveN);
×
84
    }
85

86
    #[Override]
87
    public function startPoint() : Point
88
    {
89
        if (count($this->curves) === 0) {
112✔
90
            throw new EmptyGeometryException('The CompoundCurve is empty and has no start point.');
56✔
91
        }
92

93
        return $this->curves[0]->startPoint();
56✔
94
    }
95

96
    #[Override]
97
    public function endPoint() : Point
98
    {
99
        $count = count($this->curves);
112✔
100

101
        if ($count === 0) {
112✔
102
            throw new EmptyGeometryException('The CompoundCurve is empty and has no end point.');
56✔
103
        }
104

105
        return $this->curves[$count - 1]->endPoint();
56✔
106
    }
107

108
    /**
109
     * Returns the number of Curves in this CompoundCurve.
110
     */
111
    public function numCurves() : int
112
    {
113
        return count($this->curves);
56✔
114
    }
115

116
    /**
117
     * Returns the specified Curve N in this CompoundCurve.
118
     *
119
     * @param int $n The curve number, 1-based.
120
     *
121
     * @throws NoSuchGeometryException If there is no Curve at this index.
122
     */
123
    public function curveN(int $n) : LineString|CircularString
124
    {
125
        if (! isset($this->curves[$n - 1])) {
329✔
126
            throw new NoSuchGeometryException('There is no Curve in this CompoundCurve at index ' . $n);
224✔
127
        }
128

129
        return $this->curves[$n - 1];
105✔
130
    }
131

132
    /**
133
     * Returns the curves that compose this CompoundCurve.
134
     *
135
     * @return list<LineString|CircularString>
136
     */
137
    public function curves() : array
138
    {
139
        return $this->curves;
×
140
    }
141

142
    #[NoProxy, Override]
143
    public function geometryType() : string
144
    {
145
        return 'CompoundCurve';
839✔
146
    }
147

148
    #[NoProxy, Override]
149
    public function geometryTypeBinary() : int
150
    {
151
        return Geometry::COMPOUNDCURVE;
448✔
152
    }
153

154
    #[Override]
155
    public function getBoundingBox() : BoundingBox
156
    {
NEW
157
        return array_reduce(
×
NEW
158
            $this->curves,
×
NEW
159
            fn (BoundingBox $boundingBox, Curve $curve) => $boundingBox->extendedWithBoundingBox($curve->getBoundingBox()),
×
NEW
160
            BoundingBox::new(),
×
NEW
161
        );
×
162
    }
163

164
    /**
165
     * @return list<list<list<float>>>
166
     */
167
    #[Override]
168
    public function toArray() : array
169
    {
170
        return array_map(
252✔
171
            fn (Curve $curve) => $curve->toArray(),
252✔
172
            $this->curves,
252✔
173
        );
252✔
174
    }
175

176
    #[Override]
177
    public function project(Projector $projector): CompoundCurve
178
    {
179
        return new CompoundCurve(
×
180
            $projector->getTargetCoordinateSystem($this->coordinateSystem),
×
181
            ...array_map(
×
182
                fn (Curve $curve) => $curve->project($projector),
×
183
                $this->curves,
×
UNCOV
184
            ),
×
UNCOV
185
        );
×
186
    }
187

188
    /**
189
     * Returns the number of curves in this CompoundCurve.
190
     */
191
    #[Override]
192
    public function count() : int
193
    {
194
        return count($this->curves);
455✔
195
    }
196

197
    /**
198
     * Returns an iterator for the curves in this CompoundCurve.
199
     *
200
     * @return ArrayIterator<int<0, max>, LineString|CircularString>
201
     */
202
    #[Override]
203
    public function getIterator() : ArrayIterator
204
    {
205
        return new ArrayIterator($this->curves);
980✔
206
    }
207

208
    /**
209
     * Returns a copy of this CompoundCurve, with the given curves added.
210
     */
211
    public function withAddedCurves(LineString|CircularString ...$curves): CompoundCurve
212
    {
213
        return new CompoundCurve($this->coordinateSystem, ...$this->curves, ...$curves);
56✔
214
    }
215
}
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