Passed
Pull Request — master (#1868)
by
unknown
35:27
created

PolicyDistributionPair::getPersonDistributions()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 8
nop 4
dl 0
loc 25
rs 9.4555
1
<?php
2
3
/**
4
 * Party Cohort Class
5
 *
6
 * @package TheyWorkForYou
7
 */
8
9
namespace MySociety\TheyWorkForYou;
10
11
/**
12
 * Policy DistributionPair
13
 * Brings together the member_distribution (their own policy score)
14
 * And the comporable members total and score.
15
 */
16
17
class PolicyDistributionPair {
18
    public ?PolicyDistribution $member_distribution;
19
    public ?PolicyDistribution $comparison_distribution;
20
    public int $policy_id;
21
    public string $policy_desc;
22
    public bool $covid_affected;
23
24
25
    public function __construct(
26
        ?PolicyDistribution $member_distribution,
27
        ?PolicyDistribution $comparison_distribution
28
    ) {
29
        $this->member_distribution = $member_distribution;
30
        $this->comparison_distribution = $comparison_distribution;
31
        $this->policy_id = $this->getPolicyId();
32
33
34
        $policies_obj = new Policies();
35
        $policies_obj->getCovidAffected();
36
        $this->covid_affected = in_array($this->policy_id, $policies_obj->getCovidAffected());
37
38
        $this->policy_desc = $policies_obj->getPolicies()[$this->policy_id];
39
40
    }
41
42
    public function getMoreDetailsLink(): string {
43
        $person_id = $this->getPersonId();
44
        ;
45
        $policy_id = $this->getPolicyId();
46
        $period = strtolower($this->getVotingPeriod());
47
        $party_slug = $this->member_distribution->party_slug;
48
        return TWFY_VOTES_URL . "/person/$person_id/policies/commons/$party_slug/$period/$policy_id";
0 ignored issues
show
Bug introduced by
The constant MySociety\TheyWorkForYou\TWFY_VOTES_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
49
    }
50
51
    public function getPersonId(): int {
52
        return $this->member_distribution->person_id;
53
    }
54
55
56
    public function getPartySlug(): string {
57
        return $this->member_distribution->party_slug;
58
    }
59
60
    public function getVotingPeriod(): string {
61
        return $this->member_distribution->period_slug;
62
    }
63
64
    public function getPolicyId(): int {
65
        if ($this->member_distribution) {
66
            return $this->member_distribution->policy_id;
67
        }
68
        if ($this->comparison_distribution) {
69
            return $this->comparison_distribution->policy_id;
70
        }
71
        throw new \Exception('No policy ID found for this pair');
72
    }
73
74
    public function scoreDifference(): float {
75
76
        if (!$this->comparison_distribution) {
77
            return 0;
78
        }
79
80
        return $this->member_distribution->distance_score - $this->comparison_distribution->distance_score;
81
    }
82
83
    public function sigScoreDifference(): bool {
84
85
        if (!$this->comparison_distribution) {
86
            return false;
87
        }
88
89
        $member_score = $this->member_distribution->distance_score;
90
        $comparison_score = $this->comparison_distribution->distance_score;
91
92
        if ($this->member_distribution->noDataAvailable()) {
0 ignored issues
show
Bug introduced by
The method noDataAvailable() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        if ($this->member_distribution->/** @scrutinizer ignore-call */ noDataAvailable()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
            return false;
94
        }
95
96
        if ($member_score < 0.4 && $comparison_score > 0.6) {
97
            return true;
98
        }
99
        if ($member_score > 0.6 && $comparison_score < 0.4) {
100
            return true;
101
        }
102
        return false;
103
104
    }
105
106
107
    /**
108
     * Retrieves the policy distribution pairs for a specific person, party, period, and chamber.
109
     *
110
     * @param int $person_id The ID of the person.
111
     * @param string $party_slug The slug of the party.
112
     * @param string $period_slug The slug of the period.
113
     * @param int $house The ID of the house (default is HOUSE_TYPE_COMMONS).
114
     * @return PolicyDistributionPair[] An array of PolicyDistributionPair objects.
115
     */
116
    public static function getPersonDistributions(int $person_id, string $party_slug, string $period_slug, int $house = HOUSE_TYPE_COMMONS): array {
117
118
        $distributions = PolicyDistribution::getPersonDistributions($person_id, $party_slug, $period_slug, $house);
119
120
        // group the distributions by policy_id
121
        $grouped_distributions = [];
122
        foreach ($distributions as $distribution) {
123
            $grouped_distributions[$distribution->policy_id][] = $distribution;
124
        }
125
126
        $pairs = [];
127
        foreach ($grouped_distributions as $policy_id => $policy
128
        ) {
129
            $member_distribution = null;
130
            $comparison_distribution = null;
131
            foreach ($policy as $distribution) {
132
                if ($distribution->is_target) {
133
                    $member_distribution = $distribution;
134
                } else {
135
                    $comparison_distribution = $distribution;
136
                }
137
            }
138
            $pairs[] = new PolicyDistributionPair($member_distribution, $comparison_distribution);
139
        }
140
        return $pairs;
141
    }
142
143
}
144