Failed Conditions
Push — 2.7 ( c036c0...266f0d )
by Jonathan
57:23 queued 50:07
created

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

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) {
0 ignored issues
show
It seems like you are loosely comparing $currentLevel of type boolean|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
88
                    $this->_nextValue = $currentLevel;
0 ignored issues
show
Documentation Bug introduced by
It seems like $currentLevel of type string or true 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;
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