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