Failed Conditions
Push — master ( becf73...b9880b )
by Guilherme
09:53
created

ValueGeneratorMetadata::getSequencingGenerator()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.7717

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 6
nop 1
dl 0
loc 30
ccs 13
cts 18
cp 0.7221
crap 6.7717
rs 8.9777
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\ORM\Sequencing;
10
11
class ValueGeneratorMetadata
12
{
13
    /** @var Property */
14
    protected $declaringProperty;
15
16
    /** @var string */
17
    protected $type;
18
19
    /** @var mixed[] */
20
    protected $definition;
21
22
    /**
23
     * @param mixed[] $definition
24
     */
25 318
    public function __construct(string $type, array $definition = [])
26
    {
27 318
        $this->type       = $type;
28 318
        $this->definition = $definition;
29 318
    }
30
31
    public function getDeclaringProperty() : Property
32
    {
33
        return $this->declaringProperty;
34
    }
35
36 318
    public function setDeclaringProperty(Property $declaringProperty) : void
37
    {
38 318
        $this->declaringProperty = $declaringProperty;
39 318
    }
40
41 1105
    public function getType() : string
42
    {
43 1105
        return $this->type;
44
    }
45
46
    /**
47
     * @return mixed[]
48
     */
49 298
    public function getDefinition() : array
50
    {
51 298
        return $this->definition;
52
    }
53
54
    /**
55
     * @throws DBALException
56
     */
57 294
    public function getSequencingGenerator(AbstractPlatform $platform) : Sequencing\Generator
58
    {
59 294
        $class = $this->declaringProperty->getDeclaringClass();
60
61 294
        switch ($this->type) {
62
            case GeneratorType::IDENTITY:
63 287
                $sequenceName = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $sequenceName is dead and can be removed.
Loading history...
64
65
                // Platforms that do not have native IDENTITY support need a sequence to emulate this behaviour.
66 287
                if ($platform->usesSequenceEmulatedIdentityColumns()) {
67
                    $sequencePrefix = $platform->getSequencePrefix($class->getTableName(), $class->getSchemaName());
0 ignored issues
show
Bug introduced by
The method getSchemaName() does not exist on Doctrine\ORM\Mapping\ComponentMetadata. It seems like you code against a sub-type of Doctrine\ORM\Mapping\ComponentMetadata such as Doctrine\ORM\Mapping\ClassMetadata. ( Ignorable by Annotation )

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

67
                    $sequencePrefix = $platform->getSequencePrefix($class->getTableName(), $class->/** @scrutinizer ignore-call */ getSchemaName());
Loading history...
Bug introduced by
The method getTableName() does not exist on Doctrine\ORM\Mapping\ComponentMetadata. It seems like you code against a sub-type of Doctrine\ORM\Mapping\ComponentMetadata such as Doctrine\ORM\Mapping\ClassMetadata. ( Ignorable by Annotation )

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

67
                    $sequencePrefix = $platform->getSequencePrefix($class->/** @scrutinizer ignore-call */ getTableName(), $class->getSchemaName());
Loading history...
68
                    $idSequenceName = $platform->getIdentitySequenceName($sequencePrefix, $this->declaringProperty->getColumnName());
0 ignored issues
show
Bug introduced by
The method getColumnName() does not exist on Doctrine\ORM\Mapping\Property. It seems like you code against a sub-type of Doctrine\ORM\Mapping\Property such as Doctrine\ORM\Mapping\FieldMetadata. ( Ignorable by Annotation )

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

68
                    $idSequenceName = $platform->getIdentitySequenceName($sequencePrefix, $this->declaringProperty->/** @scrutinizer ignore-call */ getColumnName());
Loading history...
69
                    $sequenceName   = $platform->quoteIdentifier($platform->fixSchemaElementName($idSequenceName));
70
                    $allocationSize = $this->definition['allocationSize'] ?? 1;
71
72
                    return new Sequencing\SequenceGenerator($sequenceName, $allocationSize);
73
                }
74
75 287
                return $this->declaringProperty->getTypeName() === 'bigint'
0 ignored issues
show
Bug introduced by
The method getTypeName() does not exist on Doctrine\ORM\Mapping\Property. It seems like you code against a sub-type of Doctrine\ORM\Mapping\Property such as Doctrine\ORM\Mapping\FieldMetadata. ( Ignorable by Annotation )

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

75
                return $this->declaringProperty->/** @scrutinizer ignore-call */ getTypeName() === 'bigint'
Loading history...
76 1
                    ? new Sequencing\BigIntegerIdentityGenerator()
77 287
                    : new Sequencing\IdentityGenerator();
78
            case GeneratorType::SEQUENCE:
79 6
                $sequenceName   = $platform->quoteIdentifier($this->definition['sequenceName']);
80 6
                $allocationSize = $this->definition['allocationSize'] ?? 1;
81
82 6
                return new Sequencing\SequenceGenerator($sequenceName, $allocationSize);
83
            case GeneratorType::CUSTOM:
84 1
                $class = $this->definition['class'];
85
86 1
                return new $class();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Doctrine\ORM\Sequencing\Generator. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
87
        }
88
    }
89
}
90