From 74b0acbefcd57afb3b6006f65e9d02fa56325f16 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 17 Oct 2022 11:41:00 +0100
Subject: [PATCH] Issue #3259109 by mondrake, longwave, andregp, daffie,
 mglaman, neclimdul, alexpott: Fix 'Cannot unset offset' PHPStan L0 errors

---
 .../tests/src/Unit/MigrationStateUnitTest.php |  1 -
 .../views/display/DisplayPluginBase.php       |  1 -
 core/phpstan-baseline.neon                    | 35 -------------------
 .../PhpStorage/FileStorageReadOnlyTest.php    |  4 +--
 .../Component/PhpStorage/FileStorageTest.php  |  4 +--
 .../PhpStorage/PhpStorageTestBase.php         |  4 +--
 .../ActiveLinkResponseFilterTest.php          | 34 +++++++-----------
 .../Core/Render/RendererPlaceholdersTest.php  |  2 +-
 8 files changed, 19 insertions(+), 66 deletions(-)

diff --git a/core/modules/migrate_drupal/tests/src/Unit/MigrationStateUnitTest.php b/core/modules/migrate_drupal/tests/src/Unit/MigrationStateUnitTest.php
index 3c770c78471c..ea4b54356ea6 100644
--- a/core/modules/migrate_drupal/tests/src/Unit/MigrationStateUnitTest.php
+++ b/core/modules/migrate_drupal/tests/src/Unit/MigrationStateUnitTest.php
@@ -396,7 +396,6 @@ public function providerGetUpgradeStates() {
     // Test menu migration with menu_ui uninstalled.
     $tests[3] = $tests[1];
     unset($tests[3]['modules_to_enable']['menu_ui']);
-    unset($tests[3]['files']['menu_ui']);
     unset($tests[3]['migrations']['menu_ui']);
     $tests[3]['expected_7'] = [
       MigrationState::NOT_FINISHED => [
diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
index c9f24b6aa993..1eed2d4c2be7 100644
--- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
+++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
@@ -449,7 +449,6 @@ public function defaultableSections($section = NULL) {
     // If the display cannot use a pager, then we cannot default it.
     if (!$this->usesPager()) {
       unset($sections['pager']);
-      unset($sections['items_per_page']);
     }
 
     foreach ($this->extenders as $extender) {
diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon
index a6575c457496..e61ca1c182b5 100644
--- a/core/phpstan-baseline.neon
+++ b/core/phpstan-baseline.neon
@@ -885,11 +885,6 @@ parameters:
 			count: 1
 			path: modules/migrate/tests/src/Kernel/MigrateTestBase.php
 
-		-
-			message: "#^Cannot unset offset 'menu_ui' on array\\{system\\: \"finished\\:\\\\n  6\\:\\\\n   …\", menu_link_content\\: \"finished\\:\\\\n  6\\:\\\\n   …\", menu\\: \"finished\\:\\\\n  6\\:\\\\n   …\"\\}\\.$#"
-			count: 1
-			path: modules/migrate_drupal/tests/src/Unit/MigrationStateUnitTest.php
-
 		-
 			message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:10\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#"
 			count: 2
@@ -1405,11 +1400,6 @@ parameters:
 			count: 1
 			path: modules/views/src/Plugin/views/cache/Time.php
 
-		-
-			message: "#^Cannot unset offset 'items_per_page' on array\\{access\\: array\\{'access'\\}, cache\\: array\\{'cache'\\}, title\\: array\\{'title'\\}, css_class\\: array\\{'css_class'\\}, use_ajax\\: array\\{'use_ajax'\\}, hide_attachment_summary\\: array\\{'hide_attachment…'\\}, show_admin_links\\: array\\{'show_admin_links'\\}, group_by\\: array\\{'group_by'\\}, \\.\\.\\.\\}\\.$#"
-			count: 1
-			path: modules/views/src/Plugin/views/display/DisplayPluginBase.php
-
 		-
 			message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:10\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#"
 			count: 2
@@ -1705,21 +1695,6 @@ parameters:
 			count: 1
 			path: tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php
 
-		-
-			message: "#^Cannot unset offset int on array\\<string, mixed\\>\\.$#"
-			count: 1
-			path: tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php
-
-		-
-			message: "#^Cannot unset offset int on array\\<string, mixed\\>\\.$#"
-			count: 1
-			path: tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php
-
-		-
-			message: "#^Cannot unset offset int on array\\<string, mixed\\>\\.$#"
-			count: 1
-			path: tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
-
 		-
 			message: "#^Result of static method Drupal\\\\Composer\\\\Composer\\:\\:ensureComposerVersion\\(\\) \\(void\\) is used\\.$#"
 			count: 1
@@ -1745,11 +1720,6 @@ parameters:
 			count: 1
 			path: tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php
 
-		-
-			message: "#^Cannot unset offset 'data\\-drupal\\-link…' on array\\{data\\-drupal\\-link\\-system\\-path\\: '\\<front\\>'\\}\\.$#"
-			count: 2
-			path: tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php
-
 		-
 			message: "#^Call to method getDefinitions\\(\\) on an unknown class Drupal\\\\Core\\\\Plugin\\\\CategorizingPluginManagerTrait\\.$#"
 			count: 3
@@ -1780,11 +1750,6 @@ parameters:
 			count: 1
 			path: tests/Drupal/Tests/Core/Render/RendererCallbackTest.php
 
-		-
-			message: "#^Cannot unset offset '\\#cache' on array\\{\\#lazy_builder\\: array\\{'Drupal\\\\\\\\Tests\\\\\\\\Core…', array\\{mixed\\}\\}\\}\\.$#"
-			count: 1
-			path: tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
-
 		-
 			message: "#^Call to an undefined method Drupal\\\\Tests\\\\Core\\\\Test\\\\TestClass\\:\\:assertArrayHasKey\\(\\)\\.$#"
 			count: 3
diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php b/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php
index c5df7508d6f5..907f8c5688df 100644
--- a/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php
+++ b/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php
@@ -57,11 +57,11 @@ public function testReadOnly() {
 
     // Find a global that doesn't exist.
     do {
-      $random = mt_rand(10000, 100000);
+      $random = 'test' . mt_rand(10000, 100000);
     } while (isset($GLOBALS[$random]));
 
     // Write out a PHP file and ensure it's successfully loaded.
-    $code = "<?php\n\$GLOBALS[$random] = TRUE;";
+    $code = "<?php\n\$GLOBALS['$random'] = TRUE;";
     $success = $php->save($name, $code);
     $this->assertTrue($success);
     $php_read = new FileReadOnlyStorage($this->readonlyStorage);
diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php b/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php
index 2ad99b4b226c..de5fbb21c259 100644
--- a/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php
+++ b/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php
@@ -70,11 +70,11 @@ public function testDeleteAll() {
 
     // Find a global that doesn't exist.
     do {
-      $random = mt_rand(10000, 100000);
+      $random = 'test' . mt_rand(10000, 100000);
     } while (isset($GLOBALS[$random]));
 
     // Write out a PHP file and ensure it's successfully loaded.
-    $code = "<?php\n\$GLOBALS[$random] = TRUE;";
+    $code = "<?php\n\$GLOBALS['$random'] = TRUE;";
     $this->assertTrue($php->save($name, $code), 'Saved php file');
     $php->load($name);
     $this->assertTrue($GLOBALS[$random], 'File saved correctly with correct value');
diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php b/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
index 007a6fa0cbd7..c670bebadf4e 100644
--- a/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
+++ b/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
@@ -39,11 +39,11 @@ public function assertCRUD($php) {
 
     // Find a global that doesn't exist.
     do {
-      $random = mt_rand(10000, 100000);
+      $random = 'test' . mt_rand(10000, 100000);
     } while (isset($GLOBALS[$random]));
 
     // Write out a PHP file and ensure it's successfully loaded.
-    $code = "<?php\n\$GLOBALS[$random] = TRUE;";
+    $code = "<?php\n\$GLOBALS['$random'] = TRUE;";
     $success = $php->save($name, $code);
     $this->assertTrue($success, 'Saved php file');
     $php->load($name);
diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php
index 5e5b06cfcc8f..e639d8c346b1 100644
--- a/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php
+++ b/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php
@@ -160,13 +160,11 @@ public function providerTestSetLinkActiveClass() {
     // Matching path, plus all matching variations.
     $attributes = [
       'data-drupal-link-system-path' => 'llama',
-      'data-drupal-link-query' => Json::encode(['foo' => 'bar']),
     ];
-    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes];
-    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['hreflang' => 'nl']];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
     // Matching path, plus all non-matching variations.
-    $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en']];
-    unset($attributes['data-drupal-link-query']);
+    $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en', 'data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => ""]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => TRUE]];
     // Special non-matching path, plus all variations.
@@ -176,7 +174,6 @@ public function providerTestSetLinkActiveClass() {
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl']];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en']];
-    unset($attributes['data-drupal-link-query']);
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => ""]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => TRUE]];
 
