Issues (257)

src/Entity/Base/AbstractDBElement.php (1 issue)

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 Doctrine\ORM\Mapping as ORM;
26
use JsonSerializable;
27
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
28
use Symfony\Component\Serializer\Annotation\Groups;
29
30
/**
31
 * This class is for managing all database objects.
32
 *
33
 * You should use this class for ALL classes which manages database records!
34
 *          (except special tables like "internal"...)
35
 * Every database table which are managed with this class (or a subclass of it)
36
 *          must have the table row "id"!! The ID is the unique key to identify the elements.
37
 *
38
 * @ORM\MappedSuperclass(repositoryClass="App\Repository\DBElementRepository")
39
 *
40
 * @DiscriminatorMap(typeProperty="type", mapping={
41
 *      "attachment_type" = "App\Entity\AttachmentType",
42
 *      "attachment" = "App\Entity\Attachment",
43
 *      "category" = "App\Entity\Attachment",
44
 *      "project" = "App\Entity\ProjectSystem\Project",
45
 *      "project_bom_entry" = "App\Entity\ProjectSystem\ProjectBOMEntry",
46
 *      "footprint" = "App\Entity\Footprint",
47
 *      "group" = "App\Entity\Group",
48
 *      "manufacturer" = "App\Entity\Manufacturer",
49
 *      "orderdetail" = "App\Entity\Orderdetail",
50
 *      "part" = "App\Entity\Part",
51
 *      "pricedetail" = "App\Entity\Pricedetail",
52
 *      "storelocation" = "App\Entity\Storelocation",
53
 *      "supplier" = "App\Entity\Supplier",
54
 *      "user" = "App\Entity\User"
55
 *  })
56
 */
57
abstract class AbstractDBElement implements JsonSerializable
58
{
59
    /** @var int|null The Identification number for this part. This value is unique for the element in this table.
60
     * Null if the element is not saved to DB yet.
61
     * @ORM\Column(type="integer")
62
     * @ORM\Id()
63
     * @ORM\GeneratedValue()
64
     * @Groups({"full"})
65
     */
66
    protected ?int $id = null;
67
68
    public function __clone()
69
    {
70
        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...
71
            //Set ID to null, so that an new entry is created
72
            $this->id = null;
73
        }
74
    }
75
76
    /**
77
     * Get the ID. The ID can be zero, or even negative (for virtual elements). If an element is virtual, can be
78
     * checked with isVirtualElement().
79
     *
80
     * Returns null, if the element is not saved to the DB yet.
81
     *
82
     * @return int|null the ID of this element
83
     */
84
    public function getID(): ?int
85
    {
86
        return $this->id;
87
    }
88
89
    public function jsonSerialize(): array
90
    {
91
        return ['@id' => $this->getID()];
92
    }
93
}
94