Conditions | 6 |
Paths | 32 |
Total Lines | 252 |
Code Lines | 72 |
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 |
||
205 | private function generateDao(BeanDescriptor $beanDescriptor, string $className, string $baseClassName, string $beanClassName, Table $table) |
||
206 | { |
||
207 | $daonamespace = $this->configuration->getDaoNamespace(); |
||
208 | $beannamespace = $this->configuration->getBeanNamespace(); |
||
209 | $tableName = $table->getName(); |
||
210 | $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
||
211 | |||
212 | list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
||
213 | |||
214 | // FIXME: lowercase tables with _ in the name should work! |
||
215 | $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
||
216 | |||
217 | $beanClassWithoutNameSpace = $beanClassName; |
||
218 | $beanClassName = $beannamespace.'\\'.$beanClassName; |
||
219 | |||
220 | list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace); |
||
221 | |||
222 | $usedBeans[] = $beanClassName; |
||
223 | // Let's suppress duplicates in used beans (if any) |
||
224 | $usedBeans = array_flip(array_flip($usedBeans)); |
||
225 | $useStatements = array_map(function ($usedBean) { |
||
226 | return "use $usedBean;\n"; |
||
227 | }, $usedBeans); |
||
228 | |||
229 | $str = "<?php |
||
230 | |||
231 | /* |
||
232 | * This file has been automatically generated by TDBM. |
||
233 | * DO NOT edit this file, as it might be overwritten. |
||
234 | * If you need to perform changes, edit the $className class instead! |
||
235 | */ |
||
236 | |||
237 | namespace {$daonamespace}\\Generated; |
||
238 | |||
239 | use Mouf\\Database\\TDBM\\TDBMService; |
||
240 | use Mouf\\Database\\TDBM\\ResultIterator; |
||
241 | use Mouf\\Database\\TDBM\\ArrayIterator; |
||
242 | ".implode('', $useStatements)." |
||
243 | /** |
||
244 | * The $baseClassName class will maintain the persistence of $beanClassWithoutNameSpace class into the $tableName table. |
||
245 | * |
||
246 | */ |
||
247 | class $baseClassName |
||
248 | { |
||
249 | |||
250 | /** |
||
251 | * @var TDBMService |
||
252 | */ |
||
253 | protected \$tdbmService; |
||
254 | |||
255 | /** |
||
256 | * The default sort column. |
||
257 | * |
||
258 | * @var string |
||
259 | */ |
||
260 | private \$defaultSort = ".($defaultSort ? "'$defaultSort'" : 'null').'; |
||
261 | |||
262 | /** |
||
263 | * The default sort direction. |
||
264 | * |
||
265 | * @var string |
||
266 | */ |
||
267 | private $defaultDirection = '.($defaultSort && $defaultSortDirection ? "'$defaultSortDirection'" : "'asc'")."; |
||
268 | |||
269 | /** |
||
270 | * Sets the TDBM service used by this DAO. |
||
271 | * |
||
272 | * @param TDBMService \$tdbmService |
||
273 | */ |
||
274 | public function __construct(TDBMService \$tdbmService) |
||
275 | { |
||
276 | \$this->tdbmService = \$tdbmService; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Persist the $beanClassWithoutNameSpace instance. |
||
281 | * |
||
282 | * @param $beanClassWithoutNameSpace \$obj The bean to save. |
||
283 | */ |
||
284 | public function save($beanClassWithoutNameSpace \$obj) |
||
285 | { |
||
286 | \$this->tdbmService->save(\$obj); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Get all $tableCamel records. |
||
291 | * |
||
292 | * @return {$beanClassWithoutNameSpace}[]|ResultIterator|ResultArray |
||
293 | */ |
||
294 | public function findAll() : iterable |
||
295 | { |
||
296 | if (\$this->defaultSort) { |
||
297 | \$orderBy = '$tableName.'.\$this->defaultSort.' '.\$this->defaultDirection; |
||
298 | } else { |
||
299 | \$orderBy = null; |
||
300 | } |
||
301 | return \$this->tdbmService->findObjects('$tableName', null, [], \$orderBy); |
||
302 | } |
||
303 | "; |
||
304 | |||
305 | if (count($primaryKeyColumns) === 1) { |
||
306 | $primaryKeyColumn = $primaryKeyColumns[0]; |
||
307 | $primaryKeyPhpType = self::dbalTypeToPhpType($table->getColumn($primaryKeyColumn)->getType()); |
||
308 | $str .= " |
||
309 | /** |
||
310 | * Get $beanClassWithoutNameSpace specified by its ID (its primary key) |
||
311 | * If the primary key does not exist, an exception is thrown. |
||
312 | * |
||
313 | * @param string|int \$id |
||
314 | * @param bool \$lazyLoading If set to true, the object will not be loaded right away. Instead, it will be loaded when you first try to access a method of the object. |
||
315 | * @return $beanClassWithoutNameSpace |
||
316 | * @throws TDBMException |
||
317 | */ |
||
318 | public function getById($primaryKeyPhpType \$id, \$lazyLoading = false) : $beanClassWithoutNameSpace |
||
319 | { |
||
320 | return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading); |
||
321 | } |
||
322 | "; |
||
323 | } |
||
324 | $str .= " |
||
325 | /** |
||
326 | * Deletes the $beanClassWithoutNameSpace passed in parameter. |
||
327 | * |
||
328 | * @param $beanClassWithoutNameSpace \$obj object to delete |
||
329 | * @param bool \$cascade if true, it will delete all object linked to \$obj |
||
330 | */ |
||
331 | public function delete($beanClassWithoutNameSpace \$obj, \$cascade = false) : void |
||
332 | { |
||
333 | if (\$cascade === true) { |
||
334 | \$this->tdbmService->deleteCascade(\$obj); |
||
335 | } else { |
||
336 | \$this->tdbmService->delete(\$obj); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | |||
341 | /** |
||
342 | * Get a list of $beanClassWithoutNameSpace specified by its filters. |
||
343 | * |
||
344 | * @param mixed \$filter The filter bag (see TDBMService::findObjects for complete description) |
||
345 | * @param array \$parameters The parameters associated with the filter |
||
346 | * @param mixed \$orderBy The order string |
||
347 | * @param array \$additionalTablesFetch A list of additional tables to fetch (for performance improvement) |
||
348 | * @param int \$mode Either TDBMService::MODE_ARRAY or TDBMService::MODE_CURSOR (for large datasets). Defaults to TDBMService::MODE_ARRAY. |
||
349 | * @return {$beanClassWithoutNameSpace}[]|ResultIterator|ResultArray |
||
350 | */ |
||
351 | protected function find(\$filter = null, array \$parameters = [], \$orderBy=null, array \$additionalTablesFetch = [], \$mode = null) : iterable |
||
352 | { |
||
353 | if (\$this->defaultSort && \$orderBy == null) { |
||
354 | \$orderBy = '$tableName.'.\$this->defaultSort.' '.\$this->defaultDirection; |
||
355 | } |
||
356 | return \$this->tdbmService->findObjects('$tableName', \$filter, \$parameters, \$orderBy, \$additionalTablesFetch, \$mode); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Get a list of $beanClassWithoutNameSpace specified by its filters. |
||
361 | * Unlike the `find` method that guesses the FROM part of the statement, here you can pass the \$from part. |
||
362 | * |
||
363 | * You should not put an alias on the main table name. So your \$from variable should look like: |
||
364 | * |
||
365 | * \"$tableName JOIN ... ON ...\" |
||
366 | * |
||
367 | * @param string \$from The sql from statement |
||
368 | * @param mixed \$filter The filter bag (see TDBMService::findObjects for complete description) |
||
369 | * @param array \$parameters The parameters associated with the filter |
||
370 | * @param mixed \$orderBy The order string |
||
371 | * @param int \$mode Either TDBMService::MODE_ARRAY or TDBMService::MODE_CURSOR (for large datasets). Defaults to TDBMService::MODE_ARRAY. |
||
372 | * @return {$beanClassWithoutNameSpace}[]|ResultIterator|ResultArray |
||
373 | */ |
||
374 | protected function findFromSql(\$from, \$filter = null, array \$parameters = [], \$orderBy = null, \$mode = null) : iterable |
||
375 | { |
||
376 | if (\$this->defaultSort && \$orderBy == null) { |
||
377 | \$orderBy = '$tableName.'.\$this->defaultSort.' '.\$this->defaultDirection; |
||
378 | } |
||
379 | return \$this->tdbmService->findObjectsFromSql('$tableName', \$from, \$filter, \$parameters, \$orderBy, \$mode); |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Get a single $beanClassWithoutNameSpace specified by its filters. |
||
384 | * |
||
385 | * @param mixed \$filter The filter bag (see TDBMService::findObjects for complete description) |
||
386 | * @param array \$parameters The parameters associated with the filter |
||
387 | * @param array \$additionalTablesFetch A list of additional tables to fetch (for performance improvement) |
||
388 | * @return $beanClassWithoutNameSpace|null |
||
389 | */ |
||
390 | protected function findOne(\$filter = null, array \$parameters = [], array \$additionalTablesFetch = []) : ?$beanClassWithoutNameSpace |
||
391 | { |
||
392 | return \$this->tdbmService->findObject('$tableName', \$filter, \$parameters, \$additionalTablesFetch); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Get a single $beanClassWithoutNameSpace specified by its filters. |
||
397 | * Unlike the `find` method that guesses the FROM part of the statement, here you can pass the \$from part. |
||
398 | * |
||
399 | * You should not put an alias on the main table name. So your \$from variable should look like: |
||
400 | * |
||
401 | * \"$tableName JOIN ... ON ...\" |
||
402 | * |
||
403 | * @param string \$from The sql from statement |
||
404 | * @param mixed \$filter The filter bag (see TDBMService::findObjects for complete description) |
||
405 | * @param array \$parameters The parameters associated with the filter |
||
406 | * @return $beanClassWithoutNameSpace|null |
||
407 | */ |
||
408 | protected function findOneFromSql(\$from, \$filter = null, array \$parameters = []) : ?$beanClassWithoutNameSpace |
||
409 | { |
||
410 | return \$this->tdbmService->findObjectFromSql('$tableName', \$from, \$filter, \$parameters); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Sets the default column for default sorting. |
||
415 | * |
||
416 | * @param string \$defaultSort |
||
417 | */ |
||
418 | public function setDefaultSort(string \$defaultSort) : void |
||
419 | { |
||
420 | \$this->defaultSort = \$defaultSort; |
||
421 | } |
||
422 | "; |
||
423 | |||
424 | $str .= $findByDaoCode; |
||
425 | $str .= '} |
||
426 | '; |
||
427 | |||
428 | $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\Generated\\'.$baseClassName)->getPathname(); |
||
429 | |||
430 | $this->dumpFile($possibleBaseFileName, $str); |
||
431 | |||
432 | $possibleFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\'.$className)->getPathname(); |
||
433 | |||
434 | // Now, let's generate the "editable" class |
||
435 | if (!file_exists($possibleFileName)) { |
||
436 | $str = "<?php |
||
437 | |||
438 | /* |
||
439 | * This file has been automatically generated by TDBM. |
||
440 | * You can edit this file as it will not be overwritten. |
||
441 | */ |
||
442 | |||
443 | namespace {$daonamespace}; |
||
444 | |||
445 | use {$daonamespace}\\Generated\\{$baseClassName}; |
||
446 | |||
447 | /** |
||
448 | * The $className class will maintain the persistence of $beanClassWithoutNameSpace class into the $tableName table. |
||
449 | */ |
||
450 | class $className extends $baseClassName |
||
451 | { |
||
452 | } |
||
453 | "; |
||
454 | $this->dumpFile($possibleFileName, $str); |
||
455 | } |
||
456 | } |
||
457 | |||
653 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: