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 ( 0cf1d5...4fe320 )
by Devin
03:59
created

PanelsInPlaceContext::closeEditor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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
   * Opens a particular block category of the "Manage Content" tab.
84
   *
85
   * @param string $category
86
   *   The category to open.
87
   *
88
   * @return \Behat\Mink\Element\NodeElement
89
   *   The tab contents.
90
   *
91
   * @When I open the :category category
92
   */
93
  public function openCategory($category) {
94
    $tab = $this->openTab('Manage Content');
95
96
    // Assert that the category exists...
97
    $link = $this->assertSession()
98
      ->elementExists('css',
99
        '.ipe-category[data-category="' . $category . '"]',
100
        $tab
101
      );
102
103
    // ...but only open it if not already active.
104
    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...
105
      $link->click();
106
      $this->minkContext->iWaitForAjaxToFinish();
107
    }
108
109
    return $tab;
110
  }
111
112
  /**
113
   * Asserts that a particular block plugin is available.
114
   *
115
   * @param string $plugin_id
116
   *   The block plugin ID.
117
   * @param string $category
118
   *   (optional) The category to open.
119
   *
120
   * @return \Behat\Mink\Element\NodeElement
121
   *   The link to instantiate the block plugin.
122
   *
123
   * @Then I should see the :plugin_id plugin
124
   * @Then I should see the :plugin_id plugin in the :category category
125
   */
126 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...
127
    return $this->assertSession()
128
      ->elementExists(
129
        'css',
130
        '.ipe-block-plugin a[data-plugin-id="' . $plugin_id . '"]',
131
        $category ? $this->openCategory($category) : $this->getActiveTab()
132
      );
133
  }
134
135
  /**
136
   * Asserts that a particular block plugin is not available.
137
   *
138
   * @param string $plugin_id
139
   *   The block plugin ID.
140
   * @param string $category
141
   *   (optional) The category to open.
142
   *
143
   * @Then I should not see the :plugin_id plugin
144
   * @Then I should not see the :plugin_id plugin in the :category category
145
   */
146 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...
147
    $this->assertSession()
148
      ->elementNotExists(
149
        'css',
150
        '.ipe-block-plugin a[data-plugin-id="' . $plugin_id . '"]',
151
        $category ? $this->openCategory($category) : $this->getActiveTab()
152
      );
153
  }
154
155
  /**
156
   * Instantiates a block plugin.
157
   *
158
   * @param string $plugin_id
159
   *   The block plugin ID.
160
   * @param string $category
161
   *   (optional) The category in which the block plugin resides.
162
   *
163
   * @return \Behat\Mink\Element\NodeElement
164
   *   The block plugin configuration form.
165
   *
166
   * @When I instantiate the :plugin_id block
167
   * @When I instantiate the :plugin_id block from the :category category
168
   */
169
  public function instantiateBlock($plugin_id, $category = NULL) {
170
    $this->assertBlockPluginExists($plugin_id, $category)
171
      ->click();
172
173
    $this->minkContext->iWaitForAjaxToFinish();
174
175
    return $this->assertSession()
176
      ->elementExists('css', '.panels-ipe-block-plugin-form', $this->getActiveTab());
177
  }
178
179
  /**
180
   * Saves the current IPE layout as a custom layout.
181
   *
182
   * @When I save the layout
183
   */
184
  public function saveLayout() {
185
    $this->assertSession()
186
      ->elementExists(
187
        'named',
188
        ['link', 'Save'],
189
        $this->assertTray()
190
      )
191
      ->click();
192
193
    $this->minkContext->iWaitForAjaxToFinish();
194
  }
195
196
  /**
197
   * Places a block into a Panels IPE layout.
198
   *
199
   * @param string $plugin_id
200
   *   The block plugin ID.
201
   * @param string $category
202
   *   (optional) The category in which the block plugin resides.
203
   *
204
   * @When I place the :plugin_id block from the :category category
205
   * @When I place the :plugin_id block
206
   */
207
  public function placeBlock($plugin_id, $category = NULL) {
208
    $this->instantiateBlock($plugin_id, $category)
209
      ->pressButton('Add');
210
211
    $this->minkContext->iWaitForAjaxToFinish();
212
  }
213
214
  /**
215
   * Reverts a panelized layout to its default state.
216
   *
217
   * @When I revert the layout
218
   */
219
  public function revertLayout() {
220
    $this->assertSession()
221
      ->elementExists('named', ['link', 'Revert to default'], $this->assertTray())
222
      ->click();
223
224
    $this->minkContext->iWaitForAjaxToFinish();
225
  }
226
227
  /**
228
   * Opens the moderation sidebar.
229
   *
230
   * @When I open the moderation sidebar
231
   */
232
  public function openModerationSidebar() {
233
    $assert = $this->assertSession();
234
235
    // Assert that the tab exists...
236
    $link = $assert->elementExists(
237
      'named',
238
      ['link', 'Tasks'],
239
      $assert->elementExists('css', '.moderation-sidebar-toolbar-tab')
240
    );
241
242
    // ...but only open it if not already active.
243
    if ($link->hasClass('sidebar-open') == 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...
244
      $link->click();
245
      $this->minkContext->iWaitForAjaxToFinish();
246
    }
247
  }
248
249
  /**
250
   * Close the Panels IPE tray.
251
   *
252
   * @When I close the editor
253
   */
254
  public function closeEditor() {
255
    $assert = $this->assertSession();
256
257
    // Assert that the tab exists...
258
    $link = $assert->elementExists(
259
      'named',
260
      ['link', 'Cancel'],
261
      $assert->elementExists('css', '.ipe-tabs', $this->assertTray())
262
    );
263
264
    $link->click();
265
    $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...
266
    $this->minkContext->iWaitForAjaxToFinish();
267
  }
268
269
}
270