Conditions | 14 |
Paths | 155 |
Total Lines | 111 |
Code Lines | 62 |
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 |
||
76 | public function resultAction($project, $username) |
||
77 | { |
||
78 | $lh = $this->get('app.labs_helper'); |
||
79 | |||
80 | /** @var Project $project */ |
||
81 | $project = ProjectRepository::getProject($project, $this->container); |
||
|
|||
82 | |||
83 | if (!$project->exists()) { |
||
84 | $this->addFlash('notice', ['invalid-project', $project]); |
||
85 | return $this->redirectToRoute('SimpleEditCounter'); |
||
86 | } |
||
87 | |||
88 | $dbName = $project->getDatabaseName(); |
||
89 | $url = $project->getUrl(); |
||
90 | |||
91 | $userTable = $lh->getTable('user', $dbName); |
||
92 | $archiveTable = $lh->getTable('archive', $dbName); |
||
93 | $revisionTable = $lh->getTable('revision', $dbName); |
||
94 | $userGroupsTable = $lh->getTable('user_groups', $dbName); |
||
95 | |||
96 | /** @var Connection $conn */ |
||
97 | $conn = $this->get('doctrine')->getManager('replicas')->getConnection(); |
||
98 | |||
99 | // Prepare the query and execute |
||
100 | $resultQuery = $conn->prepare(" |
||
101 | SELECT 'id' AS source, user_id as value FROM $userTable WHERE user_name = :username |
||
102 | UNION |
||
103 | SELECT 'arch' AS source, COUNT(*) AS value FROM $archiveTable WHERE ar_user_text = :username |
||
104 | UNION |
||
105 | SELECT 'rev' AS source, COUNT(*) AS value FROM $revisionTable WHERE rev_user_text = :username |
||
106 | UNION |
||
107 | SELECT 'groups' AS source, ug_group AS value |
||
108 | FROM $userGroupsTable JOIN $userTable on user_id = ug_user WHERE user_name = :username |
||
109 | "); |
||
110 | |||
111 | $user = new User($username); |
||
112 | $usernameParam = $user->getUsername(); |
||
113 | $resultQuery->bindParam('username', $usernameParam); |
||
114 | $resultQuery->execute(); |
||
115 | |||
116 | if ($resultQuery->errorCode() > 0) { |
||
117 | $this->addFlash('notice', [ 'no-result', $username ]); |
||
118 | return $this->redirectToRoute('SimpleEditCounterProject', [ 'project' => $project->getDomain() ]); |
||
119 | } |
||
120 | |||
121 | // Fetch the result data |
||
122 | $results = $resultQuery->fetchAll(); |
||
123 | |||
124 | // Initialize the variables - just so we don't get variable undefined errors if there is a problem |
||
125 | $id = ''; |
||
126 | $arch = ''; |
||
127 | $rev = ''; |
||
128 | $groups = ''; |
||
129 | |||
130 | // Iterate over the results, putting them in the right variables |
||
131 | foreach ($results as $row) { |
||
132 | if ($row['source'] == 'id') { |
||
133 | $id = $row['value']; |
||
134 | } |
||
135 | if ($row['source'] == 'arch') { |
||
136 | $arch = $row['value']; |
||
137 | } |
||
138 | if ($row['source'] == 'rev') { |
||
139 | $rev = $row['value']; |
||
140 | } |
||
141 | if ($row['source'] == 'groups') { |
||
142 | $groups .= $row['value']. ', '; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | // Unknown user - If the user is created the $results variable will have 3 entries. |
||
147 | // This is a workaround to detect non-existent IPs. |
||
148 | if (count($results) < 3 && $arch == 0 && $rev == 0) { |
||
149 | $this->addFlash('notice', [ 'no-result', $username ]); |
||
150 | |||
151 | return $this->redirectToRoute('SimpleEditCounterProject', [ 'project' => $project->getDomain() ]); |
||
152 | } |
||
153 | |||
154 | // Remove the last comma and space |
||
155 | if (strlen($groups) > 2) { |
||
156 | $groups = substr($groups, 0, -2); |
||
157 | } |
||
158 | |||
159 | // If the user isn't in any groups, show a message. |
||
160 | if (strlen($groups) == 0) { |
||
161 | $groups = '---'; |
||
162 | } |
||
163 | |||
164 | $globalGroups = ''; |
||
165 | |||
166 | if (boolval($this->getParameter('app.single_wiki'))) { |
||
167 | // Retrieving the global groups, using the ApiHelper class |
||
168 | $api = $this->get('app.api_helper'); |
||
169 | $globalGroups = $api->globalGroups($url, $username); |
||
170 | } |
||
171 | |||
172 | // Assign the values and display the template |
||
173 | return $this->render('simpleEditCounter/result.html.twig', [ |
||
174 | 'xtPage' => 'sc', |
||
175 | 'xtTitle' => $username, |
||
176 | 'user' => $user, |
||
177 | 'project' => $project, |
||
178 | 'project_url' => $url, |
||
179 | 'id' => $id, |
||
180 | 'arch' => $arch, |
||
181 | 'rev' => $rev + $arch, |
||
182 | 'live' => $rev, |
||
183 | 'groups' => $groups, |
||
184 | 'globalGroups' => $globalGroups, |
||
185 | ]); |
||
186 | } |
||
187 | } |
||
188 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: