Add undeclared variables to the Feed entity class

Created on 28 September 2021, over 2 years ago
Updated 8 September 2023, 10 months ago

Problem/Motivation

Working on #3239552-9: Improve hash() calculation for PHP 8.1 β†’ faced that the source_string property undeclared in entity class

Steps to reproduce

See usage in \Drupal\aggregator\ItemsImporter::refresh()

...
    // We store the hash of feed data in the database. When refreshing a
    // feed we compare stored hash and new hash calculated from downloaded
    // data. If both are equal we say that feed is not updated.
    $hash = $success ? hash('sha256', $feed->source_string) : '';
...

But in \Drupal\aggregator\Plugin\aggregator\fetcher\DefaultFetcher::fetch() the variable is set to FALSE

  public function fetch(FeedInterface $feed) {
    $request = new Request('GET', $feed->getUrl());
    $feed->source_string = FALSE;
...
       if ($response->hasHeader('Last-Modified')) {
         $feed->setLastModified(strtotime($response->getHeaderLine('Last-Modified')));
       }
       $feed->http_headers = $response->getHeaders();

Proposed resolution

- Declare property as string and fix initial value.
- remove usage of http_headers as no core/contrib usage found

Remaining tasks

- consensus
- review
- commit

User interface changes

no

API changes

property is declared

Data model changes

no

Release notes snippet

no

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France andypost

Live updates comments and jobs are added and updated live.
  • PHP 8.2

    The issue particularly affects sites running on PHP version 8.2.0 or later.

  • API clean-up

    Refactors an existing API or subsystem for consistency, performance, modularization, flexibility, third-party integration, etc. May imply an API change. Frequently used during the Code Slush phase of the release cycle.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update about 1 year ago
    144 pass
  • πŸ‡ΊπŸ‡ΈUnited States dcam

    RE: #12

    This also applies to the undeclared $feed->items property.

    I'm sensing some code smell from these undeclared variables. It's more than just a bad practice in general. They obfuscate dependencies of the parser and processor plugins. Why doesn't fetch() return the body of the response instead of assigning it to $feed->source_string? Why doesn't parse() return the items instead of assigning it to $feed->items? I do not like using an entity class like this.

    I want to remove the use of these variables altogether. But doing that would break backward compatibility by changing the plugin APIs. I don't know that I want to bother with releasing version 3 right now. Also, I feel like we should get a version that doesn't trigger deprecation notices in PHP 8.2 out sooner rather than later.

    Suggested plan:

    • Add the three variables as protected to the Feed class.
    • Add the __get function to return the variables. Have the function trigger a deprecation notice stating that they will be removed in version 3.0.0.
    • Open issues for removing the three variables and changing the plugin APIs to not use them. I suggest keeping the issues for each variable separate.

    Please provide feedback.

  • Status changed to Needs review 11 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 11 months ago
    130 pass, 14 fail
  • πŸ‡ΊπŸ‡ΈUnited States dcam

    Here's a patch for the plan I outlined in #16. Though I decided to not do anything with $http_headers and just let it be deleted as in the original patch. This may not be the correct thing to do. Feel free to comment.

  • πŸ‡ΊπŸ‡ΈUnited States dcam

    An empty($feed->items); call in the ItemsImporter caused the failures above. empty() calls the __isset() magic method, which hadn't been implemented. I could have rewritten that line to check for the existence of items in the array a different way, but I figured a more complete solution is to implement __isset().

    I also removed a couple of other dynamic variable assignments in AggregatorTestBase. One was for $feed->items, but used it in a completely different way that the importers, which I couldn't believe. The other was for yet another new dynamic variable. Since it's only in a test I've chosen to not add it to the Feed class.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 10 months ago
    143 pass, 3 fail
  • πŸ‡ΊπŸ‡ΈUnited States dcam
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 10 months ago
    147 pass
  • πŸ‡ΊπŸ‡ΈUnited States dcam

    My change to remove those additional dynamic variables in the test base broke two of the tests. They were actually using that badly-assigned items variable. So I had to write it out of those other tests.

  • Status changed to RTBC 10 months ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
    +++ b/src/Entity/Feed.php
    @@ -55,6 +55,67 @@ use Drupal\aggregator\FeedInterface;
    +    if ($name == 'items') {
    +      @trigger_error('The $items property is deprecated in 2.1.0 and will be removed from 3.0.0. See https://www.drupal.org/project/aggregator/issues/3239690.', E_USER_DEPRECATED);
    +      $this->items = $value;
    +    }
    +    elseif ($name == 'source_string') {
    +      @trigger_error('The $source_string property is deprecated in 2.1.0 and will be removed from 3.0.0. See https://www.drupal.org/project/aggregator/issues/3239690.', E_USER_DEPRECATED);
    +      $this->source_string = $value;
    +    }
    +    else {
    +      parent::__set($name, $value);
    +    }
    

    Ubernit, but personally I see elseif (and else) as a code smell.

    We can return early here and just use if.

    I think we're supposed to link to a change record rather than an issue node - we can create one here: https://www.drupal.org/node/add/changenotice?field_project=3262413&field... β†’

    Not sure this is worth fixing, feel free to ignore on both counts

  • Status changed to Needs review 10 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 10 months ago
    152 pass
  • πŸ‡ΊπŸ‡ΈUnited States dcam

    Here's the change record: https://www.drupal.org/node/3386012 β†’ .

    I also edited the __set() function as suggested.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 10 months ago
    152 pass
    • dcam β†’ committed 5424777f on 2.x
      Issue #3239690 by dcam, andypost, immaculatexavier, ParisLiakos,...
    • dcam β†’ committed c00d0077 on 1.x
      Issue #3239690 by dcam, andypost, immaculatexavier, ParisLiakos,...
  • Status changed to Fixed 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dcam

    Thank you to everyone who worked on this issue and the one that was closed as a duplicate!

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

Production build 0.69.0 2024