Passed
Pull Request — master (#6834)
by Massimiliano
15:53
created

DefaultQuoteStrategy   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 136
Duplicated Lines 4.41 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 98.25%

Importance

Changes 0
Metric Value
wmc 24
lcom 0
cbo 3
dl 6
loc 136
ccs 56
cts 57
cp 0.9825
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnName() 0 6 2
B getTableName() 0 20 7
A getSequenceName() 0 6 2
A getJoinColumnName() 0 6 2
A getReferencedJoinColumnName() 0 6 2
A getJoinTableName() 0 16 3
B getIdentifierColumnNames() 6 28 4
A getColumnAlias() 0 14 2

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
20
namespace Doctrine\ORM\Mapping;
21
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
24
/**
25
 * A set of rules for determining the physical column, alias and table quotes
26
 *
27
 * @since   2.3
28
 * @author  Fabio B. Silva <[email protected]>
29
 */
30
class DefaultQuoteStrategy implements QuoteStrategy
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35 1418
    public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform)
36
    {
37 1418
        return isset($class->fieldMappings[$fieldName]['quoted'])
38 23
            ? $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
39 1418
            : $class->fieldMappings[$fieldName]['columnName'];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1425
    public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
46
    {
47 1425
        $tableName = $class->table['name'];
48
49 1425
        if ( ! empty($class->table['schema'])) {
50 5
            $tableName = $class->table['schema'] . '.' . $class->table['name'];
51
52 5
            if ( ! $platform->supportsSchemas() && $platform->canEmulateSchemas()) {
53 5
                $tableName = $class->table['schema'] . '__' . $class->table['name'];
54
            }
55
        }
56
57 1425
        $keywords = $platform->getReservedKeywordsList();
58 1425
        $parts = explode(".", $tableName);
59 1425
        foreach ($parts as $k => $v) {
60 1425
            $parts[$k] = (isset($class->table['quoted']) || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
61
        }
62
63 1425
        return implode(".", $parts);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 6
    public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform)
70
    {
71 6
        return isset($definition['quoted'])
72
            ? $platform->quoteIdentifier($definition['sequenceName'])
73 6
            : $definition['sequenceName'];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1067
    public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
80
    {
81 1067
        return isset($joinColumn['quoted'])
82 13
            ? $platform->quoteIdentifier($joinColumn['name'])
83 1067
            : $joinColumn['name'];
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 584
    public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
90
    {
91 584
        return isset($joinColumn['quoted'])
92 8
            ? $platform->quoteIdentifier($joinColumn['referencedColumnName'])
93 584
            : $joinColumn['referencedColumnName'];
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 400
    public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform)
100
    {
101 400
        $schema = '';
102
103 400
        if (isset($association['joinTable']['schema'])) {
104 1
            $schema = $association['joinTable']['schema'] . '.';
105
        }
106
107 400
        $tableName = $association['joinTable']['name'];
108
109 400
        if (isset($association['joinTable']['quoted'])) {
110 9
            $tableName = $platform->quoteIdentifier($tableName);
111
        }
112
113 400
        return $schema . $tableName;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 378
    public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform)
120
    {
121 378
        $quotedColumnNames = [];
122
123 378
        foreach ($class->identifier as $fieldName) {
124 378
            if (isset($class->fieldMappings[$fieldName])) {
125 374
                $quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform);
126
127 374
                continue;
128
            }
129
130
            // Association defined as Id field
131 8
            $joinColumns            = $class->associationMappings[$fieldName]['joinColumns'];
132 8
            $assocQuotedColumnNames = array_map(
133 8 View Code Duplication
                function ($joinColumn) use ($platform)
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...
134
                {
135 8
                    return isset($joinColumn['quoted'])
136 1
                        ? $platform->quoteIdentifier($joinColumn['name'])
137 8
                        : $joinColumn['name'];
138 8
                },
139 8
                $joinColumns
140
            );
141
142 8
            $quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
143
        }
144
145 378
        return $quotedColumnNames;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 1138
    public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
152
    {
153
        // 1 ) Concatenate column name and counter
154
        // 2 ) Trim the column alias to the maximum identifier length of the platform.
155
        //     If the alias is to long, characters are cut off from the beginning.
156
        // 3 ) Strip non alphanumeric characters
157
        // 4 ) Prefix with "_" if the result its numeric
158 1138
        $columnName = $columnName . '_' . $counter;
159 1138
        $columnName = substr($columnName, -$platform->getMaxIdentifierLength());
160 1138
        $columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
161 1138
        $columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;
162
163 1138
        return $platform->getSQLResultCasing($columnName);
164
    }
165
}
166