Conditions | 7 |
Paths | 15 |
Total Lines | 145 |
Code Lines | 83 |
Lines | 8 |
Ratio | 5.52 % |
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 |
||
79 | public function resultAction($project, $username) |
||
80 | { |
||
81 | $lh = $this->get("app.labs_helper"); |
||
82 | |||
83 | $projectData = ProjectRepository::getProject($project, $this->container); |
||
84 | |||
85 | View Code Duplication | if (!$projectData->exists()) { |
|
86 | $this->addFlash("notice", ["invalid-project", $project]); |
||
87 | return $this->redirectToRoute("adminscore"); |
||
88 | } |
||
89 | |||
90 | $dbName = $projectData->getDatabaseName(); |
||
91 | $wikiName = $projectData->getDatabaseName(); |
||
92 | $url = $projectData->getUrl(); |
||
93 | |||
94 | $userTable = $lh->getTable("user", $dbName); |
||
95 | $pageTable = $lh->getTable("page", $dbName); |
||
96 | $loggingTable = $lh->getTable("logging", $dbName, "userindex"); |
||
97 | $revisionTable = $lh->getTable("revision", $dbName); |
||
98 | $archiveTable = $lh->getTable("archive", $dbName); |
||
99 | |||
100 | // MULTIPLIERS (to review) |
||
101 | $ACCT_AGE_MULT = 1.25; # 0 if = 365 jours |
||
102 | $EDIT_COUNT_MULT = 1.25; # 0 if = 10 000 |
||
1 ignored issue
–
show
|
|||
103 | $USER_PAGE_MULT = 0.1; # 0 if = |
||
104 | $PATROLS_MULT = 1; # 0 if = |
||
105 | $BLOCKS_MULT = 1.4; # 0 if = 10 |
||
1 ignored issue
–
show
|
|||
106 | $AFD_MULT = 1.15; |
||
107 | $RECENT_ACTIVITY_MULT = 0.9; # 0 if = |
||
108 | $AIV_MULT = 1.15; |
||
109 | $EDIT_SUMMARIES_MULT = 0.8; # 0 if = |
||
110 | $NAMESPACES_MULT = 1.0; # 0 if = |
||
111 | $PAGES_CREATED_LIVE_MULT = 1.4; # 0 if = |
||
112 | $PAGES_CREATED_ARCHIVE_MULT = 1.4; # 0 if = |
||
113 | $RPP_MULT = 1.15; # 0 if = |
||
114 | $USERRIGHTS_MULT = 0.75; # 0 if = |
||
115 | |||
116 | // Grab the connection to the replica database (which is separate from the above) |
||
117 | $conn = $this->get('doctrine')->getManager("replicas")->getConnection(); |
||
118 | |||
119 | // Prepare the query and execute |
||
120 | $resultQuery = $conn->prepare(" |
||
121 | SELECT 'id' AS source, user_id AS value FROM $userTable |
||
122 | WHERE user_name = :username |
||
123 | UNION |
||
124 | SELECT 'account-age' AS source, user_registration AS value FROM $userTable |
||
125 | WHERE user_name=:username |
||
126 | UNION |
||
127 | SELECT 'edit-count' AS source, user_editcount AS value FROM $userTable |
||
128 | WHERE user_name=:username |
||
129 | UNION |
||
130 | SELECT 'user-page' AS source, page_len AS value FROM $pageTable |
||
131 | WHERE page_namespace=2 AND page_title=:username |
||
132 | UNION |
||
133 | SELECT 'patrols' AS source, COUNT(*) AS value FROM $loggingTable |
||
134 | WHERE log_type='patrol' |
||
135 | AND log_action='patrol' |
||
136 | AND log_namespace=0 |
||
137 | AND log_deleted=0 AND log_user_text=:username |
||
138 | UNION |
||
139 | SELECT 'blocks' AS source, COUNT(*) AS value FROM $loggingTable l |
||
140 | INNER JOIN $userTable u ON l.log_user = u.user_id |
||
141 | WHERE l.log_type='block' AND l.log_action='block' |
||
142 | AND l.log_namespace=2 AND l.log_deleted=0 AND u.user_name=:username |
||
143 | UNION |
||
144 | SELECT 'afd' AS source, COUNT(*) AS value FROM $revisionTable |
||
145 | WHERE rev_page LIKE 'Articles for deletion/%' |
||
146 | AND rev_page NOT LIKE 'Articles_for_deletion/Log/%' |
||
147 | AND rev_user_text=:username |
||
148 | UNION |
||
149 | SELECT 'recent-activity' AS source, COUNT(*) AS value FROM $revisionTable |
||
150 | WHERE rev_user_text=:username AND rev_timestamp > (now()-INTERVAL 730 day) AND rev_timestamp < now() |
||
151 | UNION |
||
152 | SELECT 'aiv' AS source, COUNT(*) AS value FROM $revisionTable |
||
153 | WHERE rev_page LIKE 'Administrator intervention against vandalism%' AND rev_user_text=:username |
||
154 | UNION |
||
155 | SELECT 'edit-summaries' AS source, COUNT(*) AS value FROM $revisionTable JOIN $pageTable ON rev_page=page_id |
||
156 | WHERE page_namespace=0 AND rev_user_text=:username |
||
157 | UNION |
||
158 | SELECT 'namespaces' AS source, count(*) AS value FROM $revisionTable JOIN $pageTable ON rev_page=page_id |
||
159 | WHERE rev_user_text=:username AND page_namespace=0 |
||
160 | UNION |
||
161 | SELECT 'pages-created-live' AS source, COUNT(*) AS value FROM $revisionTable |
||
162 | WHERE rev_user_text=:username AND rev_parent_id=0 |
||
163 | UNION |
||
164 | SELECT 'pages-created-deleted' AS source, COUNT(*) AS value FROM $archiveTable |
||
165 | WHERE ar_user_text=:username AND ar_parent_id=0 |
||
166 | UNION |
||
167 | SELECT 'rpp' AS source, COUNT(*) AS value FROM $revisionTable |
||
168 | WHERE rev_page LIKE 'Requests_for_page_protection%' AND rev_user_text=:username |
||
169 | "); |
||
170 | |||
171 | $user = UserRepository::getUser($username, $this->container); |
||
1 ignored issue
–
show
|
|||
172 | $username = $user->getUsername(); |
||
173 | $resultQuery->bindParam("username", $username); |
||
174 | $resultQuery->execute(); |
||
175 | |||
176 | // Fetch the result data |
||
177 | $results = $resultQuery->fetchAll(); |
||
178 | |||
179 | $master = []; |
||
180 | $total = 0; |
||
181 | |||
182 | $id = 0; |
||
183 | |||
184 | foreach ($results as $row) { |
||
185 | $key = $row["source"]; |
||
186 | $value = $row["value"]; |
||
187 | |||
188 | if ($key == "acct_age") { |
||
189 | $now = new DateTime(); |
||
190 | $date = new DateTime($value); |
||
191 | $diff = $date->diff($now); |
||
192 | $formula = 365*$diff->format("%y")+30*$diff->format("%m")+$diff->format("%d"); |
||
193 | $value = $formula-365; |
||
194 | } |
||
195 | |||
196 | if ($key == "id") { |
||
197 | $id = $value; |
||
198 | } else { |
||
199 | $multiplierKey = strtoupper($row["source"] . "_MULT"); |
||
200 | $multiplier = ( isset($$multiplierKey) ? $$multiplierKey : 1 ); |
||
201 | $score = max(min($value * $multiplier, 100), -100); |
||
202 | $master[$key]["mult"] = $multiplier; |
||
203 | $master[$key]["value"] = $value; |
||
204 | $master[$key]["score"] = $score; |
||
205 | $total += $score; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | View Code Duplication | if ($id == 0) { |
|
210 | $this->addFlash("notice", [ "no-result", $username ]); |
||
211 | return $this->redirectToRoute("AdminScore", [ "project"=>$project ]); |
||
212 | } |
||
213 | |||
214 | return $this->render('adminscore/result.html.twig', [ |
||
215 | 'xtPage' => 'adminscore', |
||
216 | 'xtTitle' => $username, |
||
217 | 'projectUrl' => $url, |
||
218 | 'username' => $username, |
||
219 | 'project' => $wikiName, |
||
220 | 'master' => $master, |
||
221 | 'total' => $total, |
||
222 | ]); |
||
223 | } |
||
224 | } |
||
225 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.