ComplexNumbers::make()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
dl 0
loc 25
ccs 0
cts 13
cp 0
crap 90
rs 8.0555
c 1
b 0
f 0
eloc 12
nc 7
nop 2
1
<?php
2
3
namespace Samsara\Fermat\Complex;
4
5
use Samsara\Exceptions\UsageError\IntegrityConstraint;
6
use Samsara\Fermat\Complex\Types\ComplexNumber;
7
use Samsara\Fermat\Core\Types\Base\Interfaces\Groups\NumberCollectionInterface;
8
use Samsara\Fermat\Core\Types\Base\Interfaces\Numbers\SimpleNumberInterface;
9
use Samsara\Fermat\Complex\Values\ImmutableComplexNumber;
10
use Samsara\Fermat\Complex\Values\MutableComplexNumber;
0 ignored issues
show
Bug introduced by
The type Samsara\Fermat\Complex\Values\MutableComplexNumber was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
class ComplexNumbers
13
{
14
15
    public const IMMUTABLE_COMPLEX = ImmutableComplexNumber::class;
16
    public const MUTABLE_COMPLEX = MutableComplexNumber::class;
17
18
    /**
19
     * @param $type
20
     * @param $value
21
     *
22
     * @return ComplexNumber
23
     * @throws IntegrityConstraint
24
     */
25
    public static function make($type, $value)
26
    {
27
28
        if (is_string($value)) {
29
            if ($type === self::MUTABLE_COMPLEX) {
30
                return MutableComplexNumber::makeFromString($value);
31
            }
32
33
            return ImmutableComplexNumber::makeFromString($value);
34
        }
35
36
        if ($value instanceof NumberCollectionInterface && $value->count() === 2) {
37
            if ($type === self::MUTABLE_COMPLEX) {
38
                return new MutableComplexNumber($value->get(0), $value->get(1));
39
            }
40
41
            return new ImmutableComplexNumber($value->get(0), $value->get(1));
42
        }
43
44
        if (is_array($value) && count($value) === 2) {
45
            if ($type === self::MUTABLE_COMPLEX) {
46
                return new MutableComplexNumber($value[0], $value[1]);
47
            }
48
49
            return new ImmutableComplexNumber($value[0], $value[1]);
50
        }
51
52
    }
53
54
}