Emit   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 31 3
B Text() 0 30 5
1
<?php
2
3
namespace Aptarus\PromClient\Exposition;
4
5
use PDO;
6
use Aptarus\PromClient\Utility as U;
7
use Aptarus\PromClient\Configuration as Configuration;
8
9
class Emit
10
{
11
    public function __construct()
12
    {
13
        $metrics_db = U\PromClientOpenDB(Configuration::$storage_dir);
14
15
        // Clean up the DB first.
16
        $julian_day = unixtojd();
17
        $sth = $metrics_db->
18
            prepare('DELETE FROM metrics
19
                            WHERE var IN (SELECT var FROM meta
20
                                          WHERE ? - JULIANDAY(changed) > 3)');
21
        $sth->execute(array($julian_day));
22
        $meta_deleted = $sth->fetchColumn();
0 ignored issues
show
Unused Code introduced by
$meta_deleted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
        $sth = $metrics_db->prepare('DELETE FROM metrics
24
                                            WHERE ? - JULIANDAY(changed) > 3');
25
        $sth->execute(array($julian_day));
26
        $metics_deleted = $sth->fetchColumn();
0 ignored issues
show
Unused Code introduced by
$metics_deleted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
28
        $this->meta = $metrics_db
0 ignored issues
show
Bug introduced by
The property meta does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
            ->query('SELECT var, typ, help FROM meta ORDER BY var ASC')
30
            ->fetchAll(PDO::FETCH_UNIQUE);
31
32
        $sth = $metrics_db
33
            ->prepare('SELECT labels, value, label_values
34
                         FROM metrics where var = ? ORDER BY var ASC');
35
36
        foreach (array_keys($this->meta) as $var) {
37
            if ($sth->execute(array($var))) {
38
                $this->metrics[$var] = $sth->fetchAll(PDO::FETCH_UNIQUE);
0 ignored issues
show
Bug introduced by
The property metrics does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
            }
40
        }
41
    }
42
43
    // @codingStandardsIgnoreStart
44
    public function Text()
45
    {
46
        // @codingStandardsIgnoreEnd
47
        // Spew metrics.
48
        header('Content-Type: text/plain; version=0.0.4');
49
50
        foreach ($this->meta as $var) {
51
            print("# HELP $var " + $this->meta[$var]['help'] + "\n");
52
            print("# TYPE $var " + $this->meta[$var]['typ'] + "\n");
53
            foreach ($this->metrics[$var] as $labels) {
54
                $labels_quoted = '';
0 ignored issues
show
Unused Code introduced by
$labels_quoted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
                $lnames = unserialize($labels);
56
                if ($lnames) {
57
                    $lvalues = unserialize(
58
                        $this->metrics[$var][$labels]['label_values']
59
                    );
60
                    $lnv = $array_combine($lnames, $lvalues);
0 ignored issues
show
Bug introduced by
The variable $array_combine does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
61
                    $lnv_quoted = array();
62
                    foreach ($lnv as $l) {
63
                        $lnv_quoted[] = $l + '="' + $lvalues[$l] + '"';
64
                    }
65
                    $labels_quoted = '{' + join(',', $lnv_quoted) + '}';
0 ignored issues
show
Unused Code introduced by
$labels_quoted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
                }
67
                print(
68
                    "$var$plabels_quoted "
0 ignored issues
show
Bug introduced by
The variable $plabels_quoted does not exist. Did you mean $labels?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
69
                    + $this->metrics[$var][$labels]['value'] + "\n"
70
                );
71
            }
72
        }
73
    }
74
}
75
76
// vim:sw=4 ts=4 et
77