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

UnderscoredClassNamespacePrefix::joinTableName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
ccs 3
cts 3
cp 1
rs 9.4286
cc 3
eloc 4
nc 4
nop 3
crap 3
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\UnderscoreNamingStrategy;
13
14
class UnderscoredClassNamespacePrefix extends UnderscoreNamingStrategy
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $map;
20
21
    /**
22
     * @var array
23
     */
24
    protected $whitelist;
25
26
    /**
27
     * @var array
28
     */
29
    protected $blacklist;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $joinTableFieldSuffix;
35
36 16
    public function __construct(array $configuration = array())
37
    {
38 16
        $configuration = array_merge(array(
39 16
            'case' => CASE_LOWER,
40 8
            'map' => array(),
41 8
            'whitelist' => array(),
42 8
            'blacklist' => array(),
43
            'joinTableFieldSuffix' => true
44 8
        ), $configuration);
45
46 16
        if (count($configuration['whitelist']) > 0 && count($configuration['blacklist']) > 0) {
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...
47 2
            throw new \RuntimeException('You can use whitelist or blacklist or none of mentioned lists, but not booth.');
48
        }
49
50 14
        $this->map = $configuration['map'];
51 14
        $this->blacklist = $configuration['blacklist'];
52 14
        $this->whitelist = $configuration['whitelist'];
53 14
        $this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix'];
54
55 14
        parent::__construct($configuration['case']);
56
57 14
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 14
    public function classToTableName($className)
63
    {
64 14
        return (($prefix = $this->getTableNamePrefix($className)) ? $prefix . '_' : '') . parent::classToTableName($className);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 2 View Code Duplication
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = 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...
71
    {
72
        return
73 2
            parent::joinTableName($sourceEntity, $targetEntity, $propertyName)
74
            .
75 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...
76
    }
77
78
    /**
79
     * Find prefix for table from map.
80
     *
81
     * @param string $className
82
     * @return string
83
     */
84 14
    private function getTableNamePrefix($className)
85
    {
86 14
        foreach ($this->blacklist as $blacklistedFqcn) {
87
88 2
            if (strpos($className, $blacklistedFqcn) === 0) {
89 2
                return '';
90
            }
91 6
        }
92
93 12
        foreach ($this->map as $fqcnPrefix => $tablePrefix) {
94
95 12
            if (strpos($className, $fqcnPrefix) === 0) {
96
97 10
                if (count($this->whitelist) > 0) {
98
99
                    foreach ($this->whitelist as $whitelistedFqcn) {
100
101
                        if (strpos($className, $whitelistedFqcn) === 0) {
102
                            return $this->propertyToColumnName($tablePrefix);
103
                        }
104
                    }
105
106
                    return '';
107
108
                } else {
109
110 11
                    return $this->propertyToColumnName($tablePrefix);
111
                }
112
            }
113 3
        }
114
115 4
        return '';
116
    }
117
}
118