Problem/Motivation
Part of #3220021: [meta] Ensure compatibility of Drupal 9 with PHP 8.1 (as it evolves)
The \Drupal\aggregator\ItemsImporter::refresh() using to calculate hash of feed's source_string which could be NULL
Also no reason to calculate hash when fetcher failed to instantiate feed
Steps to reproduce
php81 -r 'var_dump(hash("sha256", NULL));'
PHP Deprecated: hash(): Passing null to parameter #2 ($data) of type string is deprecated in Command line code on line 1
Deprecated: hash(): Passing null to parameter #2 ($data) of type string is deprecated in Command line code on line 1
string(64) "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
Proposed resolution
calculate only in success and prevent use of NULL
Remaining tasks
review/commit
User interface changes
API changes
Data model changes
Release notes snippet
Comments
Comment #2
andypost@alexpott the interdiff vs changes from META
Comment #3
daffie commentedIs this much change necessary? Maybe better to do:
Comment #4
alexpott@daffie doing a hash when success is false is doing unnecessary work. This change does that and stops the deprecation.
Comment #5
alexpottActually... the change is not correct.
The change that passes tests is
$hash = $success ? hash('sha256', $feed->source_string) : '';If success is true then$feed->source_stringis going to be set.Comment #6
andypostdebugged it and found that source string is always string when
$statusis TRUE, moreover fetcher\Drupal\aggregator\Plugin\aggregator\fetcher\DefaultFetcher::fetch()overrides it to FALSEComment #7
alexpottThis change is not necessary for PHP 8.1 compatibility. It's a subtle behaviour change that I'm not sure we should make here. I think we should open a follow-up to move this away from a magic property and to something better like a method.
Comment #8
andypostI think the follow-up exists #2862574: Add ability to track an entity object's dirty fields (and see if it has changed)
Comment #9
alexpott@andypost I'm not sure #2862574: Add ability to track an entity object's dirty fields (and see if it has changed) is the follow-up here.
source_stringis a dynamically assigned property - it's not a field in the entity sense of the word. I think due to \Drupal\Core\Entity\ContentEntityBase::__set() we end up shoving it in the \Drupal\Core\Entity\ContentEntityBase::$values array. It's all a bit messy.Comment #10
alexpottThis change is ready though and I don't think we need a follow-up to exist prior to committing as the whole entity::__set() thing is likely to become more of an issue in the future.
Comment #11
andypostAs I see this kind of changes is tracked under #2016679: Expand Entity Type interfaces to provide methods, protect the properties
For example
- #2532776: Add the undeclared variable $in_preview to the class Comment
- #2527114: Create PHPUnit test that Entity class variables are protected or are allowed public
Comment #12
andypostComment #14
catchCommitted 51166f1 and pushed to 9.3.x. Thanks!
Comment #15
andypostThank you, filed follow-up #3239690: Add undeclared variables to the Feed entity class