Completed
Pull Request — 3.1 (#290)
by
unknown
09:17
created

DrushContext   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 1
dl 6
loc 84
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslationResources() 0 3 1
A readDrushOutput() 0 6 2
A assertDrushCommand() 0 5 2
A assertDrushCommandWithArgument() 0 6 2
A assertDrushOutput() 3 5 2
A drushOutputShouldNotContain() 3 5 2
A printLastDrushOutput() 0 3 1
A fixStepArgument() 0 3 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 Drupal\DrupalExtension\Context;
4
5
use Behat\Behat\Context\TranslatableContext;
6
7
/**
8
 * Provides step definitions for interacting directly with Drush commands.
9
 */
10
class DrushContext extends RawDrupalContext implements TranslatableContext {
11
12
  /**
13
   * Keep track of drush output.
14
   *
15
   * @var string|boolean
16
   */
17
  protected $drushOutput;
18
19
  /**
20
   * {@inheritDoc}
21
   */
22
  public static function getTranslationResources() {
23
    return glob(__DIR__ . '/../../../../i18n/*.xliff');
24
  }
25
26
  /**
27
   * Return the most recent drush command output.
28
   *
29
   * @return string
30
   */
31
  public function readDrushOutput() {
32
    if (!isset($this->drushOutput)) {
33
      throw new \RuntimeException('No drush output was found.');
34
    }
35
    return $this->drushOutput;
36
  }
37
38
  /**
39
   * @Given I run drush :command
40
   */
41
  public function assertDrushCommand($command) {
42
    if (!$this->drushOutput = $this->getDriver('drush')->$command()) {
43
       $this->drushOutput = TRUE;
44
    }
45
  }
46
47
  /**
48
   * @Given I run drush :command :arguments
49
   */
50
  public function assertDrushCommandWithArgument($command, $arguments) {
51
    $this->drushOutput = $this->getDriver('drush')->$command($this->fixStepArgument($arguments));
52
    if (!isset($this->drushOutput)) {
53
      $this->drushOutput = TRUE;
54
    }
55
  }
56
57
  /**
58
   * @Then drush output should contain :output
59
   */
60
  public function assertDrushOutput($output) {
61 View Code Duplication
    if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) === FALSE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
      throw new \Exception(sprintf("The last drush command output did not contain '%s'.\nInstead, it was:\n\n%s'", $output, $this->drushOutput));
63
    }
64
  }
65
66
  /**
67
   * @Then drush output should not contain :output
68
   */
69
  public function drushOutputShouldNotContain($output) {
70 View Code Duplication
    if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) !== FALSE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71
        throw new \Exception(sprintf("The last drush command output did contain '%s' although it should not.\nOutput:\n\n%s'", $output, $this->drushOutput));
72
    }
73
  }
74
75
  /**
76
   * @Then print last drush output
77
   */
78
  public function printLastDrushOutput() {
79
    echo $this->readDrushOutput();
80
  }
81
82
  /**
83
   * Returns fixed step argument (with \\" replaced back to ").
84
   *
85
   * @param string $argument
86
   *
87
   * @return string
88
   */
89
  protected function fixStepArgument($argument) {
90
    return str_replace('\\"', '"', $argument);
91
  }
92
93
}
94