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

PanelsInPlaceContext   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 257
Duplicated Lines 11.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 30
loc 257
rs 10
c 1
b 0
f 0
wmc 18
lcom 1
cbo 5

14 Methods

Rating   Name   Duplication   Size   Complexity  
A gatherContexts() 0 3 1
A getActiveTab() 0 4 1
A assertTray() 0 3 1
A openTab() 0 18 2
A closeEditor() 0 14 1
A openCategory() 0 18 2
A assertBlockPluginExists() 8 8 2
A assertBlockPluginNotExists() 8 8 2
A instantiateBlock() 0 9 1
A saveLayout() 0 5 1
A saveLayoutAsCustom() 7 7 1
A saveLayoutAsDefault() 7 7 1
A placeBlock() 0 6 1
A revertLayout() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Acquia\DFExtension\Context;
4
5
use Drupal\DrupalExtension\Context\DrupalSubContextBase;
6
use Drupal\DrupalExtension\Context\MinkContext;
7
8
/**
9
 * A context for working with the Panels in-place editor.
10
 */
11
class PanelsInPlaceContext extends DrupalSubContextBase {
12
13
  /**
14
   * The Mink context.
15
   *
16
   * @var MinkContext
17
   */
18
  protected $minkContext;
19
20
  /**
21
   * Gathers required contexts.
22
   *
23
   * @BeforeScenario
24
   */
25
  public function gatherContexts() {
26
    $this->minkContext = $this->getContext(MinkContext::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getContext(\Drupa...ext\MinkContext::class) of type object<Behat\Behat\Context\Context> or false is incompatible with the declared type object<Drupal\DrupalExte...on\Context\MinkContext> of property $minkContext.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
  }
28
29
  /**
30
   * Returns the active Panels IPE tab's contents.
31
   *
32
   * @return \Behat\Mink\Element\NodeElement
33
   *   The active tab's contents.
34
   */
35
  protected function getActiveTab() {
36
    return $this->assertSession()
37
      ->elementExists('css', '.ipe-tabs-content', $this->assertTray());
38
  }
39
40
  /**
41
   * Asserts the presence of the Panels IPE tray.
42
   *
43
   * @return \Behat\Mink\Element\NodeElement
44
   *   The Panels IPE tray element.
45
   *
46
   * @Then I should see the Panels IPE tray
47
   */
48
  public function assertTray() {
49
    return $this->assertSession()->elementExists('css', '#panels-ipe-tray');
50
  }
51
52
  /**
53
   * Opens a tab of the Panels IPE tray.
54
   *
55
   * @param string $tab
56
   *   The title of the tab to activate.
57
   *
58
   * @return \Behat\Mink\Element\NodeElement
59
   *   The tab contents.
60
   *
61
   * @When I open the :tab tab
62
   */
63
  public function openTab($tab) {
64
    $assert = $this->assertSession();
65
66
    // Assert that the tab exists...
67
    $link = $assert->elementExists(
68
      'named',
69
      ['link', $tab],
70
      $assert->elementExists('css', '.ipe-tabs', $this->assertTray())
71
    );
72
73
    // ...but only open it if not already active.
74
    if ($link->getParent()->hasClass('active') == FALSE) {
75
      $link->click();
76
      $this->minkContext->iWaitForAjaxToFinish();
77
    }
78
79
    return $this->getActiveTab();
80
  }
81
82
  /**
83
   * Close the Panels IPE tray.
84
   *
85
   * @When I close the editor
86
   */
87
  public function closeEditor() {
88
    $assert = $this->assertSession();
89
90
    // Assert that the tab exists...
91
    $link = $assert->elementExists(
92
      'named',
93
      ['link', 'Cancel'],
94
      $assert->elementExists('css', '.ipe-tabs', $this->assertTray())
95
    );
96
97
    $link->click();
98
    $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Mink\Driver\DriverInterface as the method getWebDriverSession() does only exist in the following implementations of said interface: Behat\Mink\Driver\Selenium2Driver.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
99
    $this->minkContext->iWaitForAjaxToFinish();
100
  }
101
102
  /**
103
   * Opens a particular block category of the "Manage Content" tab.
104
   *
105
   * @param string $category
106
   *   The category to open.
107
   *
108
   * @return \Behat\Mink\Element\NodeElement
109
   *   The tab contents.
110
   *
111
   * @When I open the :category category
112
   */
113
  public function openCategory($category) {
114
    $tab = $this->openTab('Manage Content');
115
116
    // Assert that the category exists...
117
    $link = $this->assertSession()
118
      ->elementExists('css',
119
        '.ipe-category[data-category="' . $category . '"]',
120
        $tab
121
      );
122
123
    // ...but only open it if not already active.
124
    if ($link->hasClass('active') == FALSE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
125
      $link->click();
126
      $this->minkContext->iWaitForAjaxToFinish();
127
    }
128
129
    return $tab;
130
  }
131
132
  /**
133
   * Asserts that a particular block plugin is available.
134
   *
135
   * @param string $plugin_id
136
   *   The block plugin ID.
137
   * @param string $category
138
   *   (optional) The category to open.
139
   *
140
   * @return \Behat\Mink\Element\NodeElement
141
   *   The link to instantiate the block plugin.
142
   *
143
   * @Then I should see the :plugin_id plugin
144
   * @Then I should see the :plugin_id plugin in the :category category
145
   */
146 View Code Duplication
  public function assertBlockPluginExists($plugin_id, $category = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    return $this->assertSession()
148
      ->elementExists(
149
        'css',
150
        '.ipe-block-plugin a[data-plugin-id="' . $plugin_id . '"]',
151
        $category ? $this->openCategory($category) : $this->getActiveTab()
152
      );
153
  }
154
155
  /**
156
   * Asserts that a particular block plugin is not available.
157
   *
158
   * @param string $plugin_id
159
   *   The block plugin ID.
160
   * @param string $category
161
   *   (optional) The category to open.
162
   *
163
   * @Then I should not see the :plugin_id plugin
164
   * @Then I should not see the :plugin_id plugin in the :category category
165
   */
166 View Code Duplication
  public function assertBlockPluginNotExists($plugin_id, $category = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    $this->assertSession()
168
      ->elementNotExists(
169
        'css',
170
        '.ipe-block-plugin a[data-plugin-id="' . $plugin_id . '"]',
171
        $category ? $this->openCategory($category) : $this->getActiveTab()
172
      );
173
  }
174
175
  /**
176
   * Instantiates a block plugin.
177
   *
178
   * @param string $plugin_id
179
   *   The block plugin ID.
180
   * @param string $category
181
   *   (optional) The category in which the block plugin resides.
182
   *
183
   * @return \Behat\Mink\Element\NodeElement
184
   *   The block plugin configuration form.
185
   *
186
   * @When I instantiate the :plugin_id block
187
   * @When I instantiate the :plugin_id block from the :category category
188
   */
189
  public function instantiateBlock($plugin_id, $category = NULL) {
190
    $this->assertBlockPluginExists($plugin_id, $category)
191
      ->click();
192
193
    $this->minkContext->iWaitForAjaxToFinish();
194
195
    return $this->assertSession()
196
      ->elementExists('css', '.panels-ipe-block-plugin-form', $this->getActiveTab());
197
  }
198
199
  /**
200
   * Saves the current IPE layout.
201
   *
202
   * @When I save the layout
203
   */
204
  public function saveLayout() {
205
    $this->openTab('Save');
206
207
    $this->minkContext->iWaitForAjaxToFinish();
208
  }
209
210
  /**
211
   * Saves the current IPE layout as a custom layout.
212
   *
213
   * @When I save the layout as custom
214
   */
215 View Code Duplication
  public function saveLayoutAsCustom() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
    $this->assertSession()
217
      ->elementExists('named', ['link', 'Save as custom'], $this->openTab('Save'))
218
      ->click();
219
220
    $this->minkContext->iWaitForAjaxToFinish();
221
  }
222
223
  /**
224
   * Saves the current IPE layout as a default layout.
225
   *
226
   * @When I save the layout as default
227
   */
228 View Code Duplication
  public function saveLayoutAsDefault() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
    $this->assertSession()
230
      ->elementExists('named', ['link', 'Save as default'], $this->openTab('Save'))
231
      ->click();
232
233
    $this->minkContext->iWaitForAjaxToFinish();
234
  }
235
236
  /**
237
   * Places a block into a Panels IPE layout.
238
   *
239
   * @param string $plugin_id
240
   *   The block plugin ID.
241
   * @param string $category
242
   *   (optional) The category in which the block plugin resides.
243
   *
244
   * @When I place the :plugin_id block from the :category category
245
   * @When I place the :plugin_id block
246
   */
247
  public function placeBlock($plugin_id, $category = NULL) {
248
    $this->instantiateBlock($plugin_id, $category)
249
      ->pressButton('Add');
250
251
    $this->minkContext->iWaitForAjaxToFinish();
252
  }
253
254
  /**
255
   * Reverts a panelized layout to its default state.
256
   *
257
   * @When I revert the layout
258
   */
259
  public function revertLayout() {
260
    $this->assertSession()
261
      ->elementExists('named', ['link', 'Revert to default'], $this->assertTray())
262
      ->click();
263
264
    $this->minkContext->iWaitForAjaxToFinish();
265
  }
266
267
}
268