TraitModel   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAttributes() 0 3 1
A hasAttribute() 0 4 2
A getAttribute() 0 8 3
A addAttribute() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of PhpUnitGen.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use PhpUnitGen\Model\ModelInterface\AttributeModelInterface;
17
use PhpUnitGen\Model\ModelInterface\TraitModelInterface;
18
19
/**
20
 * Class TraitModel.
21
 *
22
 * @author     Paul Thébaud <[email protected]>.
23
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
24
 * @license    https://opensource.org/licenses/MIT The MIT license.
25
 * @link       https://github.com/paul-thebaud/phpunit-generator
26
 * @since      Class available since Release 2.0.0.
27
 */
28
class TraitModel extends InterfaceModel implements TraitModelInterface
29
{
30
    /**
31
     * @var AttributeModelInterface[]|Collection $attributes Class attributes.
32
     */
33
    private $attributes;
34
35
    /**
36
     * TraitModel constructor.
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->attributes = new ArrayCollection();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function addAttribute(AttributeModelInterface $attribute): void
48
    {
49
        $this->attributes->add($attribute);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getAttributes(): Collection
56
    {
57
        return $this->attributes;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function hasAttribute(string $name, bool $static = false): bool
64
    {
65
        return $this->attributes->exists(function (int $key, AttributeModelInterface $attribute) use ($name, $static) {
66
            return $attribute->getName() === $name && $attribute->isStatic() === $static;
67
        });
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getAttribute(string $name): ?AttributeModelInterface
74
    {
75
        foreach ($this->attributes as $attribute) {
76
            if ($attribute->getName() === $name) {
77
                return $attribute;
78
            }
79
        }
80
        return null;
81
    }
82
}
83