MetricManagerTrait::saveAndReturnArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Tracking\Traits;
3
4
use Tracking\Abstracts\MetricManager;
5
6
use Stalker\Models\File;
7
use Stalker\Models\Digital\Internet\ComputerFile;
8
9
/**
10
 * Outputs events information to the console.
11
 *
12
 * @see TriggerableInterface
13
 */
14
trait MetricManagerTrait
15
{
16
17
    public $metrics = [];
18
19
    /**
20
     * Lógica
21
     */
22
    protected function run()
23
    {
24
        return true;
25
    }
26
27
    /**
28
     * Type pode ser: Extensions, Identificadores, Groups
29
     */
30
    public function registerMetricCount($type, $group, $sum = 1)
31
    {
32
        if (!isset($this->metrics[$type])) {
33
            $this->metrics[$type] = [];
34
        }
35
36
        if (!isset($this->metrics[$type][$group])) {
37
            $this->metrics[$type][$group] = 0;
38
        }
39
40
        return $this->metrics[$type][$group] += $sum;
41
    }
42
    public function mergeWith($mergeWith)
43
    {
44
        $this->metrics = $this->arrayMergeRecursive($this->metrics, $mergeWith);
45
    }
46
47
    public function saveAndReturnArray()
48
    {
49
        $this->save();
0 ignored issues
show
Unused Code introduced by
The call to the method Tracking\Traits\MetricManagerTrait::save() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
50
        return $this->returnMetrics();
51
    }
52
53
    public function returnMetrics()
54
    {
55
        return $this->metrics;
56
    }
57
58
    protected function save()
59
    {
60
        return $this->metrics;
61
    }
62
63
64
    protected function arrayMergeRecursive($array1, $array2)
65
    {
66
        if (empty($array2)) {
67
            return $array1;
68
        }
69
        if (empty($array1)) {
70
            return $array2;
71
        }
72
73
        foreach ($array2 as $indice => $valor) {
74
            if (!isset($array1[$indice])) {
75
                $array1[$indice] = $array2[$indice];
76
            } else {
77
                foreach ($array2[$indice] as $indice2 => $valor2) {
78
                    if (!isset($array1[$indice][$indice2])) {
79
                        $array1[$indice][$indice2] = $array2[$indice][$indice2];
80
                    } else{
81
                        $array1[$indice][$indice2] += $array2[$indice][$indice2];
82
                    }
83
                }
84
            }
85
        }
86
87
        return $array1;
88
    }
89
}
90