FilesystemStackTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 20 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 7
dl 17
loc 85
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 4 1
A testDirAndFileCreation() 17 17 1
A testCreateDir() 0 10 1
A testDeleteFile() 0 10 1
A testCrossVolumeRename() 0 23 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 FilesystemStackTest extends TestCase
8
{
9
    use TestTasksTrait;
10
    use Collection\loadTasks;
11
    use Task\Filesystem\loadTasks;
12
13
    protected $fixtures;
14
15
    public function setUp()
16
    {
17
        $this->fixtures = new Fixtures();
18
        $this->initTestTasksTrait();
19
        $this->fixtures->createAndCdToSandbox();
20
    }
21
22
    public function tearDown()
23
    {
24
        $this->fixtures->cleanup();
25
    }
26
27 View Code Duplication
    public function testDirAndFileCreation()
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...
28
    {
29
        // Set up a collection to add tasks to
30
        $collection = $this->collectionBuilder();
31
32
        // Set up a filesystem stack
33
        $collection->taskFilesystemStack()
0 ignored issues
show
Documentation Bug introduced by
The method taskFilesystemStack does not exist on object<Robo\Collection\CollectionBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
34
            ->mkdir('simulatedir')
35
            ->touch('simulatedir/error.txt');
36
37
        $this->assertFileNotExists('simulatedir/error.txt');
38
39
        // Run the task collection; the files should be present afterwards
40
        $result = $collection->run();
41
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
42
        $this->assertFileExists('simulatedir/error.txt');
43
    }
44
45
    public function testCreateDir()
46
    {
47
        $this->assertFileNotExists('log/error.txt');
48
        $result = $this->taskFilesystemStack()
0 ignored issues
show
Bug introduced by
The method mkdir does only exist in Robo\Task\Filesystem\FilesystemStack, 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
            ->mkdir('log')
50
            ->touch('log/error.txt')
51
            ->run();
52
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
53
        $this->assertFileExists('log/error.txt');
54
    }
55
56
    public function testDeleteFile()
57
    {
58
        $this->assertFileExists('a.txt');
59
        $result = $this->taskFilesystemStack()
0 ignored issues
show
Bug introduced by
The method stopOnFail does only exist in Robo\Task\Filesystem\FilesystemStack, 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...
60
            ->stopOnFail()
61
            ->remove('a.txt')
62
            ->run();
63
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
64
        $this->assertFileNotExists('a.txt');
65
    }
66
67
    public function testCrossVolumeRename()
68
    {
69
        $fsStack = $this->taskFilesystemStack()
0 ignored issues
show
Bug introduced by
The method mkdir does only exist in Robo\Task\Filesystem\FilesystemStack, 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...
70
            ->mkdir('log')
71
            ->touch('log/error.txt');
72
        $result = $fsStack->run();
73
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
74
75
        // We can't force _rename to run the cross-volume
76
        // code path, so we will directly call the protected
77
        // method crossVolumeRename to test to ensure it works.
78
        // We will get a reference to it via reflection, set
79
        // the reflected method object to public, and then
80
        // call it via reflection.
81
        $class = new \ReflectionClass('\Robo\Task\Filesystem\FilesystemStack');
82
        $method = $class->getMethod('crossVolumeRename');
83
        $method->setAccessible(true);
84
        $actualFsStackTask = $fsStack->getCollectionBuilderCurrentTask();
85
        $method->invokeArgs($actualFsStackTask, ['log', 'logfiles']);
86
87
        $this->assertFileNotExists('log/error.txt');
88
        $this->assertFileExists('logfiles/error.txt');
89
    }
90
91
}
92