SeeInOutputTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 10.14 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A simplify() 7 7 1
A initSeeInOutputTrait() 0 13 1
A capturedOutputStream() 0 4 1
A logger() 0 4 1
A accumulate() 0 5 1
A seeInOutput() 0 6 1
A doNotSeeInOutput() 0 6 1
A seeOutputEquals() 0 6 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
namespace Codeception\Module;
3
4
use Robo\Robo;
5
use Symfony\Component\Console\Output\BufferedOutput;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
trait SeeInOutputTrait
9
{
10
    protected $testPrinter;
11
    protected $capturedOutput;
12
    protected $logger;
13
14
    public function initSeeInOutputTrait($container, $input = null)
15
    {
16
        $this->capturedOutput = '';
17
        $this->testPrinter = new BufferedOutput(OutputInterface::VERBOSITY_DEBUG);
18
19
        $app = Robo::createDefaultApplication();
20
        $config = new \Robo\Config();
0 ignored issues
show
Deprecated Code introduced by
The class Robo\Config has been deprecated with message: Use \Robo\Config\Config

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
        \Robo\Robo::configureContainer($container, $app, $config, $input, $this->testPrinter);
22
23
        // Set the application dispatcher
24
        $app->setDispatcher($container->get('eventDispatcher'));
25
        $this->logger = $container->get('logger');
26
    }
27
28
    public function capturedOutputStream()
29
    {
30
        return $this->testPrinter;
31
    }
32
33
    public function logger()
34
    {
35
        return $this->logger;
36
    }
37
38
    protected function accumulate()
39
    {
40
        $this->capturedOutput .= $this->testPrinter->fetch();
41
        return $this->capturedOutput;
42
    }
43
44
    public function seeInOutput($value)
45
    {
46
        $output = $this->accumulate();
47
        $output = $this->simplify($output);
48
        $this->assertContains($value, $output);
0 ignored issues
show
Bug introduced by
It seems like assertContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
    }
50
51
    public function doNotSeeInOutput($value)
52
    {
53
        $output = $this->accumulate();
54
        $output = $this->simplify($output);
55
        $this->assertNotContains($value, $output);
0 ignored issues
show
Bug introduced by
It seems like assertNotContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
    }
57
58
    public function seeOutputEquals($value)
59
    {
60
        $output = $this->accumulate();
61
        $output = $this->simplify($output);
62
        $this->assertEquals($value, $output);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
    }
64
65
    /**
66
     * Make our output comparisons more platform-agnostic by converting
67
     * CRLF (Windows) or raw CR (confused output) to a LF (unix/Mac).
68
     */
69 View Code Duplication
    protected function simplify($output)
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...
70
    {
71
        $output = str_replace("\r\n", "\n", $output);
72
        $output = str_replace("\r", "\n", $output);
73
74
        return $output;
75
    }
76
}
77