diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php
index 9f651bdc7b8150113b5d07f68352e2683262f6d5..9dec751955e7d0c374c229af0eac5f3a8ab0f6df 100644
--- a/core/.phpstan-baseline.php
+++ b/core/.phpstan-baseline.php
@@ -2146,12 +2146,6 @@
 	'count' => 1,
 	'path' => __DIR__ . '/tests/Drupal/Tests/Composer/ComposerTest.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Call to deprecated method expectError\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\:
-https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
-	'count' => 1,
-	'path' => __DIR__ . '/tests/Drupal/Tests/Core/Config/ConfigTest.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Trying to mock an undefined method getRevisionId\\(\\) on class Drupal\\\\Tests\\\\Core\\\\Entity\\\\UrlTestEntity\\.$#',
 	'count' => 1,
diff --git a/core/lib/Drupal/Component/Utility/NestedArray.php b/core/lib/Drupal/Component/Utility/NestedArray.php
index c9feb599d1b4c000e32ac389d10626c43955f31b..354d58183201f4c244c735e526499e1611914a69 100644
--- a/core/lib/Drupal/Component/Utility/NestedArray.php
+++ b/core/lib/Drupal/Component/Utility/NestedArray.php
@@ -138,7 +138,7 @@ public static function &getValue(array &$array, array $parents, &$key_exists = N
    * @param bool $force
    *   (optional) If TRUE, the value is forced into the structure even if it
    *   requires the deletion of an already existing non-array parent value. If
-   *   FALSE, PHP throws an error if trying to add into a value that is not an
+   *   FALSE, throws an exception if trying to add into a value that is not an
    *   array. Defaults to FALSE.
    *
    * @see NestedArray::unsetValue()
@@ -149,7 +149,10 @@ public static function setValue(array &$array, array $parents, $value, $force =
     foreach ($parents as $parent) {
       // PHP auto-creates container arrays and NULL entries without error if $ref
       // is NULL, but throws an error if $ref is set, but not an array.
-      if ($force && isset($ref) && !is_array($ref)) {
+      if (isset($ref) && !is_array($ref)) {
+        if (!$force) {
+          throw new \LogicException('Cannot create key "' . $parent . '" on non-array value.');
+        }
         $ref = [];
       }
       $ref = &$ref[$parent];
diff --git a/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php b/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php
index f1d4436a5cfc53ebcfd8b39dad7ef6e60b2ba5fb..a48d5bab73f89763748725acfc2e103df30d6f28 100644
--- a/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php
@@ -87,6 +87,10 @@ public function testSetValue() {
     NestedArray::setValue($this->form, $this->parents, $new_value);
     $this->assertSame('New value', $this->form['details']['element']['#value'], 'Changed nested element value found.');
     $this->assertTrue($this->form['details']['element']['#required'], 'New nested element value found.');
+
+    $this->expectException(\LogicException::class);
+    $this->expectExceptionMessage('Cannot create key "child" on non-array value.');
+    NestedArray::setValue($this->form, ['details', 'element', '#value', 'child'], $new_value);
   }
 
   /**
diff --git a/core/tests/Drupal/Tests/Core/Config/ConfigTest.php b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php
index 29032f8f54db59490a786f8de40c1ca38dd3ec1a..4ba8a31f727c7d7f80d484a14eff1dccae9b7c98 100644
--- a/core/tests/Drupal/Tests/Core/Config/ConfigTest.php
+++ b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php
@@ -276,7 +276,8 @@ public function testSetIllegalOffsetValue() {
     $this->config->set('testData', 1);
 
     // Attempt to treat the single value as a nested item.
-    $this->expectError();
+    $this->expectException(\LogicException::class);
+    $this->expectExceptionMessage('Cannot create key "illegalOffset" on non-array value.');
     $this->config->set('testData.illegalOffset', 1);
   }