| Total Complexity | 81 |
| Total Lines | 622 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Divisions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Divisions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Divisions { |
||
| 11 | /** |
||
| 12 | * Member |
||
| 13 | */ |
||
| 14 | |||
| 15 | private $member; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * DB handle |
||
| 19 | */ |
||
| 20 | private $db; |
||
| 21 | |||
| 22 | private $positions; |
||
| 23 | private $policies; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Constructor |
||
| 27 | * |
||
| 28 | * @param Member $member The member to get positions for. |
||
| 29 | */ |
||
| 30 | |||
| 31 | public function __construct(?Member $member = null) { |
||
| 32 | $this->member = $member; |
||
| 33 | $this->positions = null; |
||
| 34 | $this->policies = new Policies(); |
||
| 35 | $this->db = new \ParlDB(); |
||
| 36 | } |
||
| 37 | |||
| 38 | public static function getMostRecentDivisionDate() { |
||
| 39 | $db = new \ParlDB(); |
||
| 40 | $q = $db->query( |
||
| 41 | "SELECT policy_id, max(division_date) as recent |
||
| 42 | FROM policydivisionlink |
||
| 43 | JOIN divisions USING(division_id) |
||
| 44 | GROUP BY policy_id" |
||
| 45 | ); |
||
| 46 | |||
| 47 | $policy_maxes = []; |
||
| 48 | foreach ($q as $row) { |
||
| 49 | $policy_maxes[$row['policy_id']] = $row['recent']; |
||
| 50 | } |
||
| 51 | $policy_maxes['latest'] = $policy_maxes ? max(array_values($policy_maxes)) : ''; |
||
| 52 | return $policy_maxes; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param int $number Number of divisions to return. Optional. |
||
| 57 | * @param string|string[] $houses House name (eg: "commons") or array of |
||
| 58 | * house names. Optional. |
||
| 59 | */ |
||
| 60 | public function getRecentDivisions($number = 20, $houses = null) { |
||
| 61 | $select = ''; |
||
| 62 | $order = 'ORDER BY division_date DESC, division_number DESC'; |
||
| 63 | $limit = 'LIMIT :count'; |
||
| 64 | $params = [ |
||
| 65 | ':count' => $number, |
||
| 66 | ]; |
||
| 67 | |||
| 68 | $where = []; |
||
| 69 | if ($houses) { |
||
| 70 | if (is_string($houses)) { |
||
| 71 | $houses = [ $houses ]; |
||
| 72 | } |
||
| 73 | $where[] = 'house IN ("' . implode('", "', $houses) . '")'; |
||
| 74 | } |
||
| 75 | if (!$houses || in_array('senedd', $houses)) { |
||
|
|
|||
| 76 | if (LANGUAGE == 'cy') { |
||
| 77 | $where[] = "divisions.division_id NOT LIKE '%-en-%'"; |
||
| 78 | } else { |
||
| 79 | $where[] = "divisions.division_id NOT LIKE '%-cy-%'"; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | $where = 'WHERE ' . join(' AND ', $where); |
||
| 83 | |||
| 84 | if ($this->member) { |
||
| 85 | $select = "SELECT divisions.*, vote FROM divisions |
||
| 86 | LEFT JOIN persondivisionvotes ON divisions.division_id=persondivisionvotes.division_id AND person_id=:person_id"; |
||
| 87 | $params[':person_id'] = $this->member->person_id; |
||
| 88 | } else { |
||
| 89 | $select = "SELECT * FROM divisions"; |
||
| 90 | } |
||
| 91 | |||
| 92 | $q = $this->db->query( |
||
| 93 | sprintf("%s %s %s %s", $select, $where, $order, $limit), |
||
| 94 | $params |
||
| 95 | ); |
||
| 96 | |||
| 97 | $divisions = []; |
||
| 98 | foreach ($q as $division) { |
||
| 99 | $data = $this->getParliamentDivisionDetails($division); |
||
| 100 | |||
| 101 | $mp_vote = ''; |
||
| 102 | if (array_key_exists('vote', $division)) { |
||
| 103 | if ($division['vote'] == 'aye') { |
||
| 104 | $mp_vote = 'voted in favour'; |
||
| 105 | } elseif ($division['vote'] == 'tellaye') { |
||
| 106 | $mp_vote = 'voted (as a teller) in favour'; |
||
| 107 | } elseif ($division['vote'] == 'no') { |
||
| 108 | $mp_vote = 'voted against'; |
||
| 109 | } elseif ($division['vote'] == 'tellno') { |
||
| 110 | $mp_vote = 'voted (as a teller) against'; |
||
| 111 | } elseif ($division['vote'] == 'absent') { |
||
| 112 | $mp_vote = ' was absent'; |
||
| 113 | } elseif ($division['vote'] == 'both') { |
||
| 114 | $mp_vote = ' abstained'; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | $data['mp_vote'] = $mp_vote; |
||
| 118 | $house = Utility\House::division_house_name_to_number($division['house']); |
||
| 119 | $data['members'] = \MySociety\TheyWorkForYou\Utility\House::house_to_members($house); |
||
| 120 | $divisions[] = $data; |
||
| 121 | } |
||
| 122 | |||
| 123 | return ['divisions' => $divisions]; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param int $number Number of divisions to return. Optional. |
||
| 128 | * @param int|int[] $majors Major types (e.g. 1) or array of |
||
| 129 | * major types. Optional. |
||
| 130 | */ |
||
| 131 | public function getRecentDebatesWithDivisions($number = 20, $majors = null) { |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getRecentDivisionsForPolicies($policies, $number = 20) { |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * |
||
| 209 | * Get a list of division votes related to a policy |
||
| 210 | * |
||
| 211 | * Returns an array with one key ( the policyID ) containing a hash |
||
| 212 | * with a policy_id key and a divisions key which contains an array |
||
| 213 | * with details of all the divisions. |
||
| 214 | * |
||
| 215 | * Each division is a hash with the following fields: |
||
| 216 | * division_id, date, vote, gid, url, text, strong |
||
| 217 | * |
||
| 218 | * @param int|null $policyId The ID of the policy to get divisions for |
||
| 219 | */ |
||
| 220 | |||
| 221 | public function getMemberDivisionsForPolicy($policyID = null) { |
||
| 222 | $where_extra = ''; |
||
| 223 | $args = [':person_id' => $this->member->person_id]; |
||
| 224 | if ($policyID) { |
||
| 225 | $where_extra = 'AND policy_id = :policy_id'; |
||
| 226 | $args[':policy_id'] = $policyID; |
||
| 227 | } |
||
| 228 | $q = $this->db->query( |
||
| 229 | "SELECT policy_id, division_id, division_title, yes_text, no_text, division_date, division_number, vote, gid, strength, alignment |
||
| 230 | FROM policydivisionlink JOIN persondivisionvotes USING(division_id) |
||
| 231 | JOIN divisions USING(division_id) |
||
| 232 | WHERE person_id = :person_id AND direction <> 'abstention' $where_extra |
||
| 233 | ORDER by policy_id, division_date DESC", |
||
| 234 | $args |
||
| 235 | ); |
||
| 236 | # possibly add another query here to get related policies that use the same votes |
||
| 237 | return $this->divisionsByPolicy($q); |
||
| 238 | } |
||
| 239 | public function getDivisionByGid($gid) { |
||
| 240 | $args = [ |
||
| 241 | ':gid' => $gid, |
||
| 242 | ]; |
||
| 243 | $q = $this->db->query("SELECT * FROM divisions WHERE gid = :gid", $args)->first(); |
||
| 244 | |||
| 245 | if (!$q) { |
||
| 246 | return false; |
||
| 247 | } |
||
| 248 | |||
| 249 | return $this->_division_data($q); |
||
| 250 | } |
||
| 251 | |||
| 252 | public function getDivisionResults($division_id) { |
||
| 253 | $args = [ |
||
| 254 | ':division_id' => $division_id, |
||
| 255 | ]; |
||
| 256 | $q = $this->db->query("SELECT * FROM divisions WHERE division_id = :division_id", $args)->first(); |
||
| 257 | |||
| 258 | if (!$q) { |
||
| 259 | return false; |
||
| 260 | } |
||
| 261 | |||
| 262 | return $this->_division_data($q); |
||
| 263 | |||
| 264 | } |
||
| 265 | |||
| 266 | private function _division_data($row) { |
||
| 267 | |||
| 268 | $details = $this->getParliamentDivisionDetails($row); |
||
| 269 | |||
| 270 | $house = $row['house']; |
||
| 271 | $args['division_id'] = $row['division_id']; |
||
| 272 | $args['division_date'] = $row['division_date']; |
||
| 273 | $args['house'] = \MySociety\TheyWorkForYou\Utility\House::division_house_name_to_number($house); |
||
| 274 | |||
| 275 | $q = $this->db->query( |
||
| 276 | "SELECT pdv.person_id, vote, proxy, title, given_name, family_name, lordofname, party |
||
| 277 | FROM persondivisionvotes AS pdv JOIN person_names AS pn ON (pdv.person_id = pn.person_id) |
||
| 278 | JOIN member AS m ON (pdv.person_id = m.person_id) |
||
| 279 | WHERE division_id = :division_id |
||
| 280 | AND house = :house AND entered_house <= :division_date AND left_house >= :division_date |
||
| 281 | AND start_date <= :division_date AND end_date >= :division_date |
||
| 282 | ORDER by family_name", |
||
| 283 | $args |
||
| 284 | ); |
||
| 285 | |||
| 286 | $votes = [ |
||
| 287 | 'yes_votes' => [], |
||
| 288 | 'no_votes' => [], |
||
| 289 | 'absent_votes' => [], |
||
| 290 | 'both_votes' => [], |
||
| 291 | ]; |
||
| 292 | |||
| 293 | $party_breakdown = [ |
||
| 294 | 'yes_votes' => [], |
||
| 295 | 'no_votes' => [], |
||
| 296 | 'absent_votes' => [], |
||
| 297 | 'both_votes' => [], |
||
| 298 | ]; |
||
| 299 | |||
| 300 | # Sort Lords specially |
||
| 301 | $data = $q->fetchAll(); |
||
| 302 | if ($args['house'] == HOUSE_TYPE_LORDS) { |
||
| 303 | uasort($data, 'by_peer_name'); |
||
| 304 | } |
||
| 305 | |||
| 306 | foreach ($data as $vote) { |
||
| 307 | $detail = [ |
||
| 308 | 'person_id' => $vote['person_id'], |
||
| 309 | 'name' => ucfirst(member_full_name( |
||
| 310 | $args['house'], |
||
| 311 | $vote['title'], |
||
| 312 | $vote['given_name'], |
||
| 313 | $vote['family_name'], |
||
| 314 | $vote['lordofname'] |
||
| 315 | )), |
||
| 316 | 'party' => $vote['party'], |
||
| 317 | 'proxy' => false, |
||
| 318 | 'teller' => false, |
||
| 319 | ]; |
||
| 320 | |||
| 321 | if (strpos($vote['vote'], 'tell') !== false) { |
||
| 322 | $detail['teller'] = true; |
||
| 323 | } |
||
| 324 | |||
| 325 | if ($vote['proxy']) { |
||
| 326 | $q = $this->db->query( |
||
| 327 | "SELECT title, given_name, family_name, lordofname |
||
| 328 | FROM person_names AS pn |
||
| 329 | WHERE person_id = :person_id |
||
| 330 | AND start_date <= :division_date AND end_date >= :division_date", |
||
| 331 | [ ':person_id' => $vote['proxy'], ':division_date' => $row['division_date'] ] |
||
| 332 | )->first(); |
||
| 333 | $detail['proxy'] = ucfirst(member_full_name( |
||
| 334 | HOUSE_TYPE_COMMONS, |
||
| 335 | $q['title'], |
||
| 336 | $q['given_name'], |
||
| 337 | $q['family_name'], |
||
| 338 | $q['lordofname'] |
||
| 339 | )); |
||
| 340 | } |
||
| 341 | |||
| 342 | if ($vote['vote'] == 'aye' or $vote['vote'] == 'tellaye') { |
||
| 343 | $votes['yes_votes'][] = $detail; |
||
| 344 | @$party_breakdown['yes_votes'][$detail['party']]++; |
||
| 345 | } elseif ($vote['vote'] == 'no' or $vote['vote'] == 'tellno') { |
||
| 346 | $votes['no_votes'][] = $detail; |
||
| 347 | @$party_breakdown['no_votes'][$detail['party']]++; |
||
| 348 | } elseif ($vote['vote'] == 'absent') { |
||
| 349 | $votes['absent_votes'][] = $detail; |
||
| 350 | @$party_breakdown['absent_votes'][$detail['party']]++; |
||
| 351 | } elseif ($vote['vote'] == 'both') { |
||
| 352 | $votes['both_votes'][] = $detail; |
||
| 353 | @$party_breakdown['both_votes'][$detail['party']]++; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | foreach ($votes as $vote => $count) { // array('yes_votes', 'no_votes', 'absent_votes', 'both_votes') as $vote) { |
||
| 358 | $votes[$vote . '_by_party'] = $votes[$vote]; |
||
| 359 | usort($votes[$vote . '_by_party'], function ($a, $b) { |
||
| 360 | return $a['party'] > $b['party']; |
||
| 361 | }); |
||
| 362 | } |
||
| 363 | |||
| 364 | foreach ($party_breakdown as $vote => $parties) { |
||
| 365 | $summary = []; |
||
| 366 | foreach ($parties as $party => $count) { |
||
| 367 | array_push($summary, "$party: $count"); |
||
| 368 | } |
||
| 369 | |||
| 370 | sort($summary); |
||
| 371 | $party_breakdown[$vote] = implode(', ', $summary); |
||
| 372 | } |
||
| 373 | |||
| 374 | $details = array_merge($details, $votes); |
||
| 375 | $details['party_breakdown'] = $party_breakdown; |
||
| 376 | $details['members'] = \MySociety\TheyWorkForYou\Utility\House::house_to_members($args['house']); |
||
| 377 | $details['house'] = $house; |
||
| 378 | $details['house_number'] = $args['house']; |
||
| 379 | |||
| 380 | return $details; |
||
| 381 | } |
||
| 382 | |||
| 383 | public function getDivisionResultsForMember($division_id, $person_id) { |
||
| 384 | $args = [ |
||
| 385 | ':division_id' => $division_id, |
||
| 386 | ':person_id' => $person_id, |
||
| 387 | ]; |
||
| 388 | $q = $this->db->query( |
||
| 389 | "SELECT division_id, division_title, yes_text, no_text, division_date, division_number, gid, vote |
||
| 390 | FROM divisions JOIN persondivisionvotes USING(division_id) |
||
| 391 | WHERE division_id = :division_id AND person_id = :person_id", |
||
| 392 | $args |
||
| 393 | )->first(); |
||
| 394 | |||
| 395 | // if the vote was before or after the MP was in Parliament |
||
| 396 | // then there won't be a row |
||
| 397 | if (!$q) { |
||
| 398 | return false; |
||
| 399 | } |
||
| 400 | |||
| 401 | $details = $this->getDivisionDetails($q); |
||
| 402 | return $details; |
||
| 403 | } |
||
| 404 | |||
| 405 | public function generateSummary($votes) { |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * |
||
| 437 | * Get all the divisions a member has voted in keyed by policy |
||
| 438 | * |
||
| 439 | * Returns an array with keys for each policyID, each of these contains |
||
| 440 | * the same structure as getMemberDivisionsForPolicy |
||
| 441 | * |
||
| 442 | */ |
||
| 443 | |||
| 444 | public function getAllMemberDivisionsByPolicy() { |
||
| 445 | $policies = $this->getMemberDivisionsForPolicy(); |
||
| 446 | return Utility\Shuffle::keyValue($policies); |
||
| 447 | } |
||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * Get the last n votes for a member |
||
| 452 | * |
||
| 453 | * @param $number int - How many divisions to return. Defaults to 20 |
||
| 454 | * @param $context string - The context of the page the results are being presented in. |
||
| 455 | * This affects the summary details and can either be 'Parliament' in which case the |
||
| 456 | * overall vote for all MPs is returned, plus additional information on how the MP passed |
||
| 457 | * in to the constructor voted, or the default of 'MP' which is just the vote of the |
||
| 458 | * MP passed in to the constructor. |
||
| 459 | * |
||
| 460 | * Returns an array of divisions |
||
| 461 | */ |
||
| 462 | public function getRecentMemberDivisions($number = 20) { |
||
| 479 | } |
||
| 480 | |||
| 481 | private function constructYesNoVoteDescription($direction, $title, $short_text) { |
||
| 482 | $text = ' ' ; |
||
| 483 | if ($short_text) { |
||
| 484 | $text .= sprintf(gettext('voted %s'), $short_text); |
||
| 485 | } else { |
||
| 486 | $text .= sprintf(gettext('voted %s on <em>%s</em>'), $direction, $title); |
||
| 487 | } |
||
| 488 | |||
| 489 | return $text; |
||
| 490 | } |
||
| 491 | |||
| 492 | private function constructVoteDescription($vote, $yes_text, $no_text, $division_title) { |
||
| 524 | } |
||
| 525 | |||
| 526 | private function getBasicDivisionDetails($row, $vote) { |
||
| 527 | $yes_text = $row['yes_text']; |
||
| 528 | $no_text = $row['no_text']; |
||
| 571 | } |
||
| 572 | |||
| 573 | private function getDivisionDetails($row) { |
||
| 574 | return $this->getBasicDivisionDetails($row, $row['vote']); |
||
| 575 | } |
||
| 576 | |||
| 577 | private function getParliamentDivisionDetails($row) { |
||
| 587 | } |
||
| 588 | |||
| 589 | private function divisionsByPolicy($q) { |
||
| 590 | $policies = []; |
||
| 591 | |||
| 592 | # iterate through each division, and adds it to an array of policies |
||
| 593 | # if there is only one policy being queried, it will be an array of 1 |
||
| 594 | foreach ($q as $row) { |
||
| 595 | $policy_id = $row['policy_id']; |
||
| 596 | |||
| 597 | # if this policy hasn't come up yet, create the key for it |
||
| 598 | if (!array_key_exists($policy_id, $policies)) { |
||
| 599 | $policies[$policy_id] = [ |
||
| 600 | 'policy_id' => $policy_id, |
||
| 601 | 'divisions' => [], |
||
| 602 | ]; |
||
| 603 | $policies[$policy_id]['desc'] = $this->policies->getPolicies()[$policy_id]; |
||
| 604 | $policies[$policy_id]['header'] = $this->policies->getPolicyDetails($policy_id); |
||
| 605 | if ($this->positions) { |
||
| 606 | $policies[$policy_id]['position'] = $this->positions->positionsById[$policy_id]; |
||
| 607 | } |
||
| 608 | } |
||
| 609 | |||
| 610 | |||
| 611 | $division = $this->getDivisionDetails($row); |
||
| 612 | |||
| 613 | $policies[$policy_id]['divisions'][] = $division; |
||
| 614 | }; |
||
| 615 | |||
| 616 | return $policies; |
||
| 617 | } |
||
| 618 | |||
| 619 | private function divisionUrlFromGid($gid) { |
||
| 632 | } |
||
| 633 | } |
||
| 634 |