From 59b450939fe923baf5416a4d6bab742fdb5e68c0 Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Thu, 23 Feb 2017 23:20:26 +0000
Subject: [PATCH] Issue #2854926 by xjm, himanshu-dixit, borisson_, boaloysius,
 alexpott: Remove unneeded control structures in ContentEntityBase

---
 .../Drupal/Core/Entity/ContentEntityBase.php  | 121 +++++++++---------
 1 file changed, 59 insertions(+), 62 deletions(-)

diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
index 5cd27c7c61de..6279199fe5fa 100644
--- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php
+++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
@@ -1010,14 +1010,9 @@ public function __set($name, $value) {
    * Implements the magic method for isset().
    */
   public function __isset($name) {
-    // "Official" Field API fields are always set.
-    if ($this->hasField($name)) {
-      return TRUE;
-    }
-    // For non-field properties, check the internal values.
-    else {
-      return isset($this->values[$name]);
-    }
+    // "Official" Field API fields are always set. For non-field properties,
+    // check the internal values.
+    return $this->hasField($name) ? TRUE : isset($this->values[$name]);
   }
 
   /**
@@ -1067,60 +1062,62 @@ public function createDuplicate() {
   public function __clone() {
     // Avoid deep-cloning when we are initializing a translation object, since
     // it will represent the same entity, only with a different active language.
-    if (!$this->translationInitialize) {
-      // The translation is a different object, and needs its own TypedData
-      // adapter object.
-      $this->typedData = NULL;
-      $definitions = $this->getFieldDefinitions();
-
-      // The translation cache has to be cleared before cloning the fields
-      // below so that the call to getTranslation() does not re-use the
-      // translation objects of the old entity but instead creates new
-      // translation objects from the newly cloned entity. Otherwise the newly
-      // cloned field item lists would hold references to the old translation
-      // objects in their $parent property after the call to setContext().
-      $this->clearTranslationCache();
-
-      // Because the new translation objects that are created below are
-      // themselves created by *cloning* the newly cloned entity we need to
-      // make sure that the references to property values are properly cloned
-      // before cloning the fields. Otherwise calling
-      // $items->getEntity()->isNew(), for example, would return the
-      // $enforceIsNew value of the old entity.
-
-      // Ensure the translations array is actually cloned by overwriting the
-      // original reference with one pointing to a copy of the array.
-      $translations = $this->translations;
-      $this->translations = &$translations;
-
-      // Ensure that the following properties are actually cloned by
-      // overwriting the original references with ones pointing to copies of
-      // them: enforceIsNew, newRevision, loadedRevisionId and fields.
-      $enforce_is_new = $this->enforceIsNew;
-      $this->enforceIsNew = &$enforce_is_new;
-
-      $new_revision = $this->newRevision;
-      $this->newRevision = &$new_revision;
-
-      $original_revision_id = $this->loadedRevisionId;
-      $this->loadedRevisionId = &$original_revision_id;
-
-      $fields = $this->fields;
-      $this->fields = &$fields;
-
-      foreach ($this->fields as $name => $values) {
-        $this->fields[$name] = array();
-        // Untranslatable fields may have multiple references for the same field
-        // object keyed by language. To avoid creating different field objects
-        // we retain just the original value, as references will be recreated
-        // later as needed.
-        if (!$definitions[$name]->isTranslatable() && count($values) > 1) {
-          $values = array_intersect_key($values, array(LanguageInterface::LANGCODE_DEFAULT => TRUE));
-        }
-        foreach ($values as $langcode => $items) {
-          $this->fields[$name][$langcode] = clone $items;
-          $this->fields[$name][$langcode]->setContext($name, $this->getTranslation($langcode)->getTypedData());
-        }
+    if ($this->translationInitialize) {
+      return;
+    }
+
+    // The translation is a different object, and needs its own TypedData
+    // adapter object.
+    $this->typedData = NULL;
+    $definitions = $this->getFieldDefinitions();
+
+    // The translation cache has to be cleared before cloning the fields
+    // below so that the call to getTranslation() does not re-use the
+    // translation objects of the old entity but instead creates new
+    // translation objects from the newly cloned entity. Otherwise the newly
+    // cloned field item lists would hold references to the old translation
+    // objects in their $parent property after the call to setContext().
+    $this->clearTranslationCache();
+
+    // Because the new translation objects that are created below are
+    // themselves created by *cloning* the newly cloned entity we need to
+    // make sure that the references to property values are properly cloned
+    // before cloning the fields. Otherwise calling
+    // $items->getEntity()->isNew(), for example, would return the
+    // $enforceIsNew value of the old entity.
+
+    // Ensure the translations array is actually cloned by overwriting the
+    // original reference with one pointing to a copy of the array.
+    $translations = $this->translations;
+    $this->translations = &$translations;
+
+    // Ensure that the following properties are actually cloned by
+    // overwriting the original references with ones pointing to copies of
+    // them: enforceIsNew, newRevision, loadedRevisionId and fields.
+    $enforce_is_new = $this->enforceIsNew;
+    $this->enforceIsNew = &$enforce_is_new;
+
+    $new_revision = $this->newRevision;
+    $this->newRevision = &$new_revision;
+
+    $original_revision_id = $this->loadedRevisionId;
+    $this->loadedRevisionId = &$original_revision_id;
+
+    $fields = $this->fields;
+    $this->fields = &$fields;
+
+    foreach ($this->fields as $name => $values) {
+      $this->fields[$name] = array();
+      // Untranslatable fields may have multiple references for the same field
+      // object keyed by language. To avoid creating different field objects
+      // we retain just the original value, as references will be recreated
+      // later as needed.
+      if (!$definitions[$name]->isTranslatable() && count($values) > 1) {
+        $values = array_intersect_key($values, array(LanguageInterface::LANGCODE_DEFAULT => TRUE));
+      }
+      foreach ($values as $langcode => $items) {
+        $this->fields[$name][$langcode] = clone $items;
+        $this->fields[$name][$langcode]->setContext($name, $this->getTranslation($langcode)->getTypedData());
       }
     }
   }
-- 
GitLab