Completed
Push — master ( fb3e52...428089 )
by ARCANEDEV
07:37
created

Calculatable::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php namespace Arcanedev\Units\Traits;
2
3
use Arcanedev\Units\Contracts\UnitMeasure;
4
5
/**
6
 * Trait     Calculatable
7
 *
8
 * @package  Arcanedev\Units\Traits
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
trait Calculatable
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Main Functions
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Add the unit instance.
19
     *
20
     * @param  \Arcanedev\Units\Contracts\UnitMeasure  $unit
21
     *
22
     * @return \Arcanedev\Units\Contracts\UnitMeasure
23
     */
24
    public function add(UnitMeasure $unit)
25
    {
26
        return $this->setValue(
0 ignored issues
show
Bug introduced by
It seems like setValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
            static::calculate(
28
                $this->value(), '+', $unit->to($this->unit())->value()
0 ignored issues
show
Bug introduced by
It seems like value() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like unit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
            )
30
        );
31
    }
32
33
    /**
34
     * Sub the unit instance.
35
     *
36
     * @param  \Arcanedev\Units\Contracts\UnitMeasure  $unit
37
     *
38
     * @return \Arcanedev\Units\Contracts\UnitMeasure
39
     */
40
    public function sub(UnitMeasure $unit)
41
    {
42
        return $this->setValue(
0 ignored issues
show
Bug introduced by
It seems like setValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
43
            static::calculate(
44
                $this->value(), '-', $unit->to($this->unit())->value()
0 ignored issues
show
Bug introduced by
It seems like value() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like unit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
            )
46
        );
47
    }
48
49
    /**
50
     * Multiply unit by the given number.
51
     *
52
     * @param  float|int  $number
53
     *
54
     * @return \Arcanedev\Units\Contracts\UnitMeasure
55
     */
56
    public function multiply($number)
57
    {
58
        return $this->setValue(
0 ignored issues
show
Bug introduced by
It seems like setValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
            static::calculate($this->value(), 'x', $number)
0 ignored issues
show
Bug introduced by
It seems like value() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
        );
61
    }
62
63
    /**
64
     * Divide unit by the given number.
65
     *
66
     * @param  float|int  $number
67
     *
68
     * @return \Arcanedev\Units\Contracts\Weight
69
     */
70
    public function divide($number)
71
    {
72
        return $this->setValue(
0 ignored issues
show
Bug introduced by
It seems like setValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
73
            static::calculate($this->value(), '/', $number)
0 ignored issues
show
Bug introduced by
It seems like value() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
        );
75
    }
76
77
    /**
78
     * Calculate the value.
79
     *
80
     * @param  float|int  $a
81
     * @param  string     $operator
82
     * @param  float|int  $b
83
     *
84
     * @return float|int
85
     */
86
    protected static function calculate($a, $operator, $b)
87
    {
88
        $operations = $operations = [
89
            '+' => function ($a, $b) { return $a + $b; },
90
            '-' => function ($a, $b) { return $a - $b; },
91
            'x' => function ($a, $b) { return $a * $b; },
92
            '*' => function ($a, $b) { return $a * $b; },
93
            '/' => function ($a, $b) { return $a / $b; },
94
            '^' => function ($a, $b) { return pow($a, $b); },
95 174
        ];
96
97 232
        return array_key_exists($operator, $operations)
98 230
            ? call_user_func_array($operations[$operator], [$a, $b])
99 216
            : $a;
100
    }
101
}
102