AbstractNamedDBElement::setName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace App\Entity\Base;
24
25
use App\Entity\Contracts\NamedElementInterface;
26
use App\Entity\Contracts\TimeStampableInterface;
27
use Doctrine\ORM\Mapping as ORM;
28
use Symfony\Component\Serializer\Annotation\Groups;
29
use Symfony\Component\Validator\Constraints as Assert;
30
31
/**
32
 * All subclasses of this class have an attribute "name".
33
 *
34
 * @ORM\MappedSuperclass(repositoryClass="App\Repository\NamedDBElement")
35
 * @ORM\HasLifecycleCallbacks()
36
 */
37
abstract class AbstractNamedDBElement extends AbstractDBElement implements NamedElementInterface, TimeStampableInterface
38
{
39
    use TimestampTrait;
40
41
    /**
42
     * @var string the name of this element
43
     * @ORM\Column(type="string")
44
     * @Assert\NotBlank()
45
     * @Groups({"simple", "extended", "full"})
46
     */
47
    protected string $name = '';
48
49
    /******************************************************************************
50
     *
51
     * Helpers
52
     *
53
     ******************************************************************************/
54
55
    public function __toString()
56
    {
57
        return $this->getName();
58
    }
59
60
    public function __clone()
61
    {
62
        if ($this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
63
            //We create a new object, so give it a new creation date
64
            $this->addedDate = null;
65
        }
66
        parent::__clone(); // TODO: Change the autogenerated stub
67
    }
68
69
    /********************************************************************************
70
     *
71
     *   Getters
72
     *
73
     *********************************************************************************/
74
75
    /**
76
     * Get the name of this element.
77
     *
78
     * @return string the name of this element
79
     */
80
    public function getName(): string
81
    {
82
        return $this->name;
83
    }
84
85
    /********************************************************************************
86
     *
87
     *   Setters
88
     *
89
     *********************************************************************************/
90
91
    /**
92
     * Change the name of this element.
93
     *
94
     * @param string $new_name the new name
95
     */
96
    public function setName(string $new_name): self
97
    {
98
        $this->name = $new_name;
99
100
        return $this;
101
    }
102
}
103