Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP formatter #1814

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ www/docs/rss/mp/*.rdf
data/
searchdb/
.python-version
.php-cs-fixer.cache
17 changes: 17 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->exclude([
'commonlib',
])
;
return (new PhpCsFixer\Config())
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setRules([
'@PER-CS' => true,
'@PHP74Migration' => true,
'braces_position' => ['functions_opening_brace' => 'same_line', 'classes_opening_brace' => 'same_line'],
])
->setFinder($finder)
;
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ or
* `cd www/docs/style`
* `bundle exec compass compile` for a one-off compilation or `bundle exec compass watch` to recompile on changes

## Code formatting

`script/lint` will run php-cs-fixer for php files.

## Testing

TheyWorkForYou includes a test suite, using PHPunit. To run tests, ensure that
Expand Down
9 changes: 4 additions & 5 deletions classes/AlertView.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
namespace MySociety\TheyWorkForYou;

class AlertView {

const ALERT_EXISTS = -2;
const CREATE_FAILED = -1;
public const ALERT_EXISTS = -2;
public const CREATE_FAILED = -1;

protected $user;
protected $db;
protected $alert;

public function __construct($THEUSER = null) {
$this->user = $THEUSER;
$this->db = new \ParlDB;
$this->alert = new \ALERT;
$this->db = new \ParlDB();
$this->alert = new \ALERT();
}

protected function confirmAlert($token) {
Expand Down
79 changes: 39 additions & 40 deletions classes/AlertView/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace MySociety\TheyWorkForYou\AlertView;

class Simple extends \MySociety\TheyWorkForYou\AlertView {

public function display() {
$data = array();
$data = [];
$data['recent_election'] = false;
if ( $this->user->loggedin() ) {
if ($this->user->loggedin()) {
$data['user_signed_in'] = true;
}

Expand All @@ -17,21 +16,21 @@ public function display() {
$data['postcode'] = trim(get_http_var('postcode'));

$result = $this->createAlertForPostCode($data['email'], $data['postcode']);
$data = array_merge( $data, $result );
$data = array_merge($data, $result);
} elseif (get_http_var('update')) {
$result = $this->getNewMP(get_http_var('update'));
$data = array_merge( $data, $result );
$data = array_merge($data, $result);
} elseif (get_http_var('update-alert')) {
$success = $this->replaceAlert( get_http_var('confirmation') );
$success = $this->replaceAlert(get_http_var('confirmation'));
$data['confirmation_received'] = $success;
} elseif (get_http_var('confirmed')) {
$success = $this->confirmAlert( get_http_var('confirmed') );
$success = $this->confirmAlert(get_http_var('confirmed'));
$data['confirmation_received'] = $success;
} else {
$data['email'] = $this->user->email() ? $this->user->email() : '';
$data['postcode'] = $this->user->postcode_is_set() ? $this->user->postcode() : '';

if ( $this->isEmailSignedUpForPostCode( $data['email'], $data['postcode'] ) ) {
if ($this->isEmailSignedUpForPostCode($data['email'], $data['postcode'])) {
$data['already_signed_up'] = true;
$mp = $this->getPersonFromPostcode($data['postcode']);
$data['mp_name'] = $mp->full_name();
Expand All @@ -42,10 +41,10 @@ public function display() {
}

private function getPersonFromPostcode($postcode) {
$args = array(
$args = [
'postcode' => $postcode,
'house' => 1
);
'house' => 1,
];

$member = new \MySociety\TheyWorkForYou\Member($args);
return $member;
Expand All @@ -66,30 +65,30 @@ private function validateDetails($email, $postcode) {
}

private function createAlertForPostCode($email, $postcode) {
if ( !$this->validateDetails($email, $postcode) ) {
return array('invalid-postcode-or-email' => true);
if (!$this->validateDetails($email, $postcode)) {
return ['invalid-postcode-or-email' => true];
}

try {
$person = $this->getPersonFromPostcode($postcode);
} catch ( \MySociety\TheyWorkForYou\MemberException $e ) {
return array('bad-constituency' => true);
} catch (\MySociety\TheyWorkForYou\MemberException $e) {
return ['bad-constituency' => true];
}

$details = array(
$details = [
'email' => $email,
'pid' => $person->person_id,
'pc' => $postcode,
'confirm_base' => 'https://' . DOMAIN . '/alert/by-postcode?confirmed=',
);
];

$data = array();
$data = [];
$not_logged_in = $this->user->loggedin ? false : true;
$result = $this->alert->add($details, $not_logged_in);

switch ($result) {
case self::ALERT_EXISTS:
if ( $not_logged_in ) {
if ($not_logged_in) {
// no logged in user so send them an email to let them
// know someone tried to create an alert
$this->alert->send_already_signedup_email($details);
Expand All @@ -102,7 +101,7 @@ private function createAlertForPostCode($email, $postcode) {
$data['error'] = true;
break;
default: // alert created
if ( $not_logged_in ) {
if ($not_logged_in) {
$data['confirmation_sent'] = true;
} else {
$data['signedup_no_confirm'] = true;
Expand All @@ -116,53 +115,53 @@ private function replaceAlert($confirmation) {
$existing = $this->alert->fetch_by_token($confirmation);
preg_match('/speaker:(\d+)/', $existing['criteria'], $matches);
$old_mp_id = $matches[1];
$old_mp = new \MySociety\TheyWorkForYou\Member(array( 'person_id' => $old_mp_id ) );
$new_mp = new \MySociety\TheyWorkForYou\Member(array( 'constituency' => $old_mp->constituency, 'house' => 1 ));
$old_mp = new \MySociety\TheyWorkForYou\Member([ 'person_id' => $old_mp_id ]);
$new_mp = new \MySociety\TheyWorkForYou\Member([ 'constituency' => $old_mp->constituency, 'house' => 1 ]);

$q = $this->db->query(
"SELECT alert_id, criteria, registrationtoken FROM alerts
WHERE email = :email
AND criteria LIKE :criteria
AND confirmed = 1
AND deleted = 0",
array(
[
':email' => $existing['email'],
':criteria' => '%speaker:' . $old_mp_id . '%'
)
':criteria' => '%speaker:' . $old_mp_id . '%',
]
);

foreach ($q as $row) {
// need to reset this otherwise delete does not work
$this->alert->token_checked = null;
$other_criteria = trim(preg_replace('/speaker:\d+/', '', $row['criteria']));

$details = array(
$details = [
'email' => $existing['email'],
'pid' => $new_mp->person_id,
'pc' => '',
);
if ( $other_criteria ) {
];
if ($other_criteria) {
$details['keyword'] = $other_criteria;
}

$this->alert->delete($row['alert_id'] . '::' . $row['registrationtoken']);
$this->alert->add($details, false);
}

return array(
return [
'signedup_no_confirm' => true,
'new_mp' => $new_mp->full_name(),
);
];
}

private function isEmailSignedUpForPostCode($email, $postcode) {
$is_signed_up = false;

if ( $email && $postcode ) {
if ($email && $postcode) {
try {
$person = $this->getPersonFromPostcode($postcode);
$is_signed_up = $this->alert->fetch_by_mp($email, $person->person_id);
} catch ( \MySociety\TheyWorkForYou\MemberException $e ) {
} catch (\MySociety\TheyWorkForYou\MemberException $e) {
$is_signed_up = false;
}
}
Expand All @@ -171,27 +170,27 @@ private function isEmailSignedUpForPostCode($email, $postcode) {

private function getNewMP($confirmation) {
if (!$confirmation) {
return array();
return [];
}

$existing = $this->alert->fetch_by_token($confirmation);
preg_match('/speaker:(\d+)/', $existing['criteria'], $matches);
$criteria = $matches[1];

$old_mp = new \MySociety\TheyWorkForYou\Member(array( 'person_id' => $criteria ) );
$new_mp = new \MySociety\TheyWorkForYou\Member(array( 'constituency' => $old_mp->constituency, 'house' => 1 ));
$old_mp = new \MySociety\TheyWorkForYou\Member([ 'person_id' => $criteria ]);
$new_mp = new \MySociety\TheyWorkForYou\Member([ 'constituency' => $old_mp->constituency, 'house' => 1 ]);

if ( $this->alert->fetch_by_mp( $existing['email'], $new_mp->person_id) ) {
$data = array(
if ($this->alert->fetch_by_mp($existing['email'], $new_mp->person_id)) {
$data = [
'already_signed_up' => true,
'old_mp' => $old_mp->full_name(),
'mp_name' => $new_mp->full_name(),
);
];
} else {
$data = array(
$data = [
'old_mp' => $old_mp->full_name(),
'new_mp' => $new_mp->full_name(),
);
];
}

$data['update'] = true;
Expand Down
Loading
Loading