|
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(); |
|
|
|
|
|
|
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
|
|
|
|
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:
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: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: