WriteFileTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 132
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 4
dl 36
loc 132
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 4 1
A writeFewLines() 19 19 1
A appendToFile() 0 15 1
A testWouldChange() 0 8 1
A insertFile() 17 17 1
A appendIfMatch() 0 24 1
A replaceInFile() 0 12 1
A replaceMultipleInFile() 0 11 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 Robo;
3
4
use PHPUnit\Framework\TestCase;
5
use Robo\Traits\TestTasksTrait;
6
7
class WriteFileTest extends TestCase
8
{
9
    use TestTasksTrait;
10
    use Task\File\loadTasks;
11
12
    protected $fixtures;
13
14
    public function setUp()
15
    {
16
        $this->fixtures = new Fixtures();
17
        $this->initTestTasksTrait();
18
        $this->fixtures->createAndCdToSandbox();
19
    }
20
21
    public function tearDown()
22
    {
23
        $this->fixtures->cleanup();
24
    }
25
26 View Code Duplication
    public function writeFewLines()
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...
27
    {
28
        // write lines with WriteToFile task
29
        $result = $this->taskWriteToFile('blogpost.md')
0 ignored issues
show
Bug introduced by
The method line does only exist in Robo\Task\File\Write, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
30
           ->line('****')
31
           ->line('hello world')
32
           ->line('****')
33
           ->run();
34
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
35
        $this->assertFileExists('blogpost.md');
36
        $contents = file_get_contents('blogpost.md');
37
        $expreded = <<<HERE
0 ignored issues
show
Unused Code introduced by
$expreded is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
****
39
hello world
40
****
41
42
HERE;
43
        $this->assertContains($expected, $contents);
0 ignored issues
show
Bug introduced by
The variable $expected does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
44
    }
45
46
    public function appendToFile()
47
    {
48
        $result = $this->taskWriteToFile('a.txt')
0 ignored issues
show
Bug introduced by
The method append does only exist in Robo\Task\File\Write, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49
           ->append()
50
           ->line('hello world')
51
           ->run();
52
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
53
        $this->assertFileExists('a.txt');
54
        $contents = file_get_contents('a.txt');
55
        $expected = <<<HERE
56
Ahello world
57
58
HERE;
59
        $this->assertContains($expected, $contents);
60
    }
61
62
    public function testWouldChange()
63
    {
64
        $writeTask = $this->taskWriteToFile('a.txt')
0 ignored issues
show
Bug introduced by
The method append does only exist in Robo\Task\File\Write, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65
           ->append();
66
        $this->assertEquals(false, $writeTask->wouldChange(), "No changes to test file.");
67
        $writeTask->line('hello world');
68
        $this->assertEquals(true, $writeTask->wouldChange(), "Test file would change.");
69
    }
70
71 View Code Duplication
    public function insertFile()
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...
72
    {
73
        $result = $this->taskWriteToFile('a.txt')
0 ignored issues
show
Bug introduced by
The method line does only exist in Robo\Task\File\Write, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
            ->line('****')
75
            ->textFromFile('b.txt')
76
            ->line("C")
77
            ->run();
78
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
79
        $this->assertFileExists('a.txt');
80
        $contents = file_get_contents('a.txt');
81
        $expected = <<<HERE
82
****
83
BC
84
85
HERE;
86
        $this->assertContains($expected, $contents);
87
    }
88
89
    public function appendIfMatch()
90
    {
91
        // append lines with WriteToFile task, but only if pattern does not match
92
        $result = $this->taskWriteToFile('blogpost.md')
0 ignored issues
show
Bug introduced by
The method line does only exist in Robo\Task\File\Write, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
93
           ->line('****')
94
           ->line('hello world')
95
           ->line('****')
96
           ->appendUnlessMatches('/hello/', 'Should not add this')
97
           ->appendUnlessMatches('/goodbye/', 'Should add this')
98
           ->appendIfMatches('/hello/', ' and should also add this')
99
           ->appendIfMatches('/goodbye/', ' but should not add this')
100
           ->appendIfMatches('/should/', '!')
101
           ->run();
102
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
103
        $this->assertFileExists('blogpost.md');
104
        $contents = file_get_contents('blogpost.md');
105
        $expected = <<<HERE
106
****
107
hello world
108
****
109
Should add this and should also add this!
110
HERE;
111
        $this->assertContains($expected, $contents);
112
    }
113
114
    public function replaceInFile()
115
    {
116
        $result = $this->taskReplaceInFile('a.txt')
0 ignored issues
show
Bug introduced by
The method from does only exist in Robo\Task\File\Replace, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
117
            ->from('A')
118
            ->to('B')
119
            ->run();
120
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
121
        $this->assertFileExists('a.txt');
122
        $contents = file_get_contents('a.txt');
123
        $this->assertContains('B', $contents);
124
125
    }
126
127
    public function replaceMultipleInFile()
128
    {
129
        $result = $this->taskReplaceInFile('box/robo.txt')
0 ignored issues
show
Bug introduced by
The method from does only exist in Robo\Task\File\Replace, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
130
            ->from(array('HELLO', 'ROBO'))
131
            ->to(array('Hello ', 'robo.li!'))
132
            ->run();
133
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
134
        $this->assertFileExists('box/robo.txt');
135
        $contents = file_get_contents('box/robo.txt');
136
        $this->assertContains('Hello robo.li!', $contents);
137
    }
138
}
139
140