Failed Conditions
Push — master ( e747f7...5b15a6 )
by Guilherme
19:57
created

ValueGeneratorMetadata::getDeclaringProperty()   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
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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 296
    public function getDefinition() : array
50
    {
51 296
        return $this->definition;
52
    }
53
54
    /**
55
     * @throws DBALException
56
     */
57 293
    public function getSequencingGenerator(AbstractPlatform $platform) : Sequencing\Generator
58
    {
59 293
        $class = $this->declaringProperty->getDeclaringClass();
60
61 293
        switch ($this->type) {
62
            case GeneratorType::IDENTITY:
63 286
                $sequenceName = null;
64
65
                // Platforms that do not have native IDENTITY support need a sequence to emulate this behaviour.
66 286
                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
                }
71
72 286
                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

72
                return $this->declaringProperty->/** @scrutinizer ignore-call */ getTypeName() === 'bigint'
Loading history...
73 1
                    ? new Sequencing\BigIntegerIdentityGenerator($sequenceName)
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\ORM\Sequencing\...enerator::__construct() has too many arguments starting with $sequenceName. ( Ignorable by Annotation )

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

73
                    ? /** @scrutinizer ignore-call */ new Sequencing\BigIntegerIdentityGenerator($sequenceName)

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
74 286
                    : new Sequencing\IdentityGenerator($sequenceName);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\ORM\Sequencing\...enerator::__construct() has too many arguments starting with $sequenceName. ( Ignorable by Annotation )

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

74
                    : /** @scrutinizer ignore-call */ new Sequencing\IdentityGenerator($sequenceName);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
75
            case GeneratorType::SEQUENCE:
76 6
                return new Sequencing\SequenceGenerator(
77 6
                    $platform->quoteIdentifier($this->definition['sequenceName']),
78 6
                    $this->definition['allocationSize']
79
                );
80
            case GeneratorType::CUSTOM:
81 1
                $class = $this->definition['class'];
82
83 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...
84
        }
85
    }
86
}
87