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

brick / geo / 13715715561

06 Mar 2025 10:47PM UTC coverage: 44.086% (-40.4%) from 84.507%
13715715561

push

github

BenMorel
Remove Psalm-specific annotations

1543 of 3500 relevant lines covered (44.09%)

270.91 hits per line

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

69.77
/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
 */
24
class CircularString extends Curve implements \Countable, \IteratorAggregate
25
{
26
    /**
27
     * The Points that compose this CircularString.
28
     *
29
     * An empty CircularString contains no points.
30
     *
31
     * @var list<Point>
32
     */
33
    protected array $points = [];
34

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

43
        if (! $points) {
978✔
44
            return;
216✔
45
        }
46

47
        CoordinateSystem::check($this, ...$points);
766✔
48

49
        $numPoints = count($points);
766✔
50

51
        if ($numPoints < 3) {
766✔
52
            throw new InvalidGeometryException('A CircularString must be made of at least 3 points.');
6✔
53
        }
54

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

59
        $this->points = array_values($points);
756✔
60
    }
61

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

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

83
        return $this->points[0];
248✔
84
    }
85

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

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

95
        return $this->points[$count - 1];
156✔
96
    }
97

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

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

119
        return $this->points[$n - 1];
26✔
120
    }
121

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

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

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

144
    #[Override]
145
    public function getBoundingBox() : BoundingBox
146
    {
147
        $boundingBox = BoundingBox::new();
×
148

149
        foreach ($this->points as $point) {
×
150
            $boundingBox = $boundingBox->extendedWithPoint($point);
×
151
        }
152

153
        return $boundingBox;
×
154
    }
155

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

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

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

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

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