Failed Conditions
Push — dev ( b04f64...5f5cb9 )
by Jordan
03:11
created

PolarCoordinate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 56.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 53
ccs 13
cts 23
cp 0.5652
rs 10
c 1
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A getPolarAngle() 0 3 1
A distanceTo() 0 3 1
A asPolar() 0 3 1
A asCartesian() 0 10 2
A getDistanceFromOrigin() 0 3 1
1
<?php
2
3
namespace Samsara\Fermat\Values\Geometry\CoordinateSystems;
4
5
use Samsara\Fermat\Numbers;
6
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\CoordinateInterface;
7
use Samsara\Fermat\Types\Base\Interfaces\Coordinates\TwoDCoordinateInterface;
8
use Samsara\Fermat\Types\Coordinate;
9
use Samsara\Fermat\Values\ImmutableDecimal;
10
11
class PolarCoordinate extends Coordinate implements TwoDCoordinateInterface
12
{
13
    /** @var CartesianCoordinate */
14
    protected $cachedCartesian;
15
16 3
    public function __construct($rho, $theta)
17
    {
18 3
        $theta = Numbers::makeOrDont(Numbers::IMMUTABLE, $theta);
19
20 3
        $theta = $theta->continuousModulo(Numbers::TAU);
0 ignored issues
show
Bug introduced by
The method continuousModulo() does not exist on Samsara\Fermat\Types\Bas...Numbers\NumberInterface. It seems like you code against a sub-type of Samsara\Fermat\Types\Bas...Numbers\NumberInterface such as Samsara\Fermat\Types\Bas...umbers\DecimalInterface or Samsara\Fermat\Types\Decimal. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        /** @scrutinizer ignore-call */ 
21
        $theta = $theta->continuousModulo(Numbers::TAU);
Loading history...
21
22 3
        if ($theta->isNegative()) {
23 1
            $theta = Numbers::makeTau($theta->getPrecision())->add($theta);
24
        }
25
26
        $data = [
27 3
            'rho' => $rho,
28 3
            'theta' => $theta
29
        ];
30
31 3
        parent::__construct($data);
32 3
    }
33
34 3
    public function getDistanceFromOrigin(): ImmutableDecimal
35
    {
36 3
        return $this->getAxis('rho');
37
    }
38
39
    public function distanceTo(CoordinateInterface $coordinate): ImmutableDecimal
40
    {
41
        return $this->asCartesian()->distanceTo($coordinate);
42
    }
43
44
    public function asCartesian(): CartesianCoordinate
45
    {
46
        if (is_null($this->cachedCartesian)) {
47
            $x = $this->getAxis('rho')->multiply($this->getAxis('theta')->cos());
48
            $y = $this->getAxis('rho')->multiply($this->getAxis('theta')->sin());
49
50
            $this->cachedCartesian = new CartesianCoordinate($x, $y);
51
        }
52
53
        return $this->cachedCartesian;
54
    }
55
56 3
    public function getPolarAngle(): ImmutableDecimal
57
    {
58 3
        return $this->getAxis('theta');
59
    }
60
61
    public function asPolar(): PolarCoordinate
62
    {
63
        return $this;
64
    }
65
66
}