Failed Conditions
Push — master ( 6744b4...2b8acb )
by Marco
60:45 queued 60:36
created

lib/Doctrine/ORM/Id/TableGenerator.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Id;
21
22
use Doctrine\ORM\EntityManager;
23
24
/**
25
 * Id generator that uses a single-row database table and a hi/lo algorithm.
26
 *
27
 * @since   2.0
28
 * @author  Benjamin Eberlei <[email protected]>
29
 * @author  Guilherme Blanco <[email protected]>
30
 * @author  Jonathan Wage <[email protected]>
31
 * @author  Roman Borschel <[email protected]>
32
 */
33
class TableGenerator extends AbstractIdGenerator
34
{
35
    /**
36
     * @var string
37
     */
38
    private $_tableName;
39
40
    /**
41
     * @var string
42
     */
43
    private $_sequenceName;
44
45
    /**
46
     * @var int
47
     */
48
    private $_allocationSize;
49
50
    /**
51
     * @var int|null
52
     */
53
    private $_nextValue;
54
55
    /**
56
     * @var int|null
57
     */
58
    private $_maxValue;
59
60
    /**
61
     * @param string $tableName
62
     * @param string $sequenceName
63
     * @param int    $allocationSize
64
     */
65
    public function __construct($tableName, $sequenceName = 'default', $allocationSize = 10)
66
    {
67
        $this->_tableName = $tableName;
68
        $this->_sequenceName = $sequenceName;
69
        $this->_allocationSize = $allocationSize;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function generate(
76
        EntityManager $em, $entity)
77
    {
78
        if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) {
79
            // Allocate new values
80
            $conn = $em->getConnection();
81
82
            if ($conn->getTransactionNestingLevel() === 0) {
83
                // use select for update
84
                $sql          = $conn->getDatabasePlatform()->getTableHiLoCurrentValSql($this->_tableName, $this->_sequenceName);
85
                $currentLevel = $conn->fetchColumn($sql);
86
87
                if ($currentLevel != null) {
88
                    $this->_nextValue = $currentLevel;
0 ignored issues
show
Documentation Bug introduced by
It seems like $currentLevel of type string or boolean is incompatible with the declared type integer|null of property $_nextValue.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89
                    $this->_maxValue = $this->_nextValue + $this->_allocationSize;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_nextValue + $this->_allocationSize can also be of type double. However, the property $_maxValue is declared as type integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
90
91
                    $updateSql = $conn->getDatabasePlatform()->getTableHiLoUpdateNextValSql(
92
                        $this->_tableName, $this->_sequenceName, $this->_allocationSize
93
                    );
94
95
                    if ($conn->executeUpdate($updateSql, [1 => $currentLevel, 2 => $currentLevel+1]) !== 1) {
96
                        // no affected rows, concurrency issue, throw exception
97
                    }
98
                } else {
99
                    // no current level returned, TableGenerator seems to be broken, throw exception
100
                }
101
            } else {
102
                // only table locks help here, implement this or throw exception?
103
                // or do we want to work with table locks exclusively?
104
            }
105
        }
106
107
        return $this->_nextValue++;
108
    }
109
}
110