Completed
Push — 1.0.x ( f1688f...f8d28b )
by Antonio
06:16
created

TaxonomyTermContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 63
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A iAmViewingTheTerm() 0 3 1
A iAmEditingTheTerm() 0 3 1
A iAmDeletingTheTerm() 0 3 1
A visitTermPage() 0 16 2
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\Mink\Exception\ExpectationException;
6
7
/**
8
 * Class TaxonomyTermContext.
9
 *
10
 * @package NuvoleWeb\Drupal\DrupalExtension\Context
11
 */
12
class TaxonomyTermContext extends RawDrupalContext {
13
14
  /**
15
   * Visit taxonomy term page given its type and name.
16
   *
17
   * @Given I visit the :type term :title
18
   * @Given I am visiting the :type term :title
19
   */
20
  public function iAmViewingTheTerm($type, $title) {
21
    $this->visitTermPage('view', $type, $title);
22
  }
23
24
  /**
25
   * Visit taxonomy term edit page given its type and name.
26
   *
27
   * @Given I am editing the :type term :title
28
   * @Given I edit the :type term :title
29
   */
30
  public function iAmEditingTheTerm($type, $title) {
31
    $this->visitTermPage('edit', $type, $title);
32
  }
33
34
  /**
35
   * Visit taxonomy term delete page given its type and name.
36
   *
37
   * @Given I am deleting the :type term :title
38
   * @Given I delete the :type term :title
39
   */
40
  public function iAmDeletingTheTerm($type, $title) {
41
    $this->visitTermPage('delete', $type, $title);
42
  }
43
44
  /**
45
   * Provides a common step definition callback for taxonomy term pages.
46
   *
47
   * @param string $op
48
   *   The operation being performed: 'view', 'edit', 'delete'.
49
   * @param string $type
50
   *   The term's vocabulary, either as machine name or label.
51
   * @param string $name
52
   *   The term name.
53
   *
54
   * @throws ExpectationException
55
   *   When the term does not exist.
56
   */
57
  protected function visitTermPage($op, $type, $name) {
58
    $type = $this->getCore()->convertLabelToTermTypeId($type);
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 convertLabelToTermTypeId() 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...
59
    $term = $this->getCore()->loadTaxonomyTermByName($type, $name);
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 loadTaxonomyTermByName() 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...
60
61
    if (!empty($term)) {
62
      $path = [
63
        'view' => "taxonomy/term/{$this->getCore()->getTaxonomyTermId($term)}",
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 getTaxonomyTermId() 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...
64
        'edit' => "taxonomy/term/{$this->getCore()->getTaxonomyTermId($term)}/edit",
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 getTaxonomyTermId() 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...
65
        'delete' => "taxonomy/term/{$this->getCore()->getTaxonomyTermId($term)}/delete",
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 getTaxonomyTermId() 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...
66
      ];
67
      $this->visitPath($path[$op]);
68
    }
69
    else {
70
      throw new ExpectationException("No taxonomy term with vocabulary '$type' and name '$name' has been found.", $this->getSession());
71
    }
72
  }
73
74
}
75