Conditions | 19 |
Paths | 4 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
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 |
||
42 | public function fillUserList(Facegame $facegame) |
||
43 | { |
||
44 | $hardcore = $facegame->getHardcore(); |
||
45 | $promo = $facegame->getPromo(); |
||
46 | $player = $facegame->getUser(); |
||
47 | $list = $facegame->getListUsers(); |
||
48 | |||
49 | if ($hardcore) { |
||
50 | $defaultTraits = ['department', 'promo', 'location', 'origin', 'nationality']; |
||
51 | $nbTraits = count($defaultTraits); |
||
52 | } |
||
53 | |||
54 | $users = $promo !== null ? $this->repository->findByPromo($promo) : $this->repository->findAll(); |
||
55 | |||
56 | $countUsers = count($users); |
||
57 | if ($countUsers < 5) { |
||
58 | return false; |
||
59 | } |
||
60 | |||
61 | // Gestion du nombre de questions possibles |
||
62 | $nbQuestions = min(10, $countUsers/2 - 1); |
||
63 | $nbPropositions = 3; |
||
64 | |||
65 | while (count($list) < $nbQuestions) { |
||
66 | $tempList = []; |
||
67 | $ids = []; |
||
68 | |||
69 | $tempList['firstPart'] = count($list) < $nbQuestions/2; |
||
70 | |||
71 | if ($hardcore) { |
||
72 | // Si la promo est déjà établie on ne va pas la demander comme carac |
||
73 | do { |
||
74 | $trait = $defaultTraits[rand(0, $nbTraits - 1)]; |
||
75 | } while ($promo !== null && $trait == 'promo'); |
||
76 | |||
77 | $tempList['trait'] = $trait; |
||
78 | $userTraits = []; |
||
79 | } |
||
80 | |||
81 | // La réponse est décidée aléatoirement |
||
82 | $tempList['answer'] = rand(0, $nbPropositions - 1); |
||
83 | |||
84 | for ($i = 0; $i < $nbPropositions; $i++) { |
||
85 | // On vérifie que l'user existe, qu'il a une image de profil, |
||
86 | // qu'on ne propose pas le nom de la personne ayant lancé le test |
||
87 | // et qu'on ne propose pas 2 fois la même caractéristique |
||
88 | do { |
||
89 | // On vérifie qu'on ne propose pas deux fois le même nom |
||
90 | do { |
||
91 | $tempId = rand(0, $countUsers - 1); |
||
92 | } while (in_array($tempId, $ids)); |
||
93 | |||
94 | $ids[] = $tempId; |
||
95 | $user = $users[$tempId]; |
||
96 | |||
97 | if ($hardcore) { |
||
98 | $method = 'get'.ucfirst($trait); |
||
99 | $tempTrait = $user->$method(); |
||
100 | } |
||
101 | } |
||
102 | while (!isset($user) |
||
103 | || $user->getImage() === null |
||
104 | || $user->getPromo() === null |
||
105 | || $user->getUsername() == $player->getUsername() |
||
106 | || $hardcore |
||
107 | && ($tempTrait === null || in_array($tempTrait, $userTraits, true)) |
||
108 | ); |
||
109 | |||
110 | $tempList[$i]['name'] = $user->getFirstName().' '.$user->getLastName(); |
||
111 | $tempList[$i]['picture'] = $user->getImage()->getWebPath(); |
||
112 | |||
113 | if ($hardcore) { |
||
114 | $userTraits[] = $tempTrait; |
||
115 | $tempList[$i]['trait'] = $tempTrait; |
||
116 | } |
||
117 | } |
||
118 | $list[] = $tempList; |
||
119 | } |
||
120 | $facegame->setListUsers($list); |
||
121 | return true; |
||
122 | } |
||
123 | |||
169 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.