CpuUsage   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 4.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 7
c 5
b 1
f 0
lcom 1
cbo 1
dl 3
loc 61
ccs 16
cts 18
cp 0.8889
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUsage() 0 13 2
A resolve() 0 4 1
A validate() 3 6 2
A getPath() 0 4 1

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
 * Created by PhpStorm.
4
 * User: arnaud
5
 * Date: 01/11/15
6
 * Time: 20:43
7
 */
8
9
namespace Ndrx\Profiler\Collectors\Data;
10
11
use Ndrx\Profiler\Collectors\Collector;
12
use Ndrx\Profiler\Collectors\Contracts\FinalCollectorInterface;
13
use Ndrx\Profiler\DataSources\Contracts\DataSourceInterface;
14
use Ndrx\Profiler\JsonPatch;
15
use Ndrx\Profiler\Process;
16
use Ndrx\Profiler\Renderer\BarRenderableInterface;
17
18
class CpuUsage extends Collector implements FinalCollectorInterface, BarRenderableInterface
19
{
20
    protected $initialCpuUsage;
21
    /**
22
     * EventsDataSource constructor.
23
     */
24 96
    public function __construct(Process $process, DataSourceInterface $dataSource, JsonPatch $jsonPatch = null)
25
    {
26 96
        $this->initialCpuUsage = $this->getUsage();
27
28 96
        parent::__construct($process, $dataSource, $jsonPatch);
29 96
    }
30
31 104
    protected function getUsage()
32
    {
33
        /*
34
         * getrusage is not available on windows with php < 7
35
         */
36 104
        if (!function_exists('getrusage')) {
37
            return 0;
38
        }
39
40 104
        $cpu =  getrusage();
41
42 104
        return $cpu["ru_utime.tv_sec"] * 1e6 + $cpu["ru_utime.tv_usec"];
43
    }
44
45
    /**
46
     * Fetch data
47
     * @return mixed
48
     */
49 94
    public function resolve()
50
    {
51 94
        $this->data =  $this->getUsage() - $this->initialCpuUsage;
52 94
    }
53
54
55 92
    public function validate()
56
    {
57 92 View Code Duplication
        if (!is_numeric($this->data)) {
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...
58
            throw new \LogicException('Duration must be a numeric ' . json_encode($this->data) . ' given');
59
        }
60 92
    }
61
62
    /**
63
     * The path in the final json
64
     * @example
65
     *  path /aa/bb
66
     *  will be transformed to
67
     *  {
68
     *     aa : {
69
     *              bb: <VALUE OF RESOLVE>
70
     *       }
71
     *  }
72
     * @return string
73
     */
74 102
    public function getPath()
75
    {
76 102
        return 'cpu';
77
    }
78
}
79