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