Passed
Push — master ( d7dee9...483d68 )
by Jan
04:53 queued 10s
created

ParametersTrait::addParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
7
 *
8
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as published
12
 * by the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
namespace App\Entity\Parameters;
25
26
use Doctrine\Common\Collections\Collection;
27
use Symfony\Component\Validator\Constraints as Assert;
28
29
trait ParametersTrait
30
{
31
    /**
32
     * Mapping done in subclasses.
33
     *
34
     * @var AbstractParameter[]|Collection
35
     * @Assert\Valid()
36
     */
37
    protected $parameters;
38
39
    /**
40
     * Return all associated specifications.
41
     *
42
     * @return AbstractParameter[]|Collection
43
     */
44
    public function getParameters(): Collection
45
    {
46
        return $this->parameters;
47
    }
48
49
    /**
50
     * Add a new parameter information.
51
     *
52
     * @return $this
53
     */
54
    public function addParameter(AbstractParameter $parameter): self
55
    {
56
        $parameter->setElement($this);
0 ignored issues
show
Bug introduced by
$this of type App\Entity\Parameters\ParametersTrait is incompatible with the type App\Entity\Base\AbstractDBElement expected by parameter $element of App\Entity\Parameters\Ab...Parameter::setElement(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $parameter->setElement(/** @scrutinizer ignore-type */ $this);
Loading history...
57
        $this->parameters->add($parameter);
58
59
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type App\Entity\Base\AbstractDBElement which is incompatible with the type-hinted return App\Entity\Parameters\ParametersTrait.
Loading history...
60
    }
61
62
    public function removeParameter(AbstractParameter $parameter): self
63
    {
64
        $this->parameters->removeElement($parameter);
65
66
        return $this;
67
    }
68
69
    public function getGroupedParameters(): array
70
    {
71
        $tmp = [];
72
73
        foreach ($this->parameters as $parameter) {
74
            $tmp[$parameter->getGroup()][] = $parameter;
75
        }
76
        return $tmp;
77
    }
78
}
79