Passed
Push — master ( ccdac1...a3c626 )
by Jan
04:20
created

AbstractNamedDBElement   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A __toString() 0 3 1
A setName() 0 4 1
A __clone() 0 7 2
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 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 $name = '';
48
49
    /******************************************************************************
50
     *
51
     * Helpers
52
     *
53
     ******************************************************************************/
54
55
    public function __toString()
56
    {
57
        return $this->getName();
58
    }
59
60
    /********************************************************************************
61
     *
62
     *   Getters
63
     *
64
     *********************************************************************************/
65
66
    /**
67
     * Get the name of this element.
68
     *
69
     * @return string the name of this element
70
     */
71
    public function getName(): string
72
    {
73
        return $this->name;
74
    }
75
76
    /********************************************************************************
77
     *
78
     *   Setters
79
     *
80
     *********************************************************************************/
81
82
    /**
83
     * Change the name of this element.
84
     *
85
     * @param string $new_name the new name
86
     *
87
     * @return self
88
     */
89
    public function setName(string $new_name): self
90
    {
91
        $this->name = $new_name;
92
        return $this;
93
    }
94
95
    public function __clone()
96
    {
97
        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...
98
            //We create a new object, so give it a new creation date
99
           $this->addedDate = null;
100
        }
101
        parent::__clone(); // TODO: Change the autogenerated stub
102
    }
103
}
104