Failed Conditions
Pull Request — master (#7046)
by Gabriel
14:57
created

CustomTreeWalkerJoin::walkSelectStatement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
nc 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Query;
6
7
use Doctrine\ORM\Query;
8
use Doctrine\Tests\Models\CMS\CmsAddress;
9
use Doctrine\Tests\Models\CMS\CmsUser;
10
use Doctrine\Tests\OrmTestCase;
11
12
/**
13
 * Test case for custom AST walking and adding new joins.
14
 *
15
 * @link        http://www.doctrine-project.org
16
 */
17
class CustomTreeWalkersJoinTest extends OrmTestCase
18
{
19
    private $em;
20
21
    protected function setUp()
22
    {
23
        $this->em = $this->getTestEntityManager();
24
    }
25
26
    public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
27
    {
28
        try {
29
            $query = $this->em->createQuery($dqlToBeTested);
30
            $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [CustomTreeWalkerJoin::class])
31
                  ->useQueryCache(false);
32
33
            $sqlGenerated = $query->getSql();
34
35
            $query->free();
36
        } catch (\Exception $e) {
37
            $this->fail($e->getMessage() . ' at "' . $e->getFile() . '" on line ' . $e->getLine());
38
        }
39
40
        self::assertEquals($sqlToBeConfirmed, $sqlGenerated);
41
    }
42
43
    public function testAddsJoin()
44
    {
45
        self::assertSqlGeneration(
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...::assertSqlGeneration() is not static, but was called statically. ( Ignorable by Annotation )

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

45
        self::/** @scrutinizer ignore-call */ 
46
              assertSqlGeneration(
Loading history...
46
            'select u from Doctrine\Tests\Models\CMS\CmsUser u',
47
            'SELECT t0."id" AS c0, t0."status" AS c1, t0."username" AS c2, t0."name" AS c3, t1."id" AS c4, t1."country" AS c5, t1."zip" AS c6, t1."city" AS c7, t0."email_id" AS c8, t1."user_id" AS c9 FROM "cms_users" t0 LEFT JOIN "cms_addresses" t1 ON t0."id" = t1."user_id"'
48
        );
49
    }
50
51
    public function testDoesNotAddJoin()
52
    {
53
        self::assertSqlGeneration(
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...::assertSqlGeneration() is not static, but was called statically. ( Ignorable by Annotation )

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

53
        self::/** @scrutinizer ignore-call */ 
54
              assertSqlGeneration(
Loading history...
54
            'select a from Doctrine\Tests\Models\CMS\CmsAddress a',
55
            'SELECT t0."id" AS c0, t0."country" AS c1, t0."zip" AS c2, t0."city" AS c3, t0."user_id" AS c4 FROM "cms_addresses" t0'
56
        );
57
    }
58
}
59
60
class CustomTreeWalkerJoin extends Query\TreeWalkerAdapter
61
{
62
    public function walkSelectStatement(Query\AST\SelectStatement $selectStatement)
63
    {
64
        foreach ($selectStatement->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) {
65
            $rangeVariableDecl = $identificationVariableDeclaration->rangeVariableDeclaration;
66
67
            if ($rangeVariableDecl->abstractSchemaName !== CmsUser::class) {
68
                continue;
69
            }
70
71
            $this->modifySelectStatement($selectStatement, $identificationVariableDeclaration);
72
        }
73
    }
74
75
    private function modifySelectStatement(Query\AST\SelectStatement $selectStatement, $identificationVariableDecl)
76
    {
77
        $rangeVariableDecl       = $identificationVariableDecl->rangeVariableDeclaration;
78
        $joinAssocPathExpression = new Query\AST\JoinAssociationPathExpression($rangeVariableDecl->aliasIdentificationVariable, 'address');
79
        $joinAssocDeclaration    = new Query\AST\JoinAssociationDeclaration($joinAssocPathExpression, $rangeVariableDecl->aliasIdentificationVariable . 'a', null);
80
        $join                    = new Query\AST\Join(Query\AST\Join::JOIN_TYPE_LEFT, $joinAssocDeclaration);
81
        $selectExpression        = new Query\AST\SelectExpression($rangeVariableDecl->aliasIdentificationVariable . 'a', null, false);
82
83
        $identificationVariableDecl->joins[]                = $join;
84
        $selectStatement->selectClause->selectExpressions[] = $selectExpression;
85
86
        $entityManager   = $this->getQuery()->getEntityManager();
87
        $userMetadata    = $entityManager->getClassMetadata(CmsUser::class);
88
        $addressMetadata = $entityManager->getClassMetadata(CmsAddress::class);
89
90
        $this->setQueryComponent(
91
            $rangeVariableDecl->aliasIdentificationVariable . 'a',
92
            [
93
                'metadata'     => $addressMetadata,
94
                'parent'       => $rangeVariableDecl->aliasIdentificationVariable,
95
                'relation'     => $userMetadata->getProperty('address'),
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

95
                'relation'     => $userMetadata->/** @scrutinizer ignore-call */ getProperty('address'),

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...
96
                'map'          => null,
97
                'nestingLevel' => 0,
98
                'token'        => null,
99
            ]
100
        );
101
    }
102
}
103