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