GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 8.x-2.x ( bdb6af...21ed67 )
by Devin
56:27
created

DisplayModeContext::describeViewMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acquia\DFExtension\Context;
4
5
use Behat\Gherkin\Node\PyStringNode;
6
use Drupal\Core\Entity\Entity\EntityViewMode;
7
use Drupal\Core\Entity\EntityViewModeInterface;
8
use Drupal\DrupalExtension\Context\DrupalSubContextBase;
9
use Webmozart\Assert\Assert;
10
11
/**
12
 * A context for working with entity display modes.
13
 */
14
class DisplayModeContext extends DrupalSubContextBase {
15
16
  /**
17
   * Sets the customization status of a view mode.
18
   *
19
   * @param string $entity_type
20
   *   The ID of the affected entity type.
21
   * @param string $bundle
22
   *   (optional) The ID of the affected bundle. If omitted, the entity type is
23
   *   presumed to not support bundles.
24
   * @param string $view_mode
25
   *   (optional) The view mode ID. Defaults to 'default'.
26
   * @param bool $desired_status
27
   *   (optional) Whether to customize or uncustomize the view mode.
28
   *
29
   * @When I customize :entity_type display
30
   * @When I customize the :view_mode :entity_type display
31
   * @When I customize the display of the :bundle :entity_type type
32
   * @When I customize the :view_mode display of the :bundle :entity_type type
33
   */
34
  public function setViewModeCustomization($entity_type, $bundle = NULL, $view_mode = 'default', $desired_status = TRUE) {
35
    $display = entity_get_display($entity_type, $bundle, $view_mode);
0 ignored issues
show
Deprecated Code introduced by
The function entity_get_display() has been deprecated with message: as of Drupal 8.0.x, will be removed before Drupal 9.0.0. If the display is available in configuration use: @code \Drupal::entityTypeManager() ->getStorage('entity_view_display') ->load($entity_type . '.' . $bundle . '.' . $view_mode); @endcode When the display is not available in configuration, you can create a new EntityViewDisplay object using: @code $values = array( 'targetEntityType' => $entity_type, 'bundle' => $bundle, 'mode' => $view_mode, 'status' => TRUE, ); \Drupal::entityTypeManager() ->getStorage('entity_view_display') ->create($values); @endcode

This function has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
36
37
    if ($display->isNew()) {
38
      $display = entity_get_display($entity_type, $bundle, 'default')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Drupal\Core\Entity\Entity as the method set() does only exist in the following sub-classes of Drupal\Core\Entity\Entity: Drupal\Core\Config\Entity\ConfigEntityBase, Drupal\Core\Config\Entity\ConfigEntityBundleBase, Drupal\Core\Datetime\Entity\DateFormat, Drupal\Core\Entity\ContentEntityBase, Drupal\Core\Entity\EntityDisplayBase, Drupal\Core\Entity\EntityDisplayModeBase, Drupal\Core\Entity\Entity\EntityFormDisplay, Drupal\Core\Entity\Entity\EntityFormMode, Drupal\Core\Entity\Entity\EntityViewDisplay, Drupal\Core\Entity\Entity\EntityViewMode, Drupal\Core\Entity\RevisionableContentEntityBase, Drupal\Core\Field\Entity\BaseFieldOverride, Drupal\Core\Field\FieldConfigBase, Drupal\Tests\Core\Config...seWithPluginCollections, Drupal\Tests\Core\Config...tyWithPluginCollections, Drupal\Tests\search\Unit\TestSearchPage, Drupal\address\Entity\Zone, Drupal\aggregator\Entity\Feed, Drupal\aggregator\Entity\Item, Drupal\block\Entity\Block, Drupal\block_content\Entity\BlockContent, Drupal\block_content\Entity\BlockContentType, Drupal\comment\Entity\Comment, Drupal\comment\Entity\CommentType, Drupal\config_test\Entity\ConfigQueryTest, Drupal\config_test\Entity\ConfigTest, Drupal\contact\Entity\ContactForm, Drupal\contact\Entity\Message, Drupal\content_moderatio...\ContentModerationState, Drupal\content_moderation\Entity\ModerationState, Drupal\content_moderatio...derationStateTransition, Drupal\content_translati...estTranslatableNoUISkip, Drupal\content_translati...yTestTranslatableUISkip, Drupal\crop\Entity\Crop, Drupal\crop\Entity\CropType, Drupal\ctools_wizard_tes...ity\ExampleConfigEntity, Drupal\devel_entity_test...evelEntityTestCanonical, Drupal\devel_entity_test...ity\DevelEntityTestEdit, Drupal\devel_entity_test...\DevelEntityTestNoLinks, Drupal\editor\Entity\Editor, Drupal\embed\Entity\EmbedButton, Drupal\entity\Revision\R...onableContentEntityBase, Drupal\entity_browser\Entity\EntityBrowser, Drupal\entity_gallery\Entity\EntityGallery, Drupal\entity_gallery\Entity\EntityGalleryType, Drupal\entity_module_test\Entity\EnhancedEntity, Drupal\entity_module_tes...ty\EnhancedEntityBundle, Drupal\entity_test\Entity\EntityTest, Drupal\entity_test\Entity\EntityTestAdminRoutes, Drupal\entity_test\Entit...ityTestBaseFieldDisplay, Drupal\entity_test\Entity\EntityTestBundle, Drupal\entity_test\Entity\EntityTestCache, Drupal\entity_test\Entit...TestCompositeConstraint, Drupal\entity_test\Entit...TestConstraintViolation, Drupal\entity_test\Entity\EntityTestConstraints, Drupal\entity_test\Entity\EntityTestDefaultAccess, Drupal\entity_test\Entity\EntityTestDefaultValue, Drupal\entity_test\Entity\EntityTestFieldMethods, Drupal\entity_test\Entity\EntityTestFieldOverride, Drupal\entity_test\Entity\EntityTestLabel, Drupal\entity_test\Entity\EntityTestLabelCallback, Drupal\entity_test\Entity\EntityTestMul, Drupal\entity_test\Entity\EntityTestMulChanged, Drupal\entity_test\Entit...tityTestMulDefaultValue, Drupal\entity_test\Entity\EntityTestMulLangcodeKey, Drupal\entity_test\Entity\EntityTestMulRev, Drupal\entity_test\Entity\EntityTestMulRevChanged, Drupal\entity_test\Entit...TestMultiValueBasefield, Drupal\entity_test\Entity\EntityTestNew, Drupal\entity_test\Entity\EntityTestNoId, Drupal\entity_test\Entity\EntityTestNoLabel, Drupal\entity_test\Entity\EntityTestRev, Drupal\entity_test\Entity\EntityTestStringId, Drupal\entity_test\Entity\EntityTestUpdate, Drupal\entity_test\Entity\EntityTestViewBuilder, Drupal\entity_test\Entity\EntityTestWithBundle, Drupal\entity_test\Entit...tityTestWithRevisionLog, Drupal\features\Entity\FeaturesBundle, Drupal\field\Entity\FieldConfig, Drupal\field\Entity\FieldStorageConfig, Drupal\file\Entity\File, Drupal\file_entity\Entity\FileEntity, Drupal\file_entity\Entity\FileType, Drupal\filter\Entity\FilterFormat, Drupal\image\Entity\ImageStyle, Drupal\inline_entity_for...EntityTestWithoutBundle, Drupal\language\Entity\ConfigurableLanguage, Drupal\language\Entity\ContentLanguageSettings, Drupal\language_test\Entity\NoLanguageEntityTest, Drupal\lightning_core\Entity\EntityFormMode, Drupal\lightning_core\Entity\EntityViewMode, Drupal\lightning_core\Entity\Role, Drupal\media_entity\Entity\Media, Drupal\media_entity\Entity\MediaBundle, Drupal\menu_link_content\Entity\MenuLinkContent, Drupal\message\Entity\Message, Drupal\message\Entity\MessageTemplate, Drupal\metatag\Entity\MetatagDefaults, Drupal\migrate_entity_te...tity\StringIdEntityTest, Drupal\migrate_plus\Entity\Migration, Drupal\migrate_plus\Entity\MigrationGroup, Drupal\moderation_note\Entity\ModerationNote, Drupal\module_installer_...t\Entity\TestConfigType, Drupal\multiversion\Entity\MenuLinkContent, Drupal\multiversion\Entity\Workspace, Drupal\multiversion\Entity\WorkspaceType, Drupal\node\Entity\Node, Drupal\node\Entity\NodeType, Drupal\page_manager\Entity\Page, Drupal\page_manager\Entity\PageVariant, Drupal\pathauto\Entity\PathautoPattern, Drupal\rdf\Entity\RdfMapping, Drupal\replication\Entity\ReplicationLog, Drupal\replication\Entity\ReplicationSettings, Drupal\responsive_image\...ty\ResponsiveImageStyle, Drupal\responsive_preview\Entity\Device, Drupal\rest\Entity\RestResourceConfig, Drupal\rules\Entity\ReactionRuleConfig, Drupal\rules\Entity\RulesComponentConfig, Drupal\scheduled_updates\Entity\ScheduledUpdate, Drupal\scheduled_updates...ity\ScheduledUpdateType, Drupal\search\Entity\SearchPage, Drupal\search_api\Entity\Index, Drupal\search_api\Entity\Server, Drupal\search_api\Entity\Task, Drupal\search_autocomple...completionConfiguration, Drupal\shortcut\Entity\Shortcut, Drupal\shortcut\Entity\ShortcutSet, Drupal\system\Entity\Action, Drupal\system\Entity\Menu, Drupal\taxonomy\Entity\Term, Drupal\taxonomy\Entity\Vocabulary, Drupal\tour\Entity\Tour, Drupal\user\Entity\Role, Drupal\user\Entity\User, Drupal\views\Entity\View, Drupal\workbench_moderation\Entity\ModerationState, Drupal\workbench_moderat...derationStateTransition, Drupal\workspace\Entity\Replication, Drupal\workspace\Entity\WorkspacePointer. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
Deprecated Code introduced by
The function entity_get_display() has been deprecated with message: as of Drupal 8.0.x, will be removed before Drupal 9.0.0. If the display is available in configuration use: @code \Drupal::entityTypeManager() ->getStorage('entity_view_display') ->load($entity_type . '.' . $bundle . '.' . $view_mode); @endcode When the display is not available in configuration, you can create a new EntityViewDisplay object using: @code $values = array( 'targetEntityType' => $entity_type, 'bundle' => $bundle, 'mode' => $view_mode, 'status' => TRUE, ); \Drupal::entityTypeManager() ->getStorage('entity_view_display') ->create($values); @endcode

This function has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
39
        ->createDuplicate()
40
        ->set('mode', $view_mode);
41
42
      // If the display was created anew, its previous status is effectively
43
      // FALSE.
44
      $original_status = FALSE;
45
    }
46
    else {
47
      $original_status = $display->status();
48
    }
49
50
    /** @var UndoContext $undo */
51
    $undo = $this->getContext(UndoContext::class);
52
    if ($undo && $desired_status != $original_status) {
53
      $arguments = array_slice(func_get_args(), 0, 3);
54
      $arguments[] = $original_status;
55
      $undo->push([$this, __FUNCTION__], $arguments);
56
    }
57
58
    $display->setStatus($desired_status)->save();
59
  }
60
61
  /**
62
   * Sets a description on an entity view mode.
63
   *
64
   * @param string $id
65
   *   The view mode ID.
66
   * @param \Behat\Gherkin\Node\PyStringNode $description
67
   *   The view mode description.
68
   *
69
   * @When I describe the :id view mode:
70
   */
71
  public function describeViewMode($id, PyStringNode $description) {
72
    $view_mode = EntityViewMode::load($id);
73
74
    Assert::isInstanceOf($view_mode, EntityViewModeInterface::class);
75
76
    /** @var UndoContext $undo */
77
    $undo = $this->getContext(UndoContext::class);
78
    if ($undo) {
79
      $original_description = $view_mode
80
        ->getThirdPartySetting('df_core', 'description');
81
82
      $original = new PyStringNode([$original_description], 0);
83
      $undo->push([$this, __FUNCTION__], [$id, $original]);
84
    }
85
86
    $view_mode
87
      ->setThirdPartySetting('df_core', 'description', (string) $description)
88
      ->save();
89
  }
90
91
}
92