Completed
Pull Request — master (#36)
by Daniel
09:34 queued 06:14
created

ObjectView::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Psi\Component\ContentType\Standard\View;
4
5
use Psi\Component\ContentType\View\ViewInterface;
6
7
class ObjectView implements ViewInterface, \ArrayAccess, \Iterator
8
{
9
    private $template;
10
    private $viewClosures;
11
12
    public function __construct(string $template, array $viewClosures)
13
    {
14
        $this->template = $template;
15
        $this->viewClosures = $viewClosures;
16
    }
17
18 View Code Duplication
    public function offsetGet($name)
0 ignored issues
show
Duplication introduced by
This method 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...
19
    {
20
        if (!isset($this->viewClosures[$name])) {
21
            throw new \InvalidArgumentException(sprintf(
22
                'Child view "%s" has not been set, known children: "%s"',
23
                $name, implode('", "', array_keys($this->viewClosures))
24
            ));
25
        }
26
27
        return $this->viewClosures[$name]();
28
    }
29
30
    public function offsetExists($name)
31
    {
32
        return isset($this->children[$name]);
0 ignored issues
show
Bug introduced by
The property children 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...
33
    }
34
35
    public function offsetSet($name, $value)
36
    {
37
        $this->children[$name] = $value;
38
    }
39
40
    public function offsetUnset($name)
41
    {
42
        unset($this->children[$name]);
43
    }
44
45
    public function current()
46
    {
47
        return $this->offsetGet(key($this->viewClosures));
48
    }
49
50
    public function key()
51
    {
52
        return key($this->viewClosures);
53
    }
54
55
    public function next()
56
    {
57
        return next($this->viewClosures);
58
    }
59
60
    public function rewind()
61
    {
62
        return reset($this->viewClosures);
63
    }
64
65
    public function valid()
66
    {
67
        return key($this->viewClosures) !== null;
68
    }
69
70
    public function getTemplate(): string
71
    {
72
        return $this->template;
73
    }
74
}
75