Failed Conditions
Push — master ( fa7802...d60694 )
by Guilherme
09:27
created

TableGenerator::isPostInsertGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Sequencing\Generator;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
9
/**
10
 * Id generator that uses a single-row database table and a hi/lo algorithm.
11
 */
12
class TableGenerator implements Generator
13
{
14
    /** @var string */
15
    private $tableName;
16
17
    /** @var string */
18
    private $sequenceName;
19
20
    /** @var int */
21
    private $allocationSize;
22
23
    /** @var int|null */
24
    private $nextValue;
25
26
    /** @var int|null */
27
    private $maxValue;
28
29
    public function __construct(string $tableName, string $sequenceName = 'default', int $allocationSize = 10)
30
    {
31
        $this->tableName      = $tableName;
32
        $this->sequenceName   = $sequenceName;
33
        $this->allocationSize = $allocationSize;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function generate(EntityManagerInterface $em, ?object $entity)
40
    {
41
        if ($this->maxValue === null || $this->nextValue === $this->maxValue) {
42
            // Allocate new values
43
            $conn = $em->getConnection();
44
45
            if ($conn->getTransactionNestingLevel() === 0) {
46
                // use select for update
47
                $platform     = $conn->getDatabasePlatform();
48
                $sql          = $platform->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

48
                /** @scrutinizer ignore-call */ 
49
                $sql          = $platform->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...
49
                $currentLevel = $conn->fetchColumn($sql);
50
51
                if ($currentLevel !== null) {
52
                    $this->nextValue = $currentLevel;
0 ignored issues
show
Documentation Bug introduced by
It seems like $currentLevel can also be of type false. However, the property $nextValue 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...
53
                    $this->maxValue  = $this->nextValue + $this->allocationSize;
54
55
                    $updateSql = $platform->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

55
                    /** @scrutinizer ignore-call */ 
56
                    $updateSql = $platform->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...
56
                        $this->tableName,
57
                        $this->sequenceName,
58
                        $this->allocationSize
59
                    );
60
61
                    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...
62
                        // no affected rows, concurrency issue, throw exception
63
                    }
64
                } 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...
65
                    // no current level returned, TableGenerator seems to be broken, throw exception
66
                }
67
            } 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...
68
                // only table locks help here, implement this or throw exception?
69
                // or do we want to work with table locks exclusively?
70
            }
71
        }
72
73
        return $this->nextValue++;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function isPostInsertGenerator() : bool
80
    {
81
        return false;
82
    }
83
}
84