Failed Conditions
Pull Request — 2.6 (#7180)
by Ben
11:16
created

TableGenerator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generate() 0 33 6
A __construct() 0 5 1
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);
0 ignored issues
show
Bug introduced by
The method getTableHiLoCurrentValSql() does not exist on Doctrine\DBAL\Platforms\AbstractPlatform. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
                $sql          = $conn->getDatabasePlatform()->/** @scrutinizer ignore-call */ getTableHiLoCurrentValSql($this->_tableName, $this->_sequenceName);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
                $currentLevel = $conn->fetchColumn($sql);
86
87
                if ($currentLevel != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $currentLevel of type string|boolean 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 null|integer 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(
0 ignored issues
show
Bug introduced by
The method getTableHiLoUpdateNextValSql() does not exist on Doctrine\DBAL\Platforms\AbstractPlatform. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
                    $updateSql = $conn->getDatabasePlatform()->/** @scrutinizer ignore-call */ getTableHiLoUpdateNextValSql(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
                        $this->_tableName, $this->_sequenceName, $this->_allocationSize
93
                    );
94
95
                    if ($conn->executeUpdate($updateSql, [1 => $currentLevel, 2 => $currentLevel+1]) !== 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
96
                        // no affected rows, concurrency issue, throw exception
97
                    }
98
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
99
                    // no current level returned, TableGenerator seems to be broken, throw exception
100
                }
101
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
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