Completed
Pull Request — master (#1922)
by chihiro
36:39
created

CountOutputWalker   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 10.31 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
dl 10
loc 97
ccs 26
cts 34
cp 0.7647
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C walkSelectStatement() 10 51 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
namespace Eccube\Doctrine\ORM\Tools\Pagination;
20
use Doctrine\ORM\Query\SqlWalker;
21
use Doctrine\ORM\Query\AST\SelectStatement;
22
/**
23
 * Wraps the query in order to accurately count the root objects.
24
 *
25
 * Given a DQL like `SELECT u FROM User u` it will generate an SQL query like:
26
 * SELECT COUNT(*) (SELECT DISTINCT <id> FROM (<original SQL>))
27
 *
28
 * Works with composite keys but cannot deal with queries that have multiple
29
 * root entities (e.g. `SELECT f, b from Foo, Bar`)
30
 *
31
 * @author Sander Marechal <[email protected]>
32
 */
33
class CountOutputWalker extends SqlWalker
34
{
35
    /**
36
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
37
     */
38
    private $platform;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
39
    /**
40
     * @var \Doctrine\ORM\Query\ResultSetMapping
41
     */
42
    private $rsm;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
43
    /**
44
     * @var array
45
     */
46
    private $queryComponents;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
47
    /**
48
     * Constructor.
49
     *
50
     * Stores various parameters that are otherwise unavailable
51
     * because Doctrine\ORM\Query\SqlWalker keeps everything private without
52
     * accessors.
53
     *
54
     * @param \Doctrine\ORM\Query              $query
55
     * @param \Doctrine\ORM\Query\ParserResult $parserResult
56
     * @param array                            $queryComponents
57
     */
58 19
    public function __construct($query, $parserResult, array $queryComponents)
59
    {
60 19
        $this->platform = $query->getEntityManager()->getConnection()->getDatabasePlatform();
61 19
        $this->rsm = $parserResult->getResultSetMapping();
62 19
        $this->queryComponents = $queryComponents;
63 19
        parent::__construct($query, $parserResult, $queryComponents);
64
    }
65
    /**
66
     * Walks down a SelectStatement AST node, wrapping it in a COUNT (SELECT DISTINCT).
67
     *
68
     * Note that the ORDER BY clause is not removed. Many SQL implementations (e.g. MySQL)
69
     * are able to cache subqueries. By keeping the ORDER BY clause intact, the limitSubQuery
70
     * that will most likely be executed next can be read from the native SQL cache.
71
     *
72
     * @param SelectStatement $AST
73
     *
74
     * @return string
75
     *
76
     * @throws \RuntimeException
77
     */
78 19
    public function walkSelectStatement(SelectStatement $AST)
79
    {
80 19
        if ($this->platform->getName() === "mssql") {
81
            $AST->orderByClause = null;
82
        }
83 19
        $sql = parent::walkSelectStatement($AST);
84
        // Find out the SQL alias of the identifier column of the root entity
85
        // It may be possible to make this work with multiple root entities but that
86
        // would probably require issuing multiple queries or doing a UNION SELECT
87
        // so for now, It's not supported.
88
        // Get the root entity and alias from the AST fromClause
89 19
        $from = $AST->fromClause->identificationVariableDeclarations;
90 19
        if (count($from) > 1) {
91
            throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
92
        }
93 19
        $fromRoot       = reset($from);
94 19
        $rootAlias      = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable;
95 19
        $rootClass      = $this->queryComponents[$rootAlias]['metadata'];
96 19
        $rootIdentifier = $rootClass->identifier;
97
        // For every identifier, find out the SQL alias by combing through the ResultSetMapping
98 19
        $sqlIdentifier = array();
99 19
        foreach ($rootIdentifier as $property) {
100 19
            if (isset($rootClass->fieldMappings[$property])) {
101 19 View Code Duplication
                foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102 19
                    if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
103 19
                        $sqlIdentifier[$property] = $alias;
104
                    }
105
                }
106
            }
107 19
            if (isset($rootClass->associationMappings[$property])) {
108
                $joinColumn = $rootClass->associationMappings[$property]['joinColumns'][0]['name'];
109 View Code Duplication
                foreach (array_keys($this->rsm->metaMappings, $joinColumn) as $alias) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
                    if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
111 19
                        $sqlIdentifier[$property] = $alias;
112
                    }
113
                }
114
            }
115
        }
116 19
        if (count($rootIdentifier) != count($sqlIdentifier)) {
117
            throw new \RuntimeException(sprintf(
118
                'Not all identifier properties can be found in the ResultSetMapping: %s',
119
                implode(', ', array_diff($rootIdentifier, array_keys($sqlIdentifier)))
120
            ));
121
        }
122
        // Build the counter query
123 19
        return sprintf('SELECT %s AS dctrn_count FROM (SELECT DISTINCT %s FROM (%s) dctrn_result) dctrn_table',
124 19
            $this->platform->getCountExpression('*'),
125 19
            implode(', ', $sqlIdentifier),
126
            $sql
127
        );
128
    }
129
}
130