Completed
Pull Request — master (#157)
by
unknown
01:35
created

ConfigurableLanguageDrupal8   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 6 1
C save() 0 23 7
1
<?php
2
namespace Drupal\Driver\Plugin\DriverEntity;
3
4
use Drupal\Driver\Plugin\DriverEntityPluginDrupal8Base;
5
use Drupal\Core\Language\LanguageManager;
6
use Drupal\Core\Language\LanguageInterface;
7
8
/**
9
 * A driver field plugin used to test selecting an arbitrary plugin.
10
 *
11
 * @DriverEntity(
12
 *   id = "configurable_language8",
13
 *   version = 8,
14
 *   weight = -100,
15
 *   entityTypes = {
16
 *     "configurable_language",
17
 *   },
18
 * )
19
 */
20
class ConfigurableLanguageDrupal8 extends DriverEntityPluginDrupal8Base
21
{
22
23
  /**
24
   * The langcode of the attached language.
25
   *
26
   * @var string
27
   *
28
   * @deprecated Use id() instead.
29
   */
30
    public $langcode;
31
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function load($entityId)
37
    {
38
        $entity = parent::load($entityId);
39
        $this->langcode = $this->id();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->id() can also be of type integer. However, the property $langcode is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Deprecated Code introduced by
The property Drupal\Driver\Plugin\Dri...guageDrupal8::$langcode has been deprecated with message: Use id() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
40
        return $entity;
41
    }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46
    public function save()
47
    {
48
        // For new entities, fill in details for known languages.
49
        // This is something that ConfigurableLanguage::createFromLangcode() does, in
50
        // order to allow enabling a language by langcode alone.
51
        if ($this->getEntity()->isNew()) {
52
          $langcode = $this->getEntity()->id();
53
          $standard_languages = LanguageManager::getStandardLanguageList();
54
          if (isset($standard_languages[$langcode])) {
55
            $label = $this->getEntity()->get('label');
56
            // Label defaults to langcode.
57
            if (empty($label) || $label === $langcode) {
58
              $this->set('label', $standard_languages[$langcode][0]);
59
            }
60
            if (empty($this->getEntity()->get('direction'))) {
61
              $direction = isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : LanguageInterface::DIRECTION_LTR;
62
              $this->set('direction', $direction);
63
            }
64
          }
65
        }
66
        parent::save();
67
        $this->langcode = $this->id();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->id() can also be of type integer. However, the property $langcode is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Deprecated Code introduced by
The property Drupal\Driver\Plugin\Dri...guageDrupal8::$langcode has been deprecated with message: Use id() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
68
    }
69
70
}
71