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

brick / geo / 13731438031

07 Mar 2025 11:54PM UTC coverage: 49.73%. Remained the same
13731438031

push

github

BenMorel
Use array_reduce() in getBoundingBox()

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

48 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

68.18
/src/CircularString.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 CircularString is a Curve made of zero or more connected circular arc segments.
18
 *
19
 * A circular arc segment is a curved segment defined by three points in a two-dimensional plane;
20
 * the first point cannot be the same as the third point.
21
 *
22
 * @template-implements \IteratorAggregate<Point>
23
 * @final
24
 */
25
class CircularString extends Curve implements \Countable, \IteratorAggregate
26
{
27
    /**
28
     * The Points that compose this CircularString.
29
     *
30
     * An empty CircularString contains no points.
31
     *
32
     * @var list<Point>
33
     */
34
    protected array $points = [];
35

36
    /**
37
     * @throws InvalidGeometryException  If the number of points is invalid.
38
     * @throws CoordinateSystemException If different coordinate systems are used.
39
     */
40
    public function __construct(CoordinateSystem $cs, Point ...$points)
41
    {
42
        parent::__construct($cs, ! $points);
3,444✔
43

44
        if (! $points) {
3,444✔
45
            return;
748✔
46
        }
47

48
        CoordinateSystem::check($this, ...$points);
2,710✔
49

50
        $numPoints = count($points);
2,710✔
51

52
        if ($numPoints < 3) {
2,710✔
53
            throw new InvalidGeometryException('A CircularString must be made of at least 3 points.');
21✔
54
        }
55

56
        if ($numPoints % 2 === 0) {
2,689✔
57
            throw new InvalidGeometryException('A CircularString must have an odd number of points.');
14✔
58
        }
59

60
        $this->points = array_values($points);
2,675✔
61
    }
62

63
    /**
64
     * Creates a non-empty CircularString composed of the given points.
65
     *
66
     * @param Point    $point1 The first point.
67
     * @param Point ...$pointN The subsequent points.
68
     *
69
     * @throws InvalidGeometryException  If the number of points is invalid.
70
     * @throws CoordinateSystemException If the points use different coordinate systems.
71
     */
72
    public static function of(Point $point1, Point ...$pointN) : CircularString
73
    {
74
        return new CircularString($point1->coordinateSystem(), $point1, ...$pointN);
×
75
    }
76

77
    #[Override]
78
    public function startPoint() : Point
79
    {
80
        if (count($this->points) === 0) {
945✔
81
            throw new EmptyGeometryException('The CircularString is empty and has no start point.');
56✔
82
        }
83

84
        return $this->points[0];
889✔
85
    }
86

87
    #[Override]
88
    public function endPoint() : Point
89
    {
90
        $count = count($this->points);
609✔
91

92
        if ($count === 0) {
609✔
93
            throw new EmptyGeometryException('The CircularString is empty and has no end point.');
56✔
94
        }
95

96
        return $this->points[$count - 1];
553✔
97
    }
98

99
    /**
100
     * Returns the number of Points in this CircularString.
101
     */
102
    public function numPoints() : int
103
    {
104
        return count($this->points);
56✔
105
    }
106

107
    /**
108
     * Returns the specified Point N in this CircularString.
109
     *
110
     * @param int $n The point number, 1-based.
111
     *
112
     * @throws NoSuchGeometryException If there is no Point at this index.
113
     */
114
    public function pointN(int $n) : Point
115
    {
116
        if (! isset($this->points[$n - 1])) {
133✔
117
            throw new NoSuchGeometryException('There is no Point in this CircularString at index ' . $n);
42✔
118
        }
119

120
        return $this->points[$n - 1];
91✔
121
    }
122

123
    /**
124
     * Returns the points that compose this CircularString.
125
     *
126
     * @return list<Point>
127
     */
128
    public function points() : array
129
    {
130
        return $this->points;
×
131
    }
132

133
    #[NoProxy, Override]
134
    public function geometryType() : string
135
    {
136
        return 'CircularString';
1,476✔
137
    }
138

139
    #[NoProxy, Override]
140
    public function geometryTypeBinary() : int
141
    {
142
        return Geometry::CIRCULARSTRING;
743✔
143
    }
144

145
    #[Override]
146
    public function getBoundingBox() : BoundingBox
147
    {
NEW
148
        return array_reduce(
×
NEW
149
            $this->points,
×
NEW
150
            fn (BoundingBox $boundingBox, Point $point) => $boundingBox->extendedWithPoint($point),
×
NEW
151
            BoundingBox::new(),
×
NEW
152
        );
×
153
    }
154

155
    /**
156
     * @return list<list<float>>
157
     */
158
    #[Override]
159
    public function toArray() : array
160
    {
161
        return array_map(
448✔
162
            fn (Point $point) => $point->toArray(),
448✔
163
            $this->points,
448✔
164
        );
448✔
165
    }
166

167
    #[Override]
168
    public function project(Projector $projector): CircularString
169
    {
170
        return new CircularString(
×
171
            $projector->getTargetCoordinateSystem($this->coordinateSystem),
×
172
            ...array_map(
×
173
                fn (Point $point) => $point->project($projector),
×
174
                $this->points,
×
UNCOV
175
            ),
×
UNCOV
176
        );
×
177
    }
178

179
    /**
180
     * Returns the number of points in this CircularString.
181
     */
182
    #[Override]
183
    public function count() : int
184
    {
185
        return count($this->points);
750✔
186
    }
187

188
    /**
189
     * Returns an iterator for the points in this CircularString.
190
     *
191
     * @return ArrayIterator<int<0, max>, Point>
192
     */
193
    #[Override]
194
    public function getIterator() : ArrayIterator
195
    {
196
        return new ArrayIterator($this->points);
1,912✔
197
    }
198

199
    /**
200
     * Returns a copy of this CircularString, with the given points added.
201
     */
202
    public function withAddedPoints(Point ...$points): CircularString
203
    {
204
        return new CircularString($this->coordinateSystem, ...$this->points, ...$points);
42✔
205
    }
206
}
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