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