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