Issues (404)

classes/PolicyDistributionPair.php (2 issues)

Labels
Severity
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
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
84
    /**
85
     * Calculates the significance score difference.
86
     *
87
     * This function determines whether there is a significant difference
88
     * in scores between two policy distributions.
89
     * This is used to see if it should be highlighted separately as a difference with the party.
90
     * A different is significant is a members and the comparison score are
91
     * strongly directional in different directions.
92
     * e.g. as distance is between 0 and 1
93
     * if the member score is less than 0.4 and the comparison score is greater than 0.6
94
     * or
95
     * if the member score is greater than 0.6 and the comparison score is less than 0.4
96
     * Significant differences
97
     * @return bool Returns true if a significant difference exists, otherwise false.
98
     */
99
    public function sigScoreDifference(): bool {
100
101
        if (!$this->comparison_distribution) {
102
            return false;
103
        }
104
105
        $member_score = $this->member_distribution->distance_score;
106
        $comparison_score = $this->comparison_distribution->distance_score;
107
108
        if ($this->member_distribution->noDataAvailable()) {
0 ignored issues
show
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

108
        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...
109
            return false;
110
        }
111
112
        if ($member_score < 0.4 && $comparison_score > 0.6) {
113
            return true;
114
        }
115
        if ($member_score > 0.6 && $comparison_score < 0.4) {
116
            return true;
117
        }
118
        return false;
119
120
    }
121
122
123
    /**
124
     * Retrieves the policy distribution pairs for a specific person, party, period, and chamber.
125
     *
126
     * @param int $person_id The ID of the person.
127
     * @param string $party_slug The slug of the party.
128
     * @param string $period_slug The slug of the period.
129
     * @param int $house The ID of the house (default is HOUSE_TYPE_COMMONS).
130
     * @return PolicyDistributionPair[] An array of PolicyDistributionPair objects.
131
     */
132
    public static function getPersonDistributions(int $person_id, string $party_slug, string $period_slug, int $house = HOUSE_TYPE_COMMONS): array {
133
134
        $distributions = PolicyDistribution::getPersonDistributions($person_id, $party_slug, $period_slug, $house);
135
136
        // group the distributions by policy_id
137
        $grouped_distributions = [];
138
        foreach ($distributions as $distribution) {
139
            $grouped_distributions[$distribution->policy_id][] = $distribution;
140
        }
141
142
        $pairs = [];
143
        foreach ($grouped_distributions as $policy_id => $policy
144
        ) {
145
            $member_distribution = null;
146
            $comparison_distribution = null;
147
            foreach ($policy as $distribution) {
148
                if ($distribution->is_target) {
149
                    $member_distribution = $distribution;
150
                } else {
151
                    $comparison_distribution = $distribution;
152
                }
153
            }
154
            $pairs[] = new PolicyDistributionPair($member_distribution, $comparison_distribution);
155
        }
156
        return $pairs;
157
    }
158
159
}
160