Failed Conditions
Push — master ( 8be1e3...e3936d )
by Marco
14s
created

IdentityFunction::getSql()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 6.0018

Importance

Changes 0
Metric Value
cc 6
eloc 26
nc 10
nop 1
dl 0
loc 43
ccs 26
cts 27
cp 0.963
crap 6.0018
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\Lexer;
8
use Doctrine\ORM\Query\Parser;
9
use Doctrine\ORM\Query\QueryException;
10
use Doctrine\ORM\Query\SqlWalker;
11
12
/**
13
 * "IDENTITY" "(" SingleValuedAssociationPathExpression {"," string} ")"
14
 */
15
class IdentityFunction extends FunctionNode
16
{
17
    /**
18
     * @var \Doctrine\ORM\Query\AST\PathExpression
19
     */
20
    public $pathExpression;
21
22
    /**
23
     * @var string
24
     */
25
    public $fieldMapping;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 8
    public function getSql(SqlWalker $sqlWalker)
31
    {
32 8
        $entityManager = $sqlWalker->getEntityManager();
33 8
        $platform      = $entityManager->getConnection()->getDatabasePlatform();
34 8
        $dqlAlias      = $this->pathExpression->identificationVariable;
35 8
        $assocField    = $this->pathExpression->field;
36 8
        $qComp         = $sqlWalker->getQueryComponent($dqlAlias);
37 8
        $class         = $qComp['metadata'];
38 8
        $association   = $class->getProperty($assocField);
39 8
        $targetEntity  = $sqlWalker->getEntityManager()->getClassMetadata($association->getTargetEntity());
40 8
        $joinColumns   = $association->getJoinColumns();
41 8
        $joinColumn    = reset($joinColumns);
42
43 8
        if ($this->fieldMapping !== null) {
44 2
            $property = $targetEntity->getProperty($this->fieldMapping);
0 ignored issues
show
Bug introduced by
The method getProperty() does not exist on Doctrine\Common\Persistence\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

44
            /** @scrutinizer ignore-call */ 
45
            $property = $targetEntity->getProperty($this->fieldMapping);

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...
45
46 2
            if ($property === null) {
47 1
                throw new QueryException(sprintf('Undefined reference field mapping "%s"', $this->fieldMapping));
48
            }
49
50 2
            $joinColumn = null;
51
52 2
            foreach ($joinColumns as $mapping) {
53 2
                if ($mapping->getReferencedColumnName() === $property->getColumnName()) {
54 2
                    $joinColumn = $mapping;
55
56 2
                    break;
57
                }
58
            }
59
60 2
            if ($joinColumn === null) {
61
                throw new QueryException(sprintf('Unable to resolve the reference field mapping "%s"', $this->fieldMapping));
62
            }
63
        }
64
65
        // The table with the relation may be a subclass, so get the table name from the association definition
66 8
        $sourceClass = $sqlWalker->getEntityManager()->getClassMetadata($association->getSourceEntity());
67 8
        $tableName   = $sourceClass->getTableName();
68
69 8
        $tableAlias       = $sqlWalker->getSQLTableAlias($tableName, $dqlAlias);
70 8
        $quotedColumnName = $platform->quoteIdentifier($joinColumn->getColumnName());
71
72 8
        return $tableAlias . '.' . $quotedColumnName;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 8
    public function parse(Parser $parser)
79
    {
80 8
        $parser->match(Lexer::T_IDENTIFIER);
81 8
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
82
83 8
        $this->pathExpression = $parser->SingleValuedAssociationPathExpression();
84
85 8
        if ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
86 2
            $parser->match(Lexer::T_COMMA);
87 2
            $parser->match(Lexer::T_STRING);
88
89 2
            $this->fieldMapping = $parser->getLexer()->token['value'];
90
        }
91
92 8
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
93 8
    }
94
}
95