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