Deprecate resource mentions in GDToolkit method names

Created on 22 February 2022, over 2 years ago
Updated 13 September 2023, about 1 year ago

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

📌 Task
Status

Fixed

Version

11.0 🔥

Component
Image system 

Last updated 4 days ago

Created by

🇫🇷France andypost

Live updates comments and jobs are added and updated live.
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.

  • 🇺🇸United States xjm

    Thanks 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.

    1. +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
      @@ -28,11 +28,9 @@
      -  protected $resource = NULL;
      +  protected ?\GdImage $image = NULL;
      

      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.

    2. +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
      @@ -112,6 +110,47 @@ public static function create(ContainerInterface $container, array $configuratio
      +    if ($name === 'resource') {
      ...
      +    if ($name == 'resource') {
      ...
      +    if ($name == 'resource') {
      ...
      +    if ($name == 'resource') {
      

      Why are some of these == but the others ===?

    3. +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
      @@ -112,6 +110,47 @@ public static function create(ContainerInterface $container, array $configuratio
      +      @trigger_error('Accessing the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
      ...
      +      @trigger_error('Setting the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
      ...
      +      @trigger_error('Checking the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
      ...
      +      @trigger_error('Unsetting the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
      

      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 ::resource and ::image need to be fixed one way or the other.

      Also, these seem to refer to an image method or property that does not exist?

    4. +++ b/core/tests/Drupal/Tests/Component/Utility/RectangleTest.php
      @@ -77,13 +77,10 @@ public function testRotateDimensions($width, $height, $angle, $exp_width, $exp_h
      -   *     if (is_resource($old_res)) {
      -   *       imagedestroy($old_res);
      -   *     }
      

      I wondered why this is being removed from the example code. Answer above is:

      imagedestroy is a noop since PHP 8.0, we can remove it and adjust the inline comment.

      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!

  • 🇺🇸United States xjm

    Saving credits for reviewers.

  • 🇺🇸United States xjm
    +++ b/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php
    @@ -460,9 +460,9 @@ public function testGifTransparentImages(): void {
    +    $gd_mage = $image->getToolkit()->getImage();
    +    $color_index = imagecolorat($gd_mage, $image->getWidth() - 1, 0);
    +    $color = array_values(imagecolorsforindex($gd_mage, $color_index));
    
    @@ -473,17 +473,17 @@ public function testGifTransparentImages(): void {
    +    $gd_mage = $image->getToolkit()->getImage();
    +    $color_index = imagecolorat($gd_mage, $image->getWidth() - 1, 0);
    +    $color = array_values(imagecolorsforindex($gd_mage, $color_index));
    

    One more thing -- the local variable here is mis-named. While it is kind of magic, I think this should be "image" and not "mage". 🧙‍♂️

  • 🇮🇳India _utsavsharma

    Addressed 43.2 and 45.
    Please review.

  • Status changed to Needs review over 1 year ago
  • 🇫🇷France andypost

    re-queued and updated CR with s/resource/image internal BC break

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    The 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.

  • Status changed to Needs review over 1 year ago
  • 🇫🇷France andypost

    Added to summary rename of s/resource/image - there's no other changes

  • Status changed to RTBC over 1 year ago
  • 🇮🇹Italy mondrake 🇮🇹

    the name of the methods on the CR is outdated

  • 🇫🇷France andypost

    ah, yes, s/GdImage/Image - updated IS and CR

    +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
    @@ -28,11 +28,9 @@
    -  protected $resource = NULL;
    +  protected ?\GdImage $image = NULL;
    
    @@ -135,12 +178,43 @@ public function setResource($resource) {
    +  public function setImage(?\GdImage $image): static {
    ...
    +  public function getImage(): ?\GdImage {
    
  • 🇺🇸United States smustgrave

    Seems random failure.

  • last update over 1 year ago
    29,203 pass
  • 2:14
    59:21
    Running
  • 🇳🇿New Zealand quietone

    I have updated the CR for readability and converted to a table.

  • last update over 1 year ago
    29,301 pass
  • last update over 1 year ago
    29,303 pass
  • last update over 1 year ago
    29,305 pass
  • last update over 1 year ago
    29,360 pass
  • last update over 1 year ago
    29,367 pass
  • last update over 1 year ago
    29,367 pass
  • last update over 1 year ago
    29,372 pass
  • last update over 1 year ago
    29,379 pass
  • last update over 1 year ago
    29,380 pass
  • last update over 1 year ago
    29,380 pass, 2 fail
  • last update over 1 year ago
    29,384 pass
  • last update over 1 year ago
    29,389 pass
  • last update over 1 year ago
    29,388 pass, 2 fail
  • last update over 1 year ago
    29,389 pass
  • last update over 1 year ago
    29,389 pass
  • last update over 1 year ago
    29,388 pass, 2 fail
  • 47:14
    46:02
    Running
  • last update over 1 year ago
    29,396 pass
  • last update over 1 year ago
    29,400 pass
  • last update over 1 year ago
    29,400 pass
  • last update over 1 year ago
    29,401 pass
  • last update over 1 year ago
    29,410 pass
  • Status changed to Needs work over 1 year ago
  • 🇫🇷France andypost
    +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
    @@ -112,6 +110,47 @@ public static function create(ContainerInterface $container, array $configuratio
    +      @trigger_error('Accessing the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
    ...
    +      @trigger_error('Setting the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
    ...
    +      @trigger_error('Checking the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
    ...
    +      @trigger_error('Unsetting the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
    
    @@ -120,14 +159,18 @@ public static function create(ContainerInterface $container, array $configuratio
    +   * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use
    ...
    +    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::setImage() instead. See https://www.drupal.org/node/3265963', E_USER_DEPRECATED);
    
    @@ -135,12 +178,43 @@ public function setResource($resource) {
    +   * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use
    ...
    +    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::getImage() instead. See https://www.drupal.org/node/3265963', E_USER_DEPRECATED);
    
    +++ b/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php
    @@ -532,4 +532,29 @@ public function testGetRequirements(): void {
    +    $this->expectDeprecation('Drupal\system\Plugin\ImageToolkit\GDToolkit::setResource() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::setImage() instead. See https://www.drupal.org/node/3265963');
    ...
    +    $this->expectDeprecation('Checking the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.');
    ...
    +    $this->expectDeprecation('Drupal\system\Plugin\ImageToolkit\GDToolkit::getResource() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::getImage() instead. See https://www.drupal.org/node/3265963');
    ...
    +    $this->expectDeprecation('Accessing the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.');
    ...
    +    $this->expectDeprecation('Setting the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.');
    ...
    +    $this->expectDeprecation('Unsetting the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.');
    

    needs update to 10.2.0

  • Status changed to Needs review over 1 year ago
  • last update over 1 year ago
    29,417 pass
  • Status changed to RTBC over 1 year ago
  • 🇺🇸United States smustgrave

    Change looks good. Hopefully can get in early for 10.2

  • last update over 1 year ago
    29,435 pass
    • longwave committed 3b44eb23 on 11.x
      Issue #3265953 by andypost, quietone, ankithashetty, _utsavsharma,...
  • Status changed to Fixed over 1 year ago
  • 🇬🇧United Kingdom longwave UK

    Committed and pushed 3b44eb23976 to 11.x. Thanks!

  • 🇳🇿New Zealand quietone

    Published the change record.

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

  • Status changed to Fixed about 1 year ago
  • heddn Nicaragua

    Updated the CR to say 10.2 since it doesn't (appear) to have landed in 10.1.

Production build 0.71.5 2024