LabelWrapper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 42
rs 10
wmc 7
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 5
A labels() 0 16 2
1
<?php
2
3
namespace Aptarus\PromClient;
4
5
class LabelWrapper
6
{
7
    public function __construct($metric_class, $var, $help, $labels)
8
    {
9
        if (!preg_match('/^[a-zA-Z_:][a-zA-Z0-9_:]*$/', $var)) {
10
            throw new Exceptions\InvalidName(sprintf(
11
                "Metric name '%s' is invalid",
12
                $var
13
            ));
14
        }
15
        foreach ($labels as $label) {
16
            if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $label)
17
                or substr($label, 0, 4) === "__") {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
18
                throw new Exceptions\InvalidName(sprintf(
19
                    "Label name '%s' is invalid",
20
                    $label
21
                ));
22
            }
23
        }
24
        $this->metric_class = "Aptarus\\PromClient\\" . $metric_class;
0 ignored issues
show
Bug introduced by
The property metric_class 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...
25
        $this->var = $var;
0 ignored issues
show
Bug introduced by
The property var 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...
26
        $this->help = $help;
0 ignored issues
show
Bug introduced by
The property help 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...
27
        $this->labels = $labels;
0 ignored issues
show
Bug introduced by
The property labels 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...
28
    }
29
30
    public function labels($label_values)
31
    {
32
        if (count($this->labels) != count($label_values)) {
33
            throw new Exceptions\LabelValueMismatch(sprintf(
34
                "labels/value counts don't match (%d/%d)",
35
                count($this->labels),
36
                count($label_values)
37
            ));
38
        }
39
        return new $this->metric_class(
40
            $this->var,
41
            $this->help,
42
            $this->labels,
43
            $label_values
44
        );
45
    }
46
}
47
48
// vim:sw=4 ts=4 et
49