Follow-up to #2246679: Make Language module's LanguageInterface (to be ConfigurableLanguageInterface) extend Core's LanguageInterface

Problem/Motivation

@alexpott mentioned we should not have a setId() in core/lib/Drupal/Core/Language/LanguageInterface.php and just set it in the constructor (and the id should not change after it is created).

Proposed resolution

Remove setId() from core/lib/Drupal/Core/Language/LanguageInterface.php, core/modules/language/src/Entity/Language.php.

Remaining tasks

User interface changes

No

API changes

Yes

Changes:

  • remove method setId()

Related issues

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alimac’s picture

Title: Make Language module's LanguageInterface (to be ConfigurableLanguageInterface) extend Core's LanguageInterface » remove method setId() from core/lib/Drupal/Core/Language/LanguageInterface
alimac’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

@alexpott pointed out that the setId() method was only used in tests (in phpStorm, right click on the method name and select Find Usages to find usages of a particular method). The value of id is set in the constructor and should not be modified afterward.

This patch removes the setId() method from core/lib/Drupal/Core/Language/LanguageInterface.php, core/modules/language/src/Entity/Language.php, core/lib/Drupal/Core/Language/Language.php and the tests.

alimac’s picture

Attaching patch.

Status: Needs review » Needs work

The last submitted patch, 3: 2318817-remove-setid-from-language-interface-2.patch, failed testing.

alimac’s picture

Status: Needs work » Needs review
FileSize
1.98 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 5: 2318817-remove-setid-from-language-interface-5.patch, failed testing.

alimac’s picture

Rerolled, again.

alimac’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 7: 2318817-remove-setid-from-language-interface-6.patch, failed testing.

alimac’s picture

Status: Needs work » Needs review
FileSize
2.7 KB

I fixed the syntax problem and @jmolivas spotted an extraneous line of code. Hopefully this one passes.

thehong’s picture

I think we need add some more comment to Language.id, why there's no setter for it.

YesCT’s picture

@thehong hm. Maybe, but this is a common pattern already in core. (of having an id property, a id() function (getter) and no setter).

If we wanted to add a comment explaining that id was immutable and has no setter on purpose... it would be a separate issue to find all the classes that have a protected id which does not have a setter, and add a similar comment to all of them.

martin107’s picture

Status: Needs review » Reviewed & tested by the community

I agree with the thrust of the issue...

In terms of review....

1) The implementation makes complete sense.
2) The immutability argument in #12 follows a recognizable established pattern.
3) After a slow and steady scan of the patch - nothing is out scope or inappropriate.

So +1 from me.

thehong’s picture

+++ b/core/tests/Drupal/Tests/Core/Language/LanguageUnitTest.php
@@ -47,12 +47,11 @@ public function testGetName() {
+    $language = new Language(array('id'=>$language_code));

@@ -115,20 +114,17 @@ public function testSortArrayOfLanguages(array $languages, array $expected) {
+    $language9A = new Language(array('id'=>'dd'));
...
+    $language10A = new Language(array('id'=>'ee'));
...
+    $language10B = new Language(array('id'=>'ff'));

I think this is small issue with coding standard, should change to array('key' => 'value')

thehong’s picture

Status: Reviewed & tested by the community » Needs work
martin107’s picture

@thehong forgive me but I don't understand what you are saying.

thehong’s picture

@martin107, the patch is just missing spaces around '=>'. Please check more at https://www.drupal.org/coding-standards#array

martin107’s picture

Status: Needs work » Needs review
FileSize
2.71 KB
1.32 KB

Ah thank you ... fixed.

penyaskito’s picture

Status: Needs review » Reviewed & tested by the community

Much more sense. RTBC.

penyaskito’s picture

Status: Reviewed & tested by the community » Needs work

Regret.

+++ b/core/tests/Drupal/Tests/Core/Language/LanguageUnitTest.php
@@ -47,12 +47,11 @@ public function testGetName() {
   public function testGetLangcode() {
     $language_code = $this->randomMachineName(2);
-    $this->assertSame($this->language, $this->language->setId($language_code));
-    $this->assertSame($language_code, $this->language->getId());
...
+    $this->assertSame($language_code, $language->getId());
   }

Can we use $this->language per consistence with the other tests?

  • alexpott committed 4b42df3 on 8.0.x
    Issue #2318817 by alimac, martin107: Remove method setId() from core/lib...
alexpott’s picture

Status: Needs work » Fixed

Committed 4b42df3 and pushed to 8.0.x. Thanks!

@penyaskito don't think that matters - it's a test - file a followup issue if you like.

Status: Fixed » Closed (fixed)

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