| Conditions | 11 |
| Paths | 256 |
| Total Lines | 235 |
| Code Lines | 71 |
| Lines | 22 |
| Ratio | 9.36 % |
| Changes | 19 | ||
| Bugs | 1 | Features | 3 |
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 |
||
| 222 | public function generateDao($className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) { |
||
| 223 | $tableName = $table->getName(); |
||
| 224 | $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
||
| 225 | |||
| 226 | $defaultSort = null; |
||
| 227 | foreach ($table->getColumns() as $column) { |
||
| 228 | $comments = $column->getComment(); |
||
| 229 | $matches = array(); |
||
| 230 | if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0){ |
||
| 231 | $defaultSort = $data['column_name']; |
||
| 232 | if (count($matches == 3)){ |
||
| 233 | $defaultSortDirection = $matches[2]; |
||
| 234 | }else{ |
||
| 235 | $defaultSortDirection = 'ASC'; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | // FIXME: lowercase tables with _ in the name should work! |
||
| 241 | $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
||
| 242 | |||
| 243 | $beanClassWithoutNameSpace = $beanClassName; |
||
| 244 | $beanClassName = $beannamespace."\\".$beanClassName; |
||
| 245 | |||
| 246 | $str = "<?php |
||
| 247 | |||
| 248 | /* |
||
| 249 | * This file has been automatically generated by TDBM. |
||
| 250 | * DO NOT edit this file, as it might be overwritten. |
||
| 251 | * If you need to perform changes, edit the $className class instead! |
||
| 252 | */ |
||
| 253 | |||
| 254 | namespace {$daonamespace}; |
||
| 255 | |||
| 256 | use Mouf\\Database\\TDBM\\TDBMService; |
||
| 257 | use Mouf\\Database\\TDBM\\ResultIterator; |
||
| 258 | use Mouf\\Database\\TDBM\\ArrayIterator; |
||
| 259 | use $beanClassName; |
||
| 260 | |||
| 261 | /** |
||
| 262 | * The $baseClassName class will maintain the persistence of $beanClassWithoutNameSpace class into the $tableName table. |
||
| 263 | * |
||
| 264 | */ |
||
| 265 | class $baseClassName |
||
| 266 | { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @var TDBMService |
||
| 270 | */ |
||
| 271 | protected \$tdbmService; |
||
| 272 | |||
| 273 | /** |
||
| 274 | * The default sort column. |
||
| 275 | * |
||
| 276 | * @var string |
||
| 277 | */ |
||
| 278 | private \$defaultSort = ".($defaultSort ? "'$defaultSort'" : 'null')."; |
||
| 279 | |||
| 280 | /** |
||
| 281 | * The default sort direction. |
||
| 282 | * |
||
| 283 | * @var string |
||
| 284 | */ |
||
| 285 | private \$defaultDirection = ".($defaultSort && $defaultSortDirection ? "'$defaultSortDirection'" : "'asc'")."; |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Sets the TDBM service used by this DAO. |
||
| 289 | * |
||
| 290 | * @param TDBMService \$tdbmService |
||
| 291 | */ |
||
| 292 | public function __construct(TDBMService \$tdbmService) |
||
| 293 | { |
||
| 294 | \$this->tdbmService = \$tdbmService; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return a new instance of $beanClassWithoutNameSpace object, that will be persisted in database. |
||
| 299 | * |
||
| 300 | * @return $beanClassWithoutNameSpace |
||
| 301 | */// TODO! |
||
| 302 | /*public function create() |
||
| 303 | { |
||
| 304 | return \$this->tdbmService->getNewObject('$tableName', true); |
||
| 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() |
||
| 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 | $str .= " |
||
| 336 | /** |
||
| 337 | * Get $beanClassWithoutNameSpace specified by its ID (its primary key) |
||
| 338 | * If the primary key does not exist, an exception is thrown. |
||
| 339 | * |
||
| 340 | * @param string|int \$id |
||
| 341 | * @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. |
||
| 342 | * @return $beanClassWithoutNameSpace |
||
| 343 | * @throws TDBMException |
||
| 344 | */ |
||
| 345 | public function getById(\$id, \$lazyLoading = false) |
||
| 346 | { |
||
| 347 | return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading); |
||
| 348 | } |
||
| 349 | "; |
||
| 350 | } |
||
| 351 | $str .= " |
||
| 352 | /** |
||
| 353 | * Deletes the $beanClassWithoutNameSpace passed in parameter. |
||
| 354 | * |
||
| 355 | * @param $beanClassWithoutNameSpace \$obj object to delete |
||
| 356 | * @param bool \$cascade if true, it will delete all object linked to \$obj |
||
| 357 | */ |
||
| 358 | public function delete($beanClassWithoutNameSpace \$obj, \$cascade = false) |
||
| 359 | { |
||
| 360 | if (\$cascade === true) { |
||
| 361 | \$this->tdbmService->deleteCascade(\$obj); |
||
| 362 | } else { |
||
| 363 | \$this->tdbmService->delete(\$obj); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Get a list of $beanClassWithoutNameSpace specified by its filters. |
||
| 370 | * |
||
| 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 array \$additionalTablesFetch A list of additional tables to fetch (for performance improvement) |
||
| 375 | * @param string \$mode Either TDBMService::MODE_ARRAY or TDBMService::MODE_CURSOR (for large datasets). Defaults to TDBMService::MODE_ARRAY. |
||
| 376 | * @return {$beanClassWithoutNameSpace}[]|ResultIterator|ResultArray |
||
| 377 | */ |
||
| 378 | protected function find(\$filter=null, array \$parameters = [], \$orderby=null, array \$additionalTablesFetch = array(), \$mode = null) |
||
| 379 | { |
||
| 380 | if (\$this->defaultSort && \$orderby == null) { |
||
| 381 | \$orderby = '$tableName.'.\$this->defaultSort.' '.\$this->defaultDirection; |
||
| 382 | } |
||
| 383 | return \$this->tdbmService->findObjects('$tableName', \$filter, \$parameters, \$orderby, \$additionalTablesFetch, \$mode); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get a single $beanClassWithoutNameSpace specified by its filters. |
||
| 388 | * |
||
| 389 | * @param mixed \$filter The filter bag (see TDBMService::findObjects for complete description) |
||
| 390 | * @param array \$parameters The parameters associated with the filter |
||
| 391 | * @return $beanClassWithoutNameSpace |
||
| 392 | */ |
||
| 393 | protected function findOne(\$filter=null, array \$parameters = []) |
||
| 394 | { |
||
| 395 | return \$this->tdbmService->findObject('$tableName', \$filter, \$parameters); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Sets the default column for default sorting. |
||
| 400 | * |
||
| 401 | * @param string \$defaultSort |
||
| 402 | */ |
||
| 403 | public function setDefaultSort(\$defaultSort) |
||
| 404 | { |
||
| 405 | \$this->defaultSort = \$defaultSort; |
||
| 406 | } |
||
| 407 | "; |
||
| 408 | |||
| 409 | $str .= " |
||
| 410 | } |
||
| 411 | "; |
||
| 412 | |||
| 413 | $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace."\\".$baseClassName); |
||
| 414 | if (!$possibleBaseFileNames) { |
||
| 415 | // @codeCoverageIgnoreStart |
||
| 416 | throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
||
| 417 | // @codeCoverageIgnoreEnd |
||
| 418 | } |
||
| 419 | $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
||
| 420 | |||
| 421 | $this->ensureDirectoryExist($possibleBaseFileName); |
||
| 422 | file_put_contents($possibleBaseFileName ,$str); |
||
| 423 | @chmod($possibleBaseFileName, 0664); |
||
| 424 | |||
| 425 | $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace."\\".$className); |
||
| 426 | if (!$possibleFileNames) { |
||
| 427 | // @codeCoverageIgnoreStart |
||
| 428 | throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
||
| 429 | // @codeCoverageIgnoreEnd |
||
| 430 | } |
||
| 431 | $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
||
| 432 | |||
| 433 | // Now, let's generate the "editable" class |
||
| 434 | View Code Duplication | if (!file_exists($possibleFileName)) { |
|
| 435 | $str = "<?php |
||
| 436 | |||
| 437 | /* |
||
| 438 | * This file has been automatically generated by TDBM. |
||
| 439 | * You can edit this file as it will not be overwritten. |
||
| 440 | */ |
||
| 441 | |||
| 442 | namespace {$daonamespace}; |
||
| 443 | |||
| 444 | /** |
||
| 445 | * The $className class will maintain the persistence of $beanClassWithoutNameSpace class into the $tableName table. |
||
| 446 | */ |
||
| 447 | class $className extends $baseClassName |
||
| 448 | { |
||
| 449 | |||
| 450 | } |
||
| 451 | "; |
||
| 452 | $this->ensureDirectoryExist($possibleFileName); |
||
| 453 | file_put_contents($possibleFileName ,$str); |
||
| 454 | @chmod($possibleFileName, 0664); |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 654 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.