Problem/Motivation
Follow-up to #3173031: Clean-up \Drupal\system\Plugin\ImageToolkit\GDToolkit when core require php 8.0
Since PHP version 8.0 the GD extension using classed object \GdImage instead of resource but toolkit methods has not a lot of usage and could improve DX for new-comers using nowadays state
Steps to reproduce
See #3156887: \Drupal\system\Plugin\ImageToolkit\GDToolkit needs to support \GdImage objects for PHP 8 compatibility
Usage in contrib http://grep.xnddx.ru/search?text=setResource%28&filename=
Proposed resolution
deprecate setResource() and getResource() in favour of
- public function setImage(\GdImage $gd_image): self https://git.drupalcode.org/project/drupal/-/merge_requests/1486/diffs#no...
- public function getImage(): \GdImage https://git.drupalcode.org/project/drupal/-/merge_requests/1486/diffs#no...
Remaining tasks
agree/patch/commit
User interface changes
no
API changes
\Drupal\system\Plugin\ImageToolkit\GDToolkit::setResource() replaced with setImage()
\Drupal\system\Plugin\ImageToolkit\GDToolkit::getResource() replaced with getImage()
Protected variable to store result renamed from \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource to \Drupal\system\Plugin\ImageToolkit\GDToolkit::image
Data model changes
no
Release notes snippet
no
| Comment | File | Size | Author |
|---|---|---|---|
| #62 | 3265953-62.patch | 34.11 KB | andypost |
| #62 | interdiff.txt | 8.9 KB | andypost |
| #48 | 3265953-48.patch | 34.11 KB | andypost |
Issue fork drupal-3265953
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:
- 3265953-deprecate-resource-mentions
changes, plain diff MR !1869
Comments
Comment #2
andypostComment #4
andypostFiled CR https://www.drupal.org/node/3265963 and here's initial patch and MR on top of #3173031: Clean-up \Drupal\system\Plugin\ImageToolkit\GDToolkit when core require php 8.0
Ref https://www.drupal.org/about/core/policies/core-change-policies/drupal-d...
Comment #5
andypostComment #6
daffie commentedComment #7
ankithashettyRerolled patch in #4, thanks!
Comment #9
quietone commentedJust updating the patch and this should fix the failing tests. Although, I am a bit tired ....
Comment #10
andypostLooks ready
Comment #11
alexpottCan we change all the deprecations to 10.1.x we're targeting 10.1 for stuff like this and not 10.0
Thanks!
Comment #12
mondrakeWe can typehint the property as ?\GdImage and remove from the docblock
self or static? static would allow extending the class IIRC
typehint should be ?\GdImage
IMHO we should also rename the local $resource variable
imagedestroy is a noop since PHP 8.0, we can remove it and adjust the inline comment. Also rename $original_resource
Rename $original_resource
imagedestroy is noop
Rename $original_res and $new_res
Rename $resource
Rename $resource
Rename $resource
Comment #13
andypostAddress #11 and #12, removed all
imagedestroy()as it's no-op and useless since PHP 8.1Comment #14
andypostproper patch(
Comment #15
andypostRe #12.2 static vs self - I changed to static as the class is not final and https://phpstan.org/writing-php-code/phpdoc-types#static-and-%24this
Comment #16
andypostfix remaining mentions
Comment #17
andypostthe only question is should we allow to pass NULL as GD image object, looks no way to reset it to null
Comment #18
andypostClosed as duplicate #3292531: Rename GDToolkit::resource as it object since PHP 8.0
Comment #19
mondrakethis signature will potentially cause BC break if NULL is passed to legacy
setResource()or to the method directly. We cannot rule that out from contrib, setting the resource to null can be a way to invalidate the image if for any reason a toolkit operation fails.So either we allow null like
public function setGdImage(?\GdImage $image): staticor we introduce a separate method to null the property.
seems like this is removed with no replacement?
Comment #20
andypostThank you!
1) fixed, that's what I was confused in #17
2) it's done intentionally as this variable no longer used - was supposed to be used in
imagedestroy()but as it's no-op I removed itComment #21
andypostfix docblock
Comment #22
mondrake#20.2 oh yeah I missed that!
Thanks looks all good to me now.
Comment #23
alexpottThis change results in this property no longer being set. So keeping it around but unused feels odd. If we feel we need to maintain the protected property that we should maintain the value.
I think we should do something similar to the \Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait and leverage the __get() function to trigger a deprecation if someone tries to use $this->resource and then we should give them $this->image.
Comment #24
andypostNot sure it's ok to use magic
__get()as it makes this property accessible outside of child classes https://3v4l.org/0XIA9Comment #25
andypostDiscussed in slack and value of having BC is preferable and there's getter for property so it's like public initially
Comment #26
mondrakeWell, that would be only relevant for classes that extend the GDToolkit class itself, which I am not sure there are any... the toolkit operations would not access it directly anyway.
However, this is already agreed, so fine, but we also need a
__set()implementation to fully cover BC.Comment #27
andypostAdded, do we need to implement
__isset()and__unset()?PS: there's only 3 classes using to override it in contrib http://codcontrib.hank.vps-private.net/search?text=GDToolkit&filename= but none of them accessing this property
Comment #28
mondrakeI suppose so, if we need to be fully BC... you're right.
Comment #29
andypostAdded both
Comment #30
mondrakeThanks. Cannot see anything else.
Comment #31
longwaveI wonder if this is really necessary, is it worth doing the deprecation dance for an internal property? It can just live as $resource for historical reasons - we only really need to update the public API?
Maybe bikeshedding here but do we need Gd in the method name, given the class is already the GDToolkit? Is setImage/getImage better?
Does the statement about destroying still hold true now we are no longer using resources?
Variables should be snake_case when snake case is used elsewhere in the same file.
Comment #32
andypostGreat points! will work on 2-4 because 1 requested by alexpott
Comment #33
andypostAddressed #31 2-4
Comment #34
andypostI think it should be improved as
is_tempno longer used and is just an indicator of that operation happening in pipelinelooks needs better clarification, as if operation creates new image then depending on result original will be restored or not
Comment #35
andypostThis change to
isset()is covered by testsComment #36
mondrakeLooks good to me, #31.2-4 addressed. #34 would be an interesting follow-up I guess, even if at first sight I'd daresay we cannot exclude that a class extending this would not use it - so, is it or isn't it API? Just checked and in the ImageMagick module that argument is not implemented.
Comment #38
mondrakeFlaky test failure?
Comment #40
andypostre-queued
Comment #41
mondrakeQuite sure this needs a reroll after commit of #2583041: GD toolkit & operations should catch \Throwable to fail gracefully in case of errors
Comment #42
andypostback to RTBC and re-queued as commit reverted for #2583041: GD toolkit & operations should catch \Throwable to fail gracefully in case of errors
Comment #43
xjmThanks for working on this! I guess the patch is the latest/canonical version here and not the merge request, so I am going to close the MR for clarity.
I made some small improvements to the change record.
This is, technically, an internal BC break to make the typehint stricter. I think it is probably low-risk, but should be restricted to a minor only and maybe be mentioned in the CR.
Why are some of these
==but the others===?Method names in all the deprecation messages should have parens. OTOH the correct way to document PHP member properties is
\Drupal\whatever\Class::$property. Both::resourceand::imageneed to be fixed one way or the other.Also, these seem to refer to an
imagemethod or property that does not exist?I wondered why this is being removed from the example code. Answer above is:
So that's fine.
NW mainly for #2, although the CR could use some expansion about deprecated properties/stricter property types and etc. as well. Thanks!
Comment #44
xjmSaving credits for reviewers.
Comment #45
xjmOne more thing -- the local variable here is mis-named. While it is kind of magic, I think this should be "image" and not "mage". 🧙♂️
Comment #46
_utsavsharma commentedAddressed 43.2 and 45.
Please review.
Comment #47
andypostre-queued and updated CR with
s/resource/imageinternal BC breakComment #48
andypostRe-roll after #2583041: GD toolkit & operations should catch \Throwable to fail gracefully in case of errors
Comment #49
smustgrave commentedThe changes in patch #48 look good.
Wonder if we can update the IS and CR with the additional things being deprecated outside the 2 mentioned.
Comment #50
andypostAdded to summary rename of
s/resource/image- there's no other changesComment #51
mondrakethe name of the methods on the CR is outdated
Comment #52
andypostah, yes,
s/GdImage/Image- updated IS and CRComment #53
andypostComment #54
andypostComment #56
smustgrave commentedSeems random failure.
Comment #58
andypostComment #59
quietone commentedI have updated the CR for readability and converted to a table.
Comment #61
andypostneeds update to 10.2.0
Comment #62
andypostUpdated
Comment #63
smustgrave commentedChange looks good. Hopefully can get in early for 10.2
Comment #65
longwaveCommitted and pushed 3b44eb23976 to 11.x. Thanks!
Comment #67
quietone commentedPublished the change record.
Comment #69
heddnUpdated the CR to say 10.2 since it doesn't (appear) to have landed in 10.1.