Completed
Push — master ( 44461e...067e0f )
by Nikola
04:28
created

NamerCollection::registerNamingStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of the Doctrine Naming Strategy Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 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
/**
15
 * Class NamerCollection
16
 *
17
 * @package RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy
18
 */
19
class NamerCollection implements NamingStrategy
20
{
21
    /**
22
     * Concatenate different naming strategy outputs with underscore character.
23
     */
24
    const UNDERSCORE = 'underscore';
25
26
    /**
27
     * Concatenate different naming strategy outputs without any character.
28
     */
29
    const NOTHING = 'nothing';
30
31
    /**
32
     * Concatenate different naming strategy outputs with camel casing.
33
     */
34
    const UCFIRST = 'ucfirst';
35
36
    /**
37
     * @var NamingStrategy
38
     */
39
    protected $defaultNamingStrategy;
40
41
    /**
42
     * @var NamingStrategy[]
43
     */
44
    protected $concurrentNamingStrategies;
45
46
    /**
47
     * @var string
48
     */
49
    protected $concatenation;
50
51
    /**
52
     * @var bool
53
     */
54
    protected $joinTableFieldSuffix;
55
56 3
    public function __construct(NamingStrategy $defaultNamingStrategy, array $concurrentNamingStrategies = [], array $configuration = [])
57
    {
58 3
        $this->defaultNamingStrategy = $defaultNamingStrategy;
59
60 3
        $this->concurrentNamingStrategies = [];
61
62 3
        foreach ($concurrentNamingStrategies as $namingStrategy) {
63 2
            $this->registerNamingStrategy($namingStrategy);
64
        }
65
66 3
        $configuration = array_merge([
67 3
            'concatenation' => self::UNDERSCORE,
68
            'joinTableFieldSuffix' => true
69
        ], $configuration);
70
71 3
        $this->concatenation = $configuration['concatenation'];
72 3
        $this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix'];
73 3
    }
74
75
    /**
76
     * Register naming strategy.
77
     *
78
     * @param NamingStrategy $namingStrategy
79
     *
80
     * @return NamerCollection $this
81
     */
82 3
    public function registerNamingStrategy(NamingStrategy $namingStrategy)
83
    {
84 3
        $this->concurrentNamingStrategies[] = $namingStrategy;
85 3
        return $this;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 3 View Code Duplication
    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...
92
    {
93 3
        $defaultName = $this->defaultNamingStrategy->classToTableName($className);
94
95
        /**
96
         * @var NamingStrategy $concurrentNamer
97
         */
98 3
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
99
100 3
            if (($newProposal = $concurrentNamer->classToTableName($className)) !== $defaultName) {
101 3
                return $newProposal;
102
            }
103
        }
104
105 1
        return $defaultName;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 View Code Duplication
    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...
112
    {
113
        $defaultName = $this->defaultNamingStrategy->propertyToColumnName($propertyName, $className);
114
115
        /**
116
         * @var NamingStrategy $concurrentNamer
117
         */
118
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
119
120
            if (($newProposal = $concurrentNamer->propertyToColumnName($propertyName, $className)) !== $defaultName) {
121
                return $newProposal;
122
            }
123
        }
124
125
        return $defaultName;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
132
    {
133
        $defaultName = $this->defaultNamingStrategy->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName);
134
135
        /**
136
         * @var NamingStrategy $concurrentNamer
137
         */
138
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
139
140
            if (($newProposal = $concurrentNamer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName)) != $defaultName) {
141
                return $newProposal;
142
            }
143
        }
144
145
        return $defaultName;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function referenceColumnName()
152
    {
153
        $defaultName = $this->defaultNamingStrategy->referenceColumnName();
154
155
        /**
156
         * @var NamingStrategy $concurrentNamer
157
         */
158
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
159
160
            if (($newProposal = $concurrentNamer->referenceColumnName()) != $defaultName) {
161
                return $newProposal;
162
            }
163
        }
164
165
        return $defaultName;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 View Code Duplication
    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...
172
    {
173
        $defaultName = $this->defaultNamingStrategy->joinColumnName($propertyName/*, $className */);
174
175
        /**
176
         * @var NamingStrategy $concurrentNamer
177
         */
178
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
179
180
            if (($newProposal = $concurrentNamer->joinColumnName($propertyName/*, $className */)) != $defaultName) {
181
                return $newProposal;
182
            }
183
        }
184
185
        return $defaultName;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 1
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
192
    {
193 1
        switch ($this->concatenation) {
194 1 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...
195
                return
196
                    $this->classToTableName($sourceEntity) . ucfirst($this->classToTableName($targetEntity))
197
                    .
198
                    (($this->joinTableFieldSuffix && !empty($propertyName)) ? ucfirst($this->propertyToColumnName($propertyName, $sourceEntity)) : '')
199
                    ;
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 1 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...
202
                return
203
                    $this->classToTableName($sourceEntity) . $this->classToTableName($targetEntity)
204
                    .
205
                    (($this->joinTableFieldSuffix && !empty($propertyName)) ? $this->propertyToColumnName($propertyName, $sourceEntity) : '')
206
                    ;
207
                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...
208 1
            case self::UNDERSCORE: // FALL TROUGH
209
            default:
210
                return
211 1
                    $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity)
212
                    .
213 1
                    (($this->joinTableFieldSuffix && !empty($propertyName)) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : '')
214
                    ;
215
                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...
216
        }
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
223
    {
224
        switch ($this->concatenation) {
225
            case self::UCFIRST:
226
                return $this->classToTableName($entityName) . ucfirst(($referencedColumnName ?: $this->referenceColumnName()));
227
                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...
228
            case self::NOTHING:
229
                return $this->classToTableName($entityName) . ($referencedColumnName ?: $this->referenceColumnName());
230
                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...
231
            case self::UNDERSCORE: // FALL TROUGH
232
            default:
233
                return $this->classToTableName($entityName) . '_' . ($referencedColumnName ?: $this->referenceColumnName());
234
                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...
235
        }
236
    }
237
}
238