Issues (44)

src/Geometry/Curve.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace geoPHP\Geometry;
4
5
use geoPHP\Exception\InvalidGeometryException;
6
7
/**
8
 * A curve consists of a sequence of Points.
9
 *
10
 * @package GeoPHPGeometry
11
 */
12
abstract class Curve extends Collection
13
{
14
15
    /**
16
     * @var Point
17
     */
18
    protected $startPoint;
19
20
    /**
21
     * @var Point
22
     */
23
    protected $endPoint;
24
25
    public function __construct(
26
        array $components = [],
27
        bool $allowEmptyComponents = false,
28
        string $allowedComponentType = Point::class
29
    ) {
30
        parent::__construct($components, $allowEmptyComponents, $allowedComponentType);
31
32
        if (count($this->components) === 1) {
33
            throw new InvalidGeometryException("Cannot construct a " . static::class . " with a single point");
34
        }
35
    }
36
37
    /**
38
     * @return string "Curve"
39
     */
40
    public function geometryType(): string
41
    {
42
        return Geometry::CURVE;
43
    }
44
45
    /**
46
     * @return int 1
47
     */
48
    public function dimension(): int
49
    {
50
        return 1;
51
    }
52
53
    /**
54
     * @return Point
55
     */
56
    public function startPoint()
57
    {
58
        if (!isset($this->startPoint)) {
59
            $this->startPoint = $this->pointN(1);
0 ignored issues
show
Are you sure the assignment to $this->startPoint is correct as $this->pointN(1) targeting geoPHP\Geometry\Geometry::pointN() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
        }
61
        return $this->startPoint;
62
    }
63
64
    /**
65
     * @return Point
66
     */
67
    public function endPoint()
68
    {
69
        if (!isset($this->endPoint)) {
70
            $this->endPoint = $this->pointN($this->numPoints());
0 ignored issues
show
Are you sure the assignment to $this->endPoint is correct as $this->pointN($this->numPoints()) targeting geoPHP\Geometry\Geometry::pointN() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
        }
72
        return $this->endPoint;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function isClosed(): bool
79
    {
80
        return $this->startPoint()->equals($this->endPoint());
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    public function isRing(): bool
87
    {
88
        return ($this->isClosed() && $this->isSimple());
89
    }
90
    
91
    /**
92
     * Tests whether this geometry is simple.
93
     * According to the OGR specification a ring is only simple if its start and end point are equal (in all values).
94
     * Currently, neither GEOS, nor PostGIS support it.
95
     *
96
     * @return bool
97
     */
98
    public function isSimple(): bool
99
    {
100
        return $this->startPoint()->equals($this->endPoint());
101
    }
102
    
103
    /**
104
     * @return bool
105
     */
106
    public function isValid(): bool
107
    {
108
        $geosObj = $this->getGeos();
109
        if (is_object($geosObj)) {
110
            /** @noinspection PhpUndefinedMethodInspection */
111
            return $geosObj->checkValidity()['valid'];
112
        }
113
        return $this->isSimple();
114
    }
115
    
116
    /**
117
     * The boundary of a non-closed Curve consists of its end Points.
118
     *
119
     * @return LineString|MultiPoint
120
     */
121
    public function boundary(): Geometry
122
    {
123
        return $this->isEmpty() ?
124
            new LineString() :
125
            ($this->isClosed() ?
126
                new MultiPoint() :
127
                new MultiPoint([$this->startPoint() ?? new Point(), $this->endPoint() ?? new Point()])
128
            );
129
    }
130
131
    /**
132
     * Not valid for this geometry type
133
     *
134
     * @return float 0.0
135
     */
136
    public function getArea(): float
137
    {
138
        return 0.0;
139
    }
140
}
141