Completed
Pull Request — master (#204)
by
unknown
02:07
created

MetaProvider::getNumericValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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