Conditions | 1 |
Paths | 1 |
Total Lines | 79 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
94 | public function getCode() : string |
||
95 | { |
||
96 | $singularName = $this->getSingularName(); |
||
97 | $pluralName = $this->getPluralName(); |
||
98 | $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName()); |
||
99 | $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
||
100 | $pluralVariableName = $variableName.'s'; |
||
101 | |||
102 | $str = ' /** |
||
103 | * Returns the list of %s associated to this bean via the %s pivot table. |
||
104 | * |
||
105 | * @return %s[] |
||
106 | */ |
||
107 | public function get%s() |
||
108 | { |
||
109 | return $this->_getRelationships(%s); |
||
110 | } |
||
111 | '; |
||
112 | |||
113 | $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
||
114 | |||
115 | $str = ' /** |
||
116 | * Adds a relationship with %s associated to this bean via the %s pivot table. |
||
117 | * |
||
118 | * @param %s %s |
||
119 | */ |
||
120 | public function add%s(%s %s) |
||
121 | { |
||
122 | return $this->addRelationship(%s, %s); |
||
123 | } |
||
124 | '; |
||
125 | |||
126 | $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
||
127 | |||
128 | $str = ' /** |
||
129 | * Deletes the relationship with %s associated to this bean via the %s pivot table. |
||
130 | * |
||
131 | * @param %s %s |
||
132 | */ |
||
133 | public function remove%s(%s %s) |
||
134 | { |
||
135 | return $this->_removeRelationship(%s, %s); |
||
136 | } |
||
137 | '; |
||
138 | |||
139 | $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
||
140 | |||
141 | $str = ' /** |
||
142 | * Returns whether this bean is associated with %s via the %s pivot table. |
||
143 | * |
||
144 | * @param %s %s |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function has%s(%s %s) : bool |
||
148 | { |
||
149 | return $this->hasRelationship(%s, %s); |
||
150 | } |
||
151 | '; |
||
152 | |||
153 | $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
||
154 | |||
155 | $str = ' /** |
||
156 | * Sets all relationships with %s associated to this bean via the %s pivot table. |
||
157 | * Exiting relationships will be removed and replaced by the provided relationships. |
||
158 | * |
||
159 | * @param %s[] %s |
||
160 | */ |
||
161 | public function set%s(array %s) |
||
162 | { |
||
163 | return $this->setRelationships(%s, %s); |
||
164 | } |
||
165 | '; |
||
166 | |||
167 | $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
||
168 | |||
169 | $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
||
170 | |||
171 | return $code; |
||
172 | } |
||
173 | |||
202 |
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.