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
Comments
Comment #2
andypostMVP
Comment #3
andypostThere's also
$http_headersundeclaredComment #4
andypostThere's no usage in contrib of headers http://grep.xnddx.ru/search?text=-%3Ehttp_headers&filename=
So I thing better to remove unused property
Comment #6
quietone commentedThe
aggregatormodule has been removed from Core in10.0.x-devand now lives on as a contrib module. Issues in the Core queue about theaggregatormodule, like this one, have been moved to the contrib module queue.Comment #10
larowlanClosed #2198821: Split items and source_string out of feed object as a duplicate
Comment #11
larowlanComment #12
larowlanshould we make this protected and then add a __get and trigger a deprecation warning?
Comment #13
rajandro commentedAs per #6
- this patch needs reroll.
Comment #14
immaculatexavier commentedAddressed #13
Rerolled patch against #4 for 2.x-dev in in 10.0.x-dev.
Kindly review.
Comment #15
larowlanNeeds work for #12
Tests are fixed in HEAD for the 2.x branch
Comment #16
dcam commentedRE: #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:
Please provide feedback.
Comment #17
dcam commentedHere'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.
Comment #19
dcam commentedAn
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 than the importers. 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.
Comment #20
dcam commentedComment #22
dcam commentedMy 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.
Comment #23
larowlanUbernit, 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
Comment #24
dcam commentedHere's the change record: https://www.drupal.org/node/3386012.
I also edited the
__set()function as suggested.Comment #27
dcam commentedThank you to everyone who worked on this issue and the one that was closed as a duplicate!