Failed Conditions
Pull Request — develop (#6947)
by Filippo
10:01
created

BigIntegerIdentityGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generate() 0 3 1
A isPostInsertGenerator() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Sequencing;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
9
/**
10
 * Id generator that obtains IDs from special "identity" columns. These are columns
11
 * that automatically get a database-generated, auto-incremented identifier on INSERT.
12
 * This generator obtains the last insert id after such an insert.
13
 */
14
class BigIntegerIdentityGenerator implements Generator
15
{
16
    /**
17
     * The name of the sequence to pass to lastInsertId(), if any.
18
     *
19
     * @var string
20
     */
21
    private $sequenceName;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param string|null $sequenceName The name of the sequence to pass to lastInsertId()
27
     *                                  to obtain the last generated identifier within the current
28
     *                                  database session/connection, if any.
29
     */
30 1
    public function __construct($sequenceName = null)
31
    {
32 1
        $this->sequenceName = $sequenceName;
33 1
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function generate(EntityManagerInterface $em, $entity)
39
    {
40 3
        return (string) $em->getConnection()->lastInsertId($this->sequenceName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return (string)$em->getC...Id($this->sequenceName) returns the type string which is incompatible with the return type mandated by Doctrine\ORM\Sequencing\Generator::generate() of Generator.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public function isPostInsertGenerator()
47
    {
48 3
        return true;
49
    }
50
}
51
52