Completed
Push — master ( faf894...f88301 )
by Alexey
05:18
created

Sums   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 70
Duplicated Lines 31.43 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 18
c 1
b 0
f 1
lcom 1
cbo 3
dl 22
loc 70
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A plus() 8 8 3
A minus() 8 8 3
A greater() 3 6 3
A equal() 3 6 3
B __toString() 0 26 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Money sums comparator
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2016 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Money;
13
14
class Sums extends \Object
15
{
16
    public $sums = [];
17
18
    public function __construct($sums)
19
    {
20
        $this->sums = $sums;
21
    }
22
23 View Code Duplication
    function plus(Sums $sums)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
    {
25
        $newSums = $this->sums;
26
        foreach ($sums->sums as $currency_id => $sum) {
27
            $newSums[$currency_id] = isset($newSums[$currency_id]) ? $newSums[$currency_id] + $sum : $sum;
28
        }
29
        return new Sums($newSums);
30
    }
31
32 View Code Duplication
    function minus(Sums $sums)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
    {
34
        $newSums = $this->sums;
35
        foreach ($sums->sums as $currency_id => $sum) {
36
            $newSums[$currency_id] = isset($newSums[$currency_id]) ? $newSums[$currency_id] - $sum : -$sum;
37
        }
38
        return new Sums($newSums);
39
    }
40
41
//Equal, Less and Greater
42
    public function greater(Sums $sums)
43
    {
44 View Code Duplication
        if (count($this->sums) == count($sums->sums) && isset($sums->sums[key($this->sums)])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
            return current($this->sums) > current($sums->sums);
46
        }
47
    }
48
49
    public function equal(Sums $sums)
50
    {
51 View Code Duplication
        if (count($this->sums) == count($sums->sums) && isset($sums->sums[key($this->sums)])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            return current($this->sums) == current($sums->sums);
53
        }
54
    }
55
56
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
    {
58
        $string = '';
59
        $first = true;
60
        foreach ($this->sums as $currency_id => $sum) {
61
            if ($first) {
62
                $first = false;
63
            } else {
64
                $string.= '<br />';
65
            }
66
            $string.= '<span style="white-space:nowrap;">';
67
            $string.= number_format($sum, 2, '.', ' ');
68
            if (\App::$cur->money) {
69
                $currency = \Money\Currency::get($currency_id);
70
                if ($currency) {
71
                    $string.= ' ' . $currency->acronym();
72
                } else {
73
                    $string.= ' руб.';
74
                }
75
            } else {
76
                $string.= ' руб.';
77
            }
78
            $string.= '</span>';
79
        }
80
        return $string;
81
    }
82
83
}
84