Passed
Pull Request — master (#141)
by Jordan
09:08
created

PolarCoordinate::asCartesian()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Samsara\Fermat\Coordinates\Values;
4
5
use Samsara\Fermat\Core\Numbers;
6
use Samsara\Fermat\Coordinates\Types\Base\Interfaces\Coordinates\CoordinateInterface;
7
use Samsara\Fermat\Coordinates\Types\Base\Interfaces\Coordinates\TwoDCoordinateInterface;
8
use Samsara\Fermat\Coordinates\Types\Coordinate;
9
use Samsara\Fermat\Core\Values\ImmutableDecimal;
10
11
class PolarCoordinate extends Coordinate implements TwoDCoordinateInterface
12
{
13
    /** @var CartesianCoordinate */
14
    protected $cachedCartesian;
15
16 266
    public function __construct($rho, $theta)
17
    {
18 266
        $theta = Numbers::makeOrDont(Numbers::IMMUTABLE, $theta);
19
20 266
        $theta = $theta->continuousModulo(Numbers::makeTau($theta->getScale()));
0 ignored issues
show
Bug introduced by
The method continuousModulo() does not exist on Samsara\Fermat\Core\Type...Numbers\NumberInterface. It seems like you code against a sub-type of Samsara\Fermat\Core\Type...Numbers\NumberInterface such as Samsara\Fermat\Core\Types\Decimal or Samsara\Fermat\Core\Type...umbers\DecimalInterface. ( 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::makeTau($theta->getScale()));
Loading history...
Bug introduced by
It seems like $theta->getScale() can also be of type integer; however, parameter $scale of Samsara\Fermat\Core\Numbers::makeTau() 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

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