Completed
Push — 8.1.x ( fb062c...e20c53 )
by Antonio
06:58
created

ContentContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 68
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A iAmViewingTheContent() 0 3 1
A iAmEditingTheContent() 0 3 1
A iAmDeletingTheContent() 0 3 1
A visitContentPage() 0 9 1
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\Behat\Context\SnippetAcceptingContext;
6
7
/**
8
 * Class ContentContext.
9
 *
10
 * @package NuvoleWeb\Drupal\DrupalExtension\Context
11
 */
12
class ContentContext extends RawDrupalContext implements SnippetAcceptingContext {
13
14
  /**
15
   * Assert viewing content given its type and title.
16
   *
17
   * @param string $type
18
   *    Content type machine name.
19
   * @param string $title
20
   *    Content title.
21
   *
22
   * @Given I am visiting the :type content :title
23
   * @Given I visit the :type content :title
24
   */
25
  public function iAmViewingTheContent($type, $title) {
26
    $this->visitContentPage('view', $type, $title);
27
  }
28
29
  /**
30
   * Assert editing content given its type and title.
31
   *
32
   * @param string $type
33
   *    Content type machine name.
34
   * @param string $title
35
   *    Content title.
36
   *
37
   * @Given I am editing the :type content :title
38
   * @Given I edit the :type content :title
39
   */
40
  public function iAmEditingTheContent($type, $title) {
41
    $this->visitContentPage('edit', $type, $title);
42
  }
43
44
  /**
45
   * Assert deleting content given its type and title.
46
   *
47
   * @param string $type
48
   *    Content type machine name.
49
   * @param string $title
50
   *    Content title.
51
   *
52
   * @Given I am deleting the :type content :title
53
   * @Given I delete the :type content :title
54
   */
55
  public function iAmDeletingTheContent($type, $title) {
56
    $this->visitContentPage('delete', $type, $title);
57
  }
58
59
  /**
60
   * Provides a common step definition callback for node pages.
61
   *
62
   * @param string $op
63
   *   The operation being performed: 'view', 'edit', 'delete'.
64
   * @param string $type
65
   *   The node type either as id or as label.
66
   * @param string $title
67
   *   The node title.
68
   */
69
  protected function visitContentPage($op, $type, $title) {
70
    $nid = $this->getCore()->getEntityIdByLabel('node', $type, $title);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Drupal\Driver\Cores\CoreInterface as the method getEntityIdByLabel() does only exist in the following implementations of said interface: NuvoleWeb\Drupal\Driver\Cores\Drupal8.

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...
71
    $path = [
72
      'view' => "node/$nid",
73
      'edit' => "node/$nid/edit",
74
      'delete' => "node/$nid/delete",
75
    ];
76
    $this->visitPath($path[$op]);
77
  }
78
79
}
80