MetaProvider   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 72
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __call() 0 9 5
A getNumericValue() 0 6 2
A getMessages() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ParaTest\Logging;
6
7
/**
8
 * Class MetaProvider.
9
 *
10
 * Adds __call behavior to a logging object
11
 * for aggregating totals and messages
12
 */
13
abstract class MetaProvider
14
{
15
    /**
16
     * This pattern is used to see whether a missing
17
     * method is a "total" method or not.
18
     *
19
     * @var string
20
     */
21
    protected static $totalMethod = '/^getTotal([\w]+)$/';
22
23
    /**
24
     * This pattern is used to add message retrieval for a given
25
     * type - i.e getFailures() or getErrors().
26
     *
27
     * @var string
28
     */
29
    protected static $messageMethod = '/^get((Failure|Error)s)$/';
30
31
    /**
32
     * Simplify aggregation of totals or messages.
33
     *
34
     * @param mixed $method
35
     * @param mixed $args
36
     */
37 22
    public function __call(string $method, array $args)
38
    {
39 22
        if (preg_match(self::$totalMethod, $method, $matches) && $property = strtolower($matches[1])) {
40 14
            return $this->getNumericValue($property);
41
        }
42 10
        if (preg_match(self::$messageMethod, $method, $matches) && $type = strtolower($matches[1])) {
43 10
            return $this->getMessages($type);
44
        }
45
    }
46
47
    /**
48
     * Return a value as a float or integer.
49
     *
50
     * @param $property
51
     *
52
     * @return float|int
53
     */
54 14
    protected function getNumericValue(string $property)
55
    {
56 14
        return ($property === 'time')
57 2
            ? (float) ($this->suites[0]->$property)
0 ignored issues
show
Bug introduced by
The property suites 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...
58 14
            : (int) ($this->suites[0]->$property);
59
    }
60
61
    /**
62
     * Return messages for a given type.
63
     *
64
     * @param $type
65
     *
66
     * @return array
67
     */
68 10
    protected function getMessages(string $type): array
69
    {
70 10
        $messages = [];
71 10
        $suites = $this->isSingle ? $this->suites : $this->suites[0]->suites;
0 ignored issues
show
Bug introduced by
The property isSingle 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...
72 10
        foreach ($suites as $suite) {
73
            $messages = array_merge($messages, array_reduce($suite->cases, function ($result, $case) use ($type) {
74 10
                return array_merge($result, array_reduce($case->$type, function ($msgs, $msg) {
75 10
                    $msgs[] = $msg['text'];
76
77 10
                    return $msgs;
78 10
                }, []));
79 10
            }, []));
80
        }
81
82 10
        return $messages;
83
    }
84
}
85