Problem/Motivation

Anonymous classes are here why not use them?

Steps to reproduce

Scroll down to the end of the RecursiveContextualValidatorTest.php

Proposed resolution

Replace the dummy test class with the anonymous class.

Remaining tasks

  • Review the patch
  • RTCB the patch
  • Rejoice!

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

N/A

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jibran created an issue. See original summary.

jibran’s picture

Status: Active » Needs review
FileSize
858 bytes
lauriii’s picture

Status: Needs review » Needs work

I think we should search if there are other instances where we'd like to use anonymous classes. I found at least \Drupal\Tests\Component\Utility\VariableTest

jibran’s picture

We can't replace the class in VariableTest with anonymous class. Changing it breaks the test.

1) Drupal\Tests\Component\Utility\VariableTest::testExport with data set #12 ('Drupal\Tests\Component\Utilit...y(\n))', class@anonymous/var/www/html/public/core/tests/Drupal/Tests/Component/Utility/VariableTest.php:167$22 Object ())
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Drupal\Tests\Component\Utility\StubVariableTestClass::__set_state(array(\n
-))'
+Binary String: 0x636c61737340616e6f6e796d6f7573002f7661722f7777772f68746d6c2f7075626c69632f636f72652f74657374732f44727570616c2f54657374732f436f6d706f6e656e742f5574696c6974792f5661726961626c65546573742e7068703a3136372432323a3a5f5f7365745f7374617465286172726179280a2929
jibran’s picture

I tired searching for following pattern class\s+([a-zA-Z0-9_]+)\s*\{(\s*)\} in core and didn't find any worthy candidates.

Anchal_gupta’s picture

I have fixed the custom command that failed. Please review it

jibran’s picture

Status: Needs work » Needs review
FileSize
739 bytes
1.05 KB

This should be the correct way to fix this.

Wim Leers’s picture

Priority: Normal » Minor
Status: Needs review » Reviewed & tested by the community

Nice clean-up!

alexpott’s picture

Version: 10.1.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 6cbea8d6fd to 10.1.x and 2f58ef657a to 10.0.x and 9c8bdd9b80 to 9.5.x. Thanks!

Backported to 9.5.x to keep everything aligned.

diff --git a/core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php b/core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php
index 7f39f80b5f..694922a8df 100644
--- a/core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php
+++ b/core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php
@@ -253,7 +253,7 @@ public function testValidatePropertyWithInvalidObjects($object) {
   public function providerTestValidatePropertyWithInvalidObjects() {
     $data = [];
     $data[] = [new \stdClass()];
-    $data[] = [new class {}]; // phpcs:ignore
+    $data[] = [new class() {}];
 
     $data[] = [$this->createMock('Drupal\Core\TypedData\TypedDataInterface')];
 

Better way to fix the coding standard issue.

  • alexpott committed 6cbea8d on 10.1.x
    Issue #3311214 by jibran, Anchal_gupta: Replace TestClass with anonymous...

  • alexpott committed 2f58ef6 on 10.0.x
    Issue #3311214 by jibran, Anchal_gupta: Replace TestClass with anonymous...

  • alexpott committed 9c8bdd9 on 9.5.x
    Issue #3311214 by jibran, Anchal_gupta: Replace TestClass with anonymous...

Status: Fixed » Closed (fixed)

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