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