Failed Conditions
Pull Request — master (#7724)
by
unknown
09:25
created

DefaultNamingStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping\Factory;
6
7
use Doctrine\Common\Inflector\Inflector;
8
use function strpos;
9
use function strrpos;
10
use function strtolower;
11
use function substr;
12
13
/**
14
 * The default NamingStrategy
15
 */
16
class DefaultNamingStrategy implements NamingStrategy
17
{
0 ignored issues
show
introduced by
There must be exactly 0 empty lines after class opening brace.
Loading history...
18
19
    /** @var bool */
20
    private $plural;
21
22
    /**
23
     * Underscore naming strategy construct.
24
     *
25
     * @param bool $plural
26
     */
27 2143
    public function __construct($plural = false)
28
    {
29 2143
        $this->plural = $plural;
30 2143
    }
31
32
    /**
33
     * @return bool
34
     */
35
    public function isPlural()
36
    {
37
        return $this->plural;
38
    }
39
40
    /**
41
     * Set naming as plural
42
     * Converts 'MyEntity' to 'MyEntities'
43
     *
44
     * @param bool $plural
45
     */
46
    public function setPlural($plural)
47
    {
48
        $this->plural = $plural;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 225
    public function classToTableName(string $className) : string
55
    {
56 225
        return $this->_classToTableName($className, $this->plural);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 341
    public function propertyToColumnName(string $propertyName, ?string $className = null) : string
63
    {
64 341
        return $propertyName;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function embeddedFieldToColumnName(
71
        string $propertyName,
72
        string $embeddedColumnName,
73
        ?string $className = null,
74
        ?string $embeddedClassName = null
75
    ) : string {
76
        return $propertyName . '_' . $embeddedColumnName;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 268
    public function referenceColumnName() : string
83
    {
84 268
        return 'id';
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 263
    public function joinColumnName(string $propertyName, ?string $className = null) : string
91
    {
92 263
        return $propertyName . '_' . $this->referenceColumnName();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 95
    public function joinTableName(string $sourceEntity, string $targetEntity, ?string $propertyName = null) : string
99
    {
100 95
        return strtolower($this->_classToTableName($sourceEntity) . '_' .
101 95
            $this->_classToTableName($targetEntity));
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 15
    public function joinKeyColumnName(string $entityName, ?string $referencedColumnName = null) : string
108
    {
109 15
        return strtolower(
110 15
            $this->_classToTableName($entityName) . '_' . ($referencedColumnName ?: $this->referenceColumnName())
111
        );
112
    }
113
114 298
    private function _classToTableName(string $className, bool $pluralize = false) : string
115
    {
116 298
        if (strpos($className, '\\') !== false) {
117 293
            $className = substr($className, strrpos($className, '\\') + 1);
118
        }
119
120 298
        if ($pluralize) {
121 3
            $className = Inflector::pluralize($className);
122
        }
123
124 298
        return $className;
125
    }
126
}
127