Passed
Push — master ( 372311...b093ab )
by Jordan
02:26
created

PolarCoordinate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 70.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 53
ccs 17
cts 24
cp 0.7083
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A distanceTo() 0 3 1
A asPolar() 0 3 1
A getDistanceFromOrigin() 0 3 1
A __construct() 0 16 2
A getPolarAngle() 0 3 1
A asCartesian() 0 10 2
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 4
    public function __construct($rho, $theta)
17
    {
18 4
        $theta = Numbers::makeOrDont(Numbers::IMMUTABLE, $theta);
19
20 4
        $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 4
        if ($theta->isNegative()) {
23
            $theta = Numbers::makeTau($theta->getScale())->add($theta);
24
        }
25
26 4
        $data = [
27 4
            'rho' => $rho,
28 4
            'theta' => $theta
29
        ];
30
31 4
        parent::__construct($data);
32 4
    }
33
34
    public function getDistanceFromOrigin(): ImmutableDecimal
35
    {
36
        return $this->getAxis('rho');
37
    }
38
39
    public function distanceTo(CoordinateInterface $coordinate): ImmutableDecimal
40
    {
41
        return $this->asCartesian()->distanceTo($coordinate);
42
    }
43
44 1
    public function asCartesian(): CartesianCoordinate
45
    {
46 1
        if (is_null($this->cachedCartesian)) {
47 1
            $x = $this->getAxis('rho')->multiply($this->getAxis('theta')->cos());
48 1
            $y = $this->getAxis('rho')->multiply($this->getAxis('theta')->sin());
49
50 1
            $this->cachedCartesian = new CartesianCoordinate($x, $y);
0 ignored issues
show
Bug introduced by
It seems like $y can also be of type Samsara\Fermat\Values\ImmutableDecimal; however, parameter $y of Samsara\Fermat\Values\Ge...ordinate::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

50
            $this->cachedCartesian = new CartesianCoordinate($x, /** @scrutinizer ignore-type */ $y);
Loading history...
51
        }
52
53 1
        return $this->cachedCartesian;
54
    }
55
56 2
    public function getPolarAngle(): ImmutableDecimal
57
    {
58 2
        return $this->getAxis('theta');
59
    }
60
61
    public function asPolar(): PolarCoordinate
62
    {
63
        return $this;
64
    }
65
66
}