Completed
Pull Request — master (#34)
by Pol
06:14
created

AttributesAware::getAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @see      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\GraphViz;
15
16
trait AttributesAware
17
{
18
    /** @var Attribute[] */
19
    protected $attributes = [];
20
21
    /**
22
     * @param string $name
23
     *
24
     * @return \phpDocumentor\GraphViz\Attribute
25
     *
26
     * @throws AttributeNotFound
27
     */
28
    public function getAttribute(string $name): Attribute
29
    {
30
        if (!\array_key_exists($name, $this->attributes)) {
31
            throw new AttributeNotFound($name);
32
        }
33
34
        return $this->attributes[$name];
35
    }
36
37
    /**
38
     * @param string $name
39
     * @param string $value
40
     *
41
     * @return \phpDocumentor\GraphViz\AttributesAware
0 ignored issues
show
Comprehensibility Bug introduced by
The return type AttributesAware is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
42
     */
43
    public function setAttribute(string $name, string $value): self
44
    {
45
        $this->attributes[$name] = new Attribute($name, $value);
46
47
        return $this;
48
    }
49
}
50