FileSystemAdapterTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 16.87 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 14
loc 83
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpAdapter() 0 5 1
A readFile() 0 7 1
A readFile_ThrowsExceptionIfInvalidPath() 7 7 1
A readFile_ThrowsExceptionIfPathNotFound() 7 7 1
A test_getExtension() 0 6 1
A dataProvider_test_getExtension() 0 8 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 Hgraca\Phorensic\Test\SharedKernel\Port\FileSystem\Adapter\FileSystem;
4
5
use Hgraca\FileSystem\Exception\FileNotFoundException;
6
use Hgraca\FileSystem\Exception\InvalidPathException;
7
use Hgraca\FileSystem\FileSystemInterface;
8
use Hgraca\Phorensic\SharedKernel\Port\FileSystem\Adapter\FileSystem\FileSystemAdapter;
9
use Mockery;
10
use Mockery\MockInterface;
11
use PHPUnit_Framework_TestCase;
12
13
final class FileSystemAdapterTest extends PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var MockInterface|FileSystemInterface
17
     */
18
    private $fileSystemMock;
19
20
    /**
21
     * @var FileSystemAdapter
22
     */
23
    private $adapter;
24
25
    /**
26
     * @before
27
     */
28
    public function setUpAdapter()
29
    {
30
        $this->fileSystemMock = Mockery::mock(FileSystemInterface::class);
31
        $this->adapter = new FileSystemAdapter($this->fileSystemMock);
32
    }
33
34
    /**
35
     * @test
36
     *
37
     * @small
38
     */
39
    public function readFile()
40
    {
41
        $path = 'some/path/to/file';
42
        $this->fileSystemMock->shouldReceive('readFile')->once()->with($path)->andReturn('some content');
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Hgraca\FileSystem\FileSystemInterface.

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...
43
44
        $this->adapter->readFile($path);
45
    }
46
47
    /**
48
     * @test
49
     *
50
     * @small
51
     *
52
     * @expectedException \Hgraca\Phorensic\SharedKernel\Port\FileSystem\Exception\InvalidPathException
53
     */
54 View Code Duplication
    public function readFile_ThrowsExceptionIfInvalidPath()
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
        $path = 'some/path/to/file';
57
        $this->fileSystemMock->shouldReceive('readFile')->once()->with($path)->andThrow(InvalidPathException::class);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Hgraca\FileSystem\FileSystemInterface.

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...
58
59
        $this->adapter->readFile($path);
60
    }
61
62
    /**
63
     * @test
64
     *
65
     * @small
66
     *
67
     * @expectedException \Hgraca\Phorensic\SharedKernel\Port\FileSystem\Exception\FileNotFoundException
68
     */
69 View Code Duplication
    public function readFile_ThrowsExceptionIfPathNotFound()
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
        $path = 'some/path/to/file';
72
        $this->fileSystemMock->shouldReceive('readFile')->once()->with($path)->andThrow(FileNotFoundException::class);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Hgraca\FileSystem\FileSystemInterface.

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...
73
74
        $this->adapter->readFile($path);
75
    }
76
77
    /**
78
     * @dataProvider dataProvider_test_getExtension
79
     */
80
    public function test_getExtension(string $path, string $expectedExtension)
81
    {
82
        $adapter = new FileSystemAdapter();
83
84
        self::assertEquals($expectedExtension, $adapter->getExtension($path));
85
    }
86
87
    public function dataProvider_test_getExtension()
88
    {
89
        return [
90
            ['/a/dir/fileA', ''],
91
            ['/a/dir/fileA.php', 'php'],
92
            ['/a/dir/fileA.bladibla', 'bladibla'],
93
        ];
94
    }
95
}
96