Completed
Push — master ( 37855a...7bba72 )
by Nikola
02:29
created

NamerCollection::joinTableName()   D

Complexity

Conditions 10
Paths 16

Size

Total Lines 27
Code Lines 18

Duplication

Lines 14
Ratio 51.85 %

Code Coverage

Tests 10
CRAP Score 20.6294

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 14
loc 27
ccs 10
cts 19
cp 0.5263
rs 4.8197
cc 10
eloc 18
nc 16
nop 3
crap 20.6294

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of the Doctrine Naming Strategy Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy;
11
12
use Doctrine\ORM\Mapping\NamingStrategy;
13
14
class NamerCollection implements NamingStrategy
15
{
16
    const UNDERSCORE = 'underscore';
17
    const NOTHING = 'nothing';
18
    const UCFIRST = 'ucfirst';
19
20
    /**
21
     * @var NamingStrategy
22
     */
23
    protected $defaultNamer;
24
25
    /**
26
     * @var NamingStrategy[]
27
     */
28
    protected $concurrentNamers;
29
30
    /**
31
     * @var string
32
     */
33
    protected $concatenation;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $joinTableFieldSuffix;
39
40 4
    public function __construct(NamingStrategy $defaultNamer, array $concurrentNamers, array $configuration = array())
41
    {
42 4
        $this->defaultNamer = $defaultNamer;
43 4
        $this->concurrentNamers = $concurrentNamers;
44 4
        $configuration = array_merge(array(
45 4
            'concatenation' => self::UNDERSCORE,
46
            'joinTableFieldSuffix' => true
47 2
        ), $configuration);
48
49 4
        $this->concatenation = $configuration['concatenation'];
50 4
        $this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix'];
51
52 4
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 4
    public function classToTableName($className)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59 4
        $defaultName = $this->defaultNamer->classToTableName($className);
60
61
        /**
62
         * @var NamingStrategy $concurrentNamer
63
         */
64 4
        foreach ($this->concurrentNamers as $concurrentNamer) {
65
66 4
            if (($newProposal = $concurrentNamer->classToTableName($className)) != $defaultName) {
67 4
                return $newProposal;
68
            }
69 1
        }
70
71
        return $defaultName;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function propertyToColumnName($propertyName, $className = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79
        $defaultName = $this->defaultNamer->propertyToColumnName($propertyName, $className);
80
81
        /**
82
         * @var NamingStrategy $concurrentNamer
83
         */
84
        foreach ($this->concurrentNamers as $concurrentNamer) {
85
86
            if (($newProposal = $concurrentNamer->propertyToColumnName($propertyName, $className)) != $defaultName) {
87
                return $newProposal;
88
            }
89
        }
90
91
        return $defaultName;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
98
    {
99
        $defaultName = $this->defaultNamer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName);
100
101
        /**
102
         * @var NamingStrategy $concurrentNamer
103
         */
104
        foreach ($this->concurrentNamers as $concurrentNamer) {
105
106
            if (($newProposal = $concurrentNamer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName)) != $defaultName) {
107
                return $newProposal;
108
            }
109
        }
110
111
        return $defaultName;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function referenceColumnName()
118
    {
119
        $defaultName = $this->defaultNamer->referenceColumnName();
120
121
        /**
122
         * @var NamingStrategy $concurrentNamer
123
         */
124
        foreach ($this->concurrentNamers as $concurrentNamer) {
125
126
            if (($newProposal = $concurrentNamer->referenceColumnName()) != $defaultName) {
127
                return $newProposal;
128
            }
129
        }
130
131
        return $defaultName;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function joinColumnName($propertyName/*, $className = null*/)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
138
    {
139
        $defaultName = $this->defaultNamer->joinColumnName($propertyName/*, $className */);
140
141
        /**
142
         * @var NamingStrategy $concurrentNamer
143
         */
144
        foreach ($this->concurrentNamers as $concurrentNamer) {
145
146
            if (($newProposal = $concurrentNamer->joinColumnName($propertyName/*, $className */)) != $defaultName) {
147
                return $newProposal;
148
            }
149
        }
150
151
        return $defaultName;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 2
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
158
    {
159 2
        switch ($this->concatenation) {
160 2 View Code Duplication
            case self::UCFIRST:
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...
161
                return
162
                    $this->classToTableName($sourceEntity) . ucfirst($this->classToTableName($targetEntity))
163
                    .
164
                    (($this->joinTableFieldSuffix && $propertyName) ? ucfirst($this->propertyToColumnName($propertyName, $sourceEntity)) : '')
0 ignored issues
show
Bug Best Practice introduced by
The expression $propertyName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
165
                    ;
166
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
167 2 View Code Duplication
            case self::NOTHING:
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...
168
                return
169
                    $this->classToTableName($sourceEntity) . $this->classToTableName($targetEntity)
170
                    .
171
                    (($this->joinTableFieldSuffix && $propertyName) ? $this->propertyToColumnName($propertyName, $sourceEntity) : '')
0 ignored issues
show
Bug Best Practice introduced by
The expression $propertyName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
172
                    ;
173
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
174 2
            case self::UNDERSCORE: // FALL TROUGH
175 1
            default:
176
                return
177 2
                    $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity)
178 1
                    .
179 2
                    (($this->joinTableFieldSuffix && $propertyName) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : '')
0 ignored issues
show
Bug Best Practice introduced by
The expression $propertyName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
180 1
                    ;
181
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
182
        }
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
189
    {
190
        switch ($this->concatenation) {
191
            case self::UCFIRST:
192
                return $this->classToTableName($entityName) . ucfirst(($referencedColumnName ?: $this->referenceColumnName()));
193
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
194
            case self::NOTHING:
195
                return $this->classToTableName($entityName) . ($referencedColumnName ?: $this->referenceColumnName());
196
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
197
            case self::UNDERSCORE: // FALL TROUGH
198
            default:
199
                return $this->classToTableName($entityName) . '_' . ($referencedColumnName ?: $this->referenceColumnName());
200
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
201
        }
202
    }
203
}
204