Completed
Push — master ( b40085...1d81aa )
by Tim
02:16
created

TestResult::addOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 10
loc 10
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor;
4
5 View Code Duplication
final class TestResult
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
6
{
7
    /**
8
     * @param array $output
9
     */
10
    public function __construct($output = array())
11
    {
12
        assert(is_array($output));
13
        $this->setInput($output);
0 ignored issues
show
Bug introduced by
The method setInput() does not seem to exist on object<SimpleSAML\Module\monitor\TestResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
14
    }
15
16
    /**
17
     * @param mixed|null $output
18
     * @param string|null $key
19
     *
20
     * @return void
21
     */
22
    public function setOutput($output, $key = null)
23
    {
24
        assert(is_string($key) || is_null($key));
25
        if (is_null($key)) {
26
            assert(is_array($output));
27
            foreach ($output as $key => $value) {
28
                $this->addOutput($key, $value);
29
            }
30
        } elseif (array_key_exists($key, $this->output)) {
0 ignored issues
show
Bug introduced by
The property output 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...
31
            $this->output[$key] = $output;
32
        } else {
33
            $this->addOutput($key, $output);
34
        }
35
    }
36
37
    /**
38
     * @param string $key
39
     * @param mixed|null $value
40
     *
41
     * @return void
42
     */
43
    public function addOutput($key, $value = null)
44
    {
45
        assert(is_string($key));
46
        if (isSet($this->output[$key])) {
47
            assert(is_array($this->output[$key]));
48
            $this->output[$key] = array_merge($this->output[$key], $value);
49
        } else {
50
            $this->output[$key] = $value;
51
        }
52
    }
53
54
    /**
55
     * @param string|null $item
56
     *
57
     * @return mixed
58
     */
59
    public function getOutput($item = null)
60
    {
61
        assert(is_string($item) || is_null($item));
62
        return is_null($item) ? $this->output : (isSet($this->output[$item]) ? $this->output[$item] : null);
63
    }
64
}
65