@@ -191,13 +188,11 @@ public function providerTestSetLinkActiveClass() {
     // Matching path, plus all matching variations.
     $attributes = [
       'data-drupal-link-system-path' => 'llama',
-      'data-drupal-link-query' => Json::encode(['foo' => 'bar']),
     ];
-    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes];
-    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['hreflang' => 'nl']];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
     // Matching path, plus all non-matching variations.
-    $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en']];
-    unset($attributes['data-drupal-link-query']);
+    $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en', 'data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['data-drupal-link-query' => ""]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['data-drupal-link-query' => TRUE]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => ""]];
@@ -209,7 +204,6 @@ public function providerTestSetLinkActiveClass() {
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl']];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en']];
-    unset($attributes['data-drupal-link-query']);
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['data-drupal-link-query' => ""]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['data-drupal-link-query' => TRUE]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => ""]];
@@ -226,13 +220,11 @@ public function providerTestSetLinkActiveClass() {
     // Matching path, plus all matching variations.
     $attributes = [
       'data-drupal-link-system-path' => 'my-front-page',
-      'data-drupal-link-query' => Json::encode(['foo' => 'bar']),
     ];
-    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes];
-    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['hreflang' => 'en']];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['hreflang' => 'en', 'data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
     // Matching path, plus all non-matching variations.
-    $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl']];
-    unset($attributes['data-drupal-link-query']);
+    $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['data-drupal-link-query' => ""]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['data-drupal-link-query' => TRUE]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en', 'data-drupal-link-query' => ""]];
@@ -240,13 +232,11 @@ public function providerTestSetLinkActiveClass() {
     // Special matching path, plus all variations.
     $attributes = [
       'data-drupal-link-system-path' => '<front>',
-      'data-drupal-link-query' => Json::encode(['foo' => 'bar']),
     ];
-    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes];
-    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['hreflang' => 'en']];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes + ['hreflang' => 'en', 'data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
     // Special matching path, plus all non-matching variations.
-    $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl']];
-    unset($attributes['data-drupal-link-query']);
+    $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'nl', 'data-drupal-link-query' => Json::encode(['foo' => 'bar'])]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['data-drupal-link-query' => ""]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['data-drupal-link-query' => TRUE]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en', 'data-drupal-link-query' => ""]];
diff --git a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
index 744f78796370..c23cc1943e09 100644
--- a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
+++ b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
@@ -477,7 +477,7 @@ public function providerPlaceholders() {
     // - uncacheable
     $x = $base_element_b;
     $expected_placeholder_render_array = $x['#attached']['placeholders'][(string) $generate_placeholder_markup()];
-    unset($x['#attached']['placeholders'][(string) $generate_placeholder_markup()]['#cache']);
+    $this->assertArrayNotHasKey('#cache', $expected_placeholder_render_array);
     $cases[] = [
       $x,
       $args,
-- 
GitLab