Completed
Pull Request — 4.2 (#128)
by David
03:18
created

PivotTableMethodsDescriptor::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Mouf\Database\TDBM\Utils;
4
5
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
6
use Doctrine\DBAL\Schema\Table;
7
8
class PivotTableMethodsDescriptor implements MethodDescriptorInterface
9
{
10
    /**
11
     * @var Table
12
     */
13
    private $pivotTable;
14
15
    private $useAlternateName = false;
16
17
    /**
18
     * @var ForeignKeyConstraint
19
     */
20
    private $localFk;
21
22
    /**
23
     * @var ForeignKeyConstraint
24
     */
25
    private $remoteFk;
26
27
    /**
28
     * @param Table $pivotTable    The pivot table
29
     * @param Table $mainBeanTable The main bean table
0 ignored issues
show
Bug introduced by
There is no parameter named $mainBeanTable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
     */
31
    public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk)
32
    {
33
        $this->pivotTable = $pivotTable;
34
        $this->localFk = $localFk;
35
        $this->remoteFk = $remoteFk;
36
    }
37
38
    /**
39
     * Requests the use of an alternative name for this method.
40
     */
41
    public function useAlternativeName()
42
    {
43
        $this->useAlternateName = true;
44
    }
45
46
    /**
47
     * Returns the name of the method to be generated.
48
     *
49
     * @return string
50
     */
51
    public function getName() : string
52
    {
53
        if (!$this->useAlternateName) {
54
            return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
55
        } else {
56
            return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
57
        }
58
    }
59
60
    /**
61
     * Returns the plural name.
62
     *
63
     * @return string
64
     */
65 View Code Duplication
    private function getPluralName() : string
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...
66
    {
67
        if (!$this->useAlternateName) {
68
            return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
69
        } else {
70
            return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
71
        }
72
    }
73
74
    /**
75
     * Returns the singular name.
76
     *
77
     * @return string
78
     */
79 View Code Duplication
    private function getSingularName() : string
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...
80
    {
81
        if (!$this->useAlternateName) {
82
            return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName()));
83
        } else {
84
            return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
85
        }
86
    }
87
88
    /**
89
     * Returns the code of the method.
90
     *
91
     * @return string
92
     */
93
    public function getCode() : string
94
    {
95
        $singularName = $this->getSingularName();
96
        $pluralName = $this->getPluralName();
97
        $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
98
        $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
99
        $pluralVariableName = $variableName.'s';
100
101
        $str = '    /**
102
     * Returns the list of %s associated to this bean via the %s pivot table.
103
     *
104
     * @return %s[]
105
     */
106
    public function get%s()
107
    {
108
        return $this->_getRelationships(%s);
109
    }
110
';
111
112
        $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true));
113
114
        $str = '    /**
115
     * Adds a relationship with %s associated to this bean via the %s pivot table.
116
     *
117
     * @param %s %s
118
     */
119
    public function add%s(%s %s)
120
    {
121
        return $this->addRelationship(%s, %s);
122
    }
123
';
124
125
        $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
126
127
        $str = '    /**
128
     * Deletes the relationship with %s associated to this bean via the %s pivot table.
129
     *
130
     * @param %s %s
131
     */
132
    public function remove%s(%s %s)
133
    {
134
        return $this->_removeRelationship(%s, %s);
135
    }
136
';
137
138
        $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
139
140
        $str = '    /**
141
     * Returns whether this bean is associated with %s via the %s pivot table.
142
     *
143
     * @param %s %s
144
     * @return bool
145
     */
146
    public function has%s(%s %s) : bool
147
    {
148
        return $this->hasRelationship(%s, %s);
149
    }
150
';
151
152
        $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
153
154
        $str = '    /**
155
     * Sets all relationships with %s associated to this bean via the %s pivot table.
156
     * Exiting relationships will be removed and replaced by the provided relationships.
157
     *
158
     * @param %s[] %s
159
     */
160
    public function set%s(array %s)
161
    {
162
        return $this->setRelationships(%s, %s);
163
    }
164
';
165
166
        $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName);
167
168
        $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode;
169
170
        return $code;
171
    }
172
173
    /**
174
     * Returns an array of classes that needs a "use" for this method.
175
     *
176
     * @return string[]
177
     */
178
    public function getUsedClasses() : array
179
    {
180
        return [TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName())];
181
    }
182
183
    /**
184
     * Returns the code to past in jsonSerialize.
185
     *
186
     * @return string
187
     */
188
    public function getJsonSerializeCode() : string
189
    {
190
        $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
191
        $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
192
193
        return '        if (!$stopRecursion) {
194
            $array[\''.lcfirst($this->getPluralName()).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') {
195
                return '.$variableName.'->jsonSerialize(true);
196
            }, $this->'.$this->getName().'());
197
        }
198
';
199
    }
200
}
201