Completed
Pull Request — master (#31)
by Chad
01:21
created

HeaderStrategyTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 36 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

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