Problem/Motivation

In SubscriberExportForm.php there are several uses of the $_GET super global, as indicated by running PHPCS with the DrupalPractice standard in the file, which can be seen in the attached screenshot. As stated by phpcs, $_GET super global must not be accessed directly, the request.stack service should be injected and $stack->getCurrentRequest()->query->get() should be used instead.

Proposed resolution

Inject the request.stack service and use $stack->getCurrentRequest()->query->get() instead.

CommentFileSizeAuthor
simplenews-get-super-global-phpcs.png146.04 KBmarcos_lima

Issue fork simplenews-3248661

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

marcos_lima created an issue. See original summary.

marcos_lima’s picture

Assigned: marcos_lima » Unassigned
Status: Active » Needs review

Removed the GET super global uses and opened an MR. Since the request_stack already has a blueprint to be injected in FormBase (see code below), which is extended by SubscriberExportForm.php, I used that to implement the injection.

protected function getRequest() {
    if (!$this->requestStack) {
      $this->requestStack = \Drupal::service('request_stack');
    }
    return $this->requestStack->getCurrentRequest();
  }

And since we cannot use isset() on the result of an expression, I needed to assign the request to a variable first so then we could keep the current logic, like:

$states = $this->getRequest()->query->get('states');
$default['states'] = isset($states) ? $states : ['active' => 'active'];
marcos_lima’s picture

Assigned: Unassigned » marcos_lima
Status: Needs review » Needs work

Thank you kindly for the code review @Berdir. Assigning myself to implement and test the proposed changes.

marcos_lima’s picture

Assigned: marcos_lima » Unassigned
Status: Needs work » Needs review

I made a commit implementing the changes that @Berdir proposed in the MR, which resulted in a much cleaner fix.

adamps’s picture

Status: Needs review » Needs work

Thanks looks good. $this->getRequest()->query is present 6 times in the same function so please can we save it in a local variable $query?

marcos_lima’s picture

Status: Needs work » Needs review

Thanks for the review @AdamPS! Made a commit addressing what you suggested in #6.

  • AdamPS committed 2855bf0 on 3.x authored by marcos_lima
    Issue #3248661 by marcos_lima, AdamPS, Berdir: The $_GET super global...

AdamPS credited Berdir.

adamps’s picture

Title: The $_GET super global must not be accessed directly; inject the request.stack service » Avoid use of the $_GET super global
Status: Needs review » Fixed

Thanks

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.