Problem/Motivation

Part of the effort to remove the drupal_static() usages: #1577902: [META] Remove all usages of drupal_static() & drupal_static_reset() .

AutoIncrementingTestItem used for testing, still uses drupal_static().

Proposed resolution

Replace the usage with a class property.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

N/A

Comments

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Status: Needs work » Needs review
StatusFileSize
new943 bytes

Patch.

andypost’s picture

+++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/AutoIncrementingTestItem.php
@@ -17,6 +17,13 @@
+  protected static $cache = 0;

as it used in private method maybe better to make var private so it will not be serialized?

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

martin107’s picture

StatusFileSize
new941 bytes
new661 bytes

Everytime I read stackoverflow concerning static properties -- andypost's serialisation comment comes up.

So I think the change makes sense to me

@claudiu.cristea - thanks for all the effort you have made recently into removing drupal_static(() calls from core.
It is really good to see.

claudiu.cristea++

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Nice

catch’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/AutoIncrementingTestItem.php
@@ -17,6 +17,13 @@
 
+  /**
+   * Memory cache.
+   *
+   * @var int
+   */
+  private static $cache = 0;
+
   /**
    * {@inheritdoc}
    */
@@ -32,8 +39,7 @@ public function preSave() {

@@ -32,8 +39,7 @@ public function preSave() {
    *   The incremented field value.
    */
   private static function getIncrementedFieldValue() {
-    $current_value = &drupal_static(__METHOD__, 0);
-    return ++$current_value;
+    return ++static::$cache;
   }
 

Since the variable is only used in this one method, could it just be declared in the method itself instead of as a static class property?

claudiu.cristea’s picture

StatusFileSize
new904 bytes
new756 bytes

OK, better.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

nice

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 59f1d6c and pushed to 8.8.x. Thanks!

  • catch committed 59f1d6c on 8.8.x
    Issue #3035297 by claudiu.cristea, martin107, andypost: Don't use...

Status: Fixed » Closed (fixed)

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