Conditions | 8 |
Paths | 64 |
Total Lines | 223 |
Code Lines | 66 |
Lines | 22 |
Ratio | 9.87 % |
Changes | 24 | ||
Bugs | 1 | Features | 4 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
276 | public function generateDao(BeanDescriptor $beanDescriptor, $className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) |
||
277 | { |
||
278 | $tableName = $table->getName(); |
||
279 | $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
||
280 | |||
281 | list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
||
282 | |||
283 | // FIXME: lowercase tables with _ in the name should work! |
||
284 | $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
||
285 | |||
286 | $beanClassWithoutNameSpace = $beanClassName; |
||
287 | $beanClassName = $beannamespace.'\\'.$beanClassName; |
||
288 | |||
289 | list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace); |
||
290 | |||
291 | $usedBeans[] = $beanClassName; |
||
292 | // Let's suppress duplicates in used beans (if any) |
||
293 | $usedBeans = array_flip(array_flip($usedBeans)); |
||
294 | $useStatements = array_map(function ($usedBean) { |
||
295 | return "use $usedBean;\n"; |
||
296 | }, $usedBeans); |
||
297 | |||
298 | $str = "<?php |
||
299 | |||
300 | /* |
||
301 | * This file has been automatically generated by TDBM. |
||
302 | * DO NOT edit this file, as it might be overwritten. |
||
303 | * If you need to perform changes, edit the $className class instead! |
||
304 | */ |
||
305 | |||
306 | namespace {$daonamespace}; |
||
307 | |||
308 | use Mouf\\Database\\TDBM\\TDBMService; |
||
309 | use Mouf\\Database\\TDBM\\ResultIterator; |
||
310 | use Mouf\\Database\\TDBM\\ArrayIterator; |
||
311 | ".implode('', $useStatements)." |
||
312 | |||
313 | /** |
||
314 | * The $baseClassName class will maintain the persistence of $beanClassWithoutNameSpace class into the $tableName table. |
||
315 | * |
||
316 | */ |
||
317 | class $baseClassName |
||
318 | { |
||
319 | |||
320 | /** |
||
321 | * @var TDBMService |
||
322 | */ |
||
323 | protected \$tdbmService; |
||
324 | |||
325 | /** |
||
326 | * The default sort column. |
||
327 | * |
||
328 | * @var string |
||
329 | */ |
||
330 | private \$defaultSort = ".($defaultSort ? "'$defaultSort'" : 'null').'; |
||
331 | |||
332 | /** |
||
333 | * The default sort direction. |
||
334 | * |
||
335 | * @var string |
||
336 | */ |
||
337 | private $defaultDirection = '.($defaultSort && $defaultSortDirection ? "'$defaultSortDirection'" : "'asc'")."; |
||
338 | |||
339 | /** |
||
340 | * Sets the TDBM service used by this DAO. |
||
341 | * |
||
342 | * @param TDBMService \$tdbmService |
||
343 | */ |
||
344 | public function __construct(TDBMService \$tdbmService) |
||
345 | { |
||
346 | \$this->tdbmService = \$tdbmService; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Persist the $beanClassWithoutNameSpace instance. |
||
351 | * |
||
352 | * @param $beanClassWithoutNameSpace \$obj The bean to save. |
||
353 | */ |
||
354 | public function save($beanClassWithoutNameSpace \$obj) |
||
355 | { |
||
356 | \$this->tdbmService->save(\$obj); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Get all $tableCamel records. |
||
361 | * |
||
362 | * @return {$beanClassWithoutNameSpace}[]|ResultIterator|ResultArray |
||
363 | */ |
||
364 | public function findAll() |
||
365 | { |
||
366 | if (\$this->defaultSort) { |
||
367 | \$orderBy = '$tableName.'.\$this->defaultSort.' '.\$this->defaultDirection; |
||
368 | } else { |
||
369 | \$orderBy = null; |
||
370 | } |
||
371 | return \$this->tdbmService->findObjects('$tableName', null, [], \$orderBy); |
||
372 | } |
||
373 | "; |
||
374 | |||
375 | if (count($primaryKeyColumns) === 1) { |
||
376 | $primaryKeyColumn = $primaryKeyColumns[0]; |
||
377 | $str .= " |
||
378 | /** |
||
379 | * Get $beanClassWithoutNameSpace specified by its ID (its primary key) |
||
380 | * If the primary key does not exist, an exception is thrown. |
||
381 | * |
||
382 | * @param string|int \$id |
||
383 | * @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. |
||
384 | * @return $beanClassWithoutNameSpace |
||
385 | * @throws TDBMException |
||
386 | */ |
||
387 | public function getById(\$id, \$lazyLoading = false) |
||
388 | { |
||
389 | return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading); |
||
390 | } |
||
391 | "; |
||
392 | } |
||
393 | $str .= " |
||
394 | /** |
||
395 | * Deletes the $beanClassWithoutNameSpace passed in parameter. |
||
396 | * |
||
397 | * @param $beanClassWithoutNameSpace \$obj object to delete |
||
398 | * @param bool \$cascade if true, it will delete all object linked to \$obj |
||
399 | */ |
||
400 | public function delete($beanClassWithoutNameSpace \$obj, \$cascade = false) |
||
401 | { |
||
402 | if (\$cascade === true) { |
||
403 | \$this->tdbmService->deleteCascade(\$obj); |
||
404 | } else { |
||
405 | \$this->tdbmService->delete(\$obj); |
||
406 | } |
||
407 | } |
||
408 | |||
409 | |||
410 | /** |
||
411 | * Get a list of $beanClassWithoutNameSpace specified by its filters. |
||
412 | * |
||
413 | * @param mixed \$filter The filter bag (see TDBMService::findObjects for complete description) |
||
414 | * @param array \$parameters The parameters associated with the filter |
||
415 | * @param mixed \$orderBy The order string |
||
416 | * @param array \$additionalTablesFetch A list of additional tables to fetch (for performance improvement) |
||
417 | * @param int \$mode Either TDBMService::MODE_ARRAY or TDBMService::MODE_CURSOR (for large datasets). Defaults to TDBMService::MODE_ARRAY. |
||
418 | * @return {$beanClassWithoutNameSpace}[]|ResultIterator|ResultArray |
||
419 | */ |
||
420 | protected function find(\$filter = null, array \$parameters = [], \$orderBy=null, array \$additionalTablesFetch = [], \$mode = null) |
||
421 | { |
||
422 | if (\$this->defaultSort && \$orderBy == null) { |
||
423 | \$orderBy = '$tableName.'.\$this->defaultSort.' '.\$this->defaultDirection; |
||
424 | } |
||
425 | return \$this->tdbmService->findObjects('$tableName', \$filter, \$parameters, \$orderBy, \$additionalTablesFetch, \$mode); |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Get a single $beanClassWithoutNameSpace specified by its filters. |
||
430 | * |
||
431 | * @param mixed \$filter The filter bag (see TDBMService::findObjects for complete description) |
||
432 | * @param array \$parameters The parameters associated with the filter |
||
433 | * @return $beanClassWithoutNameSpace |
||
434 | */ |
||
435 | protected function findOne(\$filter=null, array \$parameters = []) |
||
436 | { |
||
437 | return \$this->tdbmService->findObject('$tableName', \$filter, \$parameters); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Sets the default column for default sorting. |
||
442 | * |
||
443 | * @param string \$defaultSort |
||
444 | */ |
||
445 | public function setDefaultSort(\$defaultSort) |
||
446 | { |
||
447 | \$this->defaultSort = \$defaultSort; |
||
448 | } |
||
449 | "; |
||
450 | |||
451 | $str .= $findByDaoCode; |
||
452 | $str .= '} |
||
453 | '; |
||
454 | |||
455 | $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$baseClassName); |
||
456 | if (empty($possibleBaseFileNames)) { |
||
457 | // @codeCoverageIgnoreStart |
||
458 | throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
||
459 | // @codeCoverageIgnoreEnd |
||
460 | } |
||
461 | $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
||
462 | |||
463 | $this->ensureDirectoryExist($possibleBaseFileName); |
||
464 | file_put_contents($possibleBaseFileName, $str); |
||
465 | @chmod($possibleBaseFileName, 0664); |
||
466 | |||
467 | $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$className); |
||
468 | if (empty($possibleFileNames)) { |
||
469 | // @codeCoverageIgnoreStart |
||
470 | throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
||
471 | // @codeCoverageIgnoreEnd |
||
472 | } |
||
473 | $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
||
474 | |||
475 | // Now, let's generate the "editable" class |
||
476 | View Code Duplication | if (!file_exists($possibleFileName)) { |
|
477 | $str = "<?php |
||
478 | |||
479 | /* |
||
480 | * This file has been automatically generated by TDBM. |
||
481 | * You can edit this file as it will not be overwritten. |
||
482 | */ |
||
483 | |||
484 | namespace {$daonamespace}; |
||
485 | |||
486 | /** |
||
487 | * The $className class will maintain the persistence of $beanClassWithoutNameSpace class into the $tableName table. |
||
488 | */ |
||
489 | class $className extends $baseClassName |
||
490 | { |
||
491 | |||
492 | } |
||
493 | "; |
||
494 | $this->ensureDirectoryExist($possibleFileName); |
||
495 | file_put_contents($possibleFileName, $str); |
||
496 | @chmod($possibleFileName, 0664); |
||
497 | } |
||
498 | } |
||
499 | |||
711 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.