Failed Conditions
Pull Request — master (#31)
by Chad
01:33
created

DeriveHeaderStrategyTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 44 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 10 1
A rowIsHeaderRow() 7 7 1
A rowNotIsHeaderRow() 8 8 1
A getFileObject() 7 7 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 SubjectivePHPTest\Csv;
4
5
use SplFileObject;
6
use SubjectivePHP\Csv\DeriveHeaderStrategy;
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * @coversDefaultClass \SubjectivePHP\Csv\DeriveHeaderStrategy
11
 */
12
final class DeriveHeaderStrategyTest extends TestCase
13
{
14
    /**
15
     * @test
16
     * @covers ::getHeaders
17
     */
18
    public function getHeaders()
19
    {
20
        $strategy = new DeriveHeaderStrategy();
21
        $fileObject = $this->getFileObject();
22
        $headers = $strategy->getHeaders($fileObject);
0 ignored issues
show
Unused Code introduced by
$headers 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...
23
        $this->assertSame(
24
            ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description'],
25
            $strategy->getHeaders($fileObject)
26
        );
27
    }
28
29
    /**
30
     * @test
31
     * @covers ::isHeaderRow
32
     */
33 View Code Duplication
    public function rowIsHeaderRow()
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...
34
    {
35
        $strategy = new DeriveHeaderStrategy();
36
        $fileObject = $this->getFileObject();
37
        $headers = $strategy->getHeaders($fileObject);
0 ignored issues
show
Unused Code introduced by
$headers 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
        $this->assertTrue($strategy->isHeaderRow($fileObject->fgetcsv()));
39
    }
40
41
    /**
42
     * @test
43
     * @covers ::isHeaderRow
44
     */
45 View Code Duplication
    public function rowNotIsHeaderRow()
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...
46
    {
47
        $strategy = new DeriveHeaderStrategy();
48
        $fileObject = $this->getFileObject();
49
        $headers = $strategy->getHeaders($fileObject);
0 ignored issues
show
Unused Code introduced by
$headers 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...
50
        $fileObject->fgetcsv();
51
        $this->assertFalse($strategy->isHeaderRow($fileObject->fgetcsv()));
52
    }
53
54 View Code Duplication
    private function getFileObject() : SplFileObject
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...
55
    {
56
        $fileObject = new SplFileObject(__DIR__ . '/_files/pipe_delimited.txt');
57
        $fileObject->setFlags(SplFileObject::READ_CSV);
58
        $fileObject->setCsvControl('|');
59
        return $fileObject;
60
    }
61
}
62