TailTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 216
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 216
loc 216
c 0
b 0
f 0
wmc 14
lcom 1
cbo 9
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testInstanceOf() 4 4 1
A testCanModifyAcceptsLocalFile() 15 15 1
B testBasicReadingTheLastNLines() 27 27 1
A createFile() 19 19 1
A testOutputLinesStartingFromN() 21 21 1
A testAddingAPostfixToTheEndOfTheFile() 9 9 1
A testCallingWithBlankPostfixWillReplaceInLine() 9 9 1
A testSettingKeepOldFileToFalseWillDeleteTheOldFile() 9 9 1
A testCallingModifyDoesTail() 16 16 1
A testCallingModifyWillPassThroughOptions() 18 18 1
A testCallingModifyWithoutLinesWillThrowAnException() 8 8 1
A setUp() 6 6 1
A testCallingModifyWithANonLocalFileWillThrowAnException() 10 10 1
A testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() 14 14 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
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Integration\Modify;
15
16
use Graze\DataFile\Helper\Builder\Builder;
17
use Graze\DataFile\Helper\Builder\BuilderInterface;
18
use Graze\DataFile\Helper\Process\ProcessFactory;
19
use Graze\DataFile\Modify\FileModifierInterface;
20
use Graze\DataFile\Modify\Tail;
21
use Graze\DataFile\Node\FileNodeInterface;
22
use Graze\DataFile\Node\LocalFile;
23
use Graze\DataFile\Test\AbstractFileTestCase;
24
use InvalidArgumentException;
25
use Mockery as m;
26
use Mockery\MockInterface;
27
use Symfony\Component\Process\Exception\ProcessFailedException;
28
use Symfony\Component\Process\Process;
29
30 View Code Duplication
class TailTest extends AbstractFileTestCase
0 ignored issues
show
Duplication introduced by
This class 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...
31
{
32
    /**
33
     * @var Tail
34
     */
35
    protected $tail;
36
37
    /**
38
     * @var BuilderInterface|MockInterface
39
     */
40
    protected $builder;
41
42
    public function setUp()
43
    {
44
        $this->builder = m::mock(Builder::class)->makePartial();
45
        $this->tail = new Tail();
46
        $this->tail->setBuilder($this->builder);
0 ignored issues
show
Documentation introduced by
$this->builder is of type object<Mockery\Mock>, but the function expects a object<Graze\DataFile\He...ilder\BuilderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
    }
48
49
    public function testInstanceOf()
50
    {
51
        static::assertInstanceOf(FileModifierInterface::class, $this->tail);
52
    }
53
54
    public function testCanModifyAcceptsLocalFile()
55
    {
56
        $localFile = m::mock(LocalFile::class);
57
        $localFile->shouldReceive('exists')->andReturn(true, false);
58
59
        static::assertTrue($this->tail->canModify($localFile));
60
        static::assertFalse(
61
            $this->tail->canModify($localFile),
62
            "CanExtend should return false if the file does not exist"
63
        );
64
65
        $randomThing = m::mock(FileNodeInterface::class);
66
67
        static::assertFalse($this->tail->canModify($randomThing));
68
    }
69
70
    public function testBasicReadingTheLastNLines()
71
    {
72
        $file = $this->createFile('last_five_lines');
73
74
        $newFile = $this->tail->tail($file, 5);
75
76
        static::assertEquals(
77
            [
78
                "Line 6",
79
                "Line 7",
80
                "Line 8",
81
                "Line 9",
82
                "Line 10",
83
            ],
84
            $newFile->getContents()
85
        );
86
87
        $newFile = $this->tail->tail($file, 2);
88
89
        static::assertEquals(
90
            [
91
                "Line 9",
92
                "Line 10",
93
            ],
94
            $newFile->getContents()
95
        );
96
    }
97
98
    /**
99
     * @param string $path
100
     *
101
     * @return LocalFile
102
     */
103
    private function createFile($path)
104
    {
105
        $file = new LocalFile(static::$dir . $path);
106
        $file->put(
107
            "Line 1
108
Line 2
109
Line 3
110
Line 4
111
Line 5
112
Line 6
113
Line 7
114
Line 8
115
Line 9
116
Line 10
117
"
118
        );
119
120
        return $file;
121
    }
122
123
    public function testOutputLinesStartingFromN()
124
    {
125
        $file = $this->createFile('from.second.line.onwards');
126
127
        $newFile = $this->tail->tail($file, '+2');
128
129
        static::assertEquals(
130
            [
131
                "Line 2",
132
                "Line 3",
133
                "Line 4",
134
                "Line 5",
135
                "Line 6",
136
                "Line 7",
137
                "Line 8",
138
                "Line 9",
139
                "Line 10",
140
            ],
141
            $newFile->getContents()
142
        );
143
    }
144
145
    public function testAddingAPostfixToTheEndOfTheFile()
146
    {
147
        $file = $this->createFile('postfix_test.test');
148
149
        $newFile = $this->tail->tail($file, 4, ['postfix' => 'pfixtest']);
150
151
        static::assertNotNull($newFile);
152
        static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename());
153
    }
154
155
    public function testCallingWithBlankPostfixWillReplaceInLine()
156
    {
157
        $file = $this->createFile('inline_tail.test');
158
159
        $newFile = $this->tail->tail($file, 2, ['postfix' => '']);
160
161
        static::assertNotNull($newFile);
162
        static::assertEquals($file->getFilename(), $newFile->getFilename());
163
    }
164
165
    public function testSettingKeepOldFileToFalseWillDeleteTheOldFile()
166
    {
167
        $file = $this->createFile('inline_replace.test');
168
169
        $newFile = $this->tail->tail($file, 5, ['keepOldFile' => false]);
170
171
        static::assertTrue($newFile->exists());
172
        static::assertFalse($file->exists());
173
    }
174
175
    public function testCallingModifyDoesTail()
176
    {
177
        $file = $this->createFile('simple_tail.test');
178
179
        $newFile = $this->tail->modify($file, ['lines' => 4]);
180
181
        static::assertEquals(
182
            [
183
                "Line 7",
184
                "Line 8",
185
                "Line 9",
186
                "Line 10",
187
            ],
188
            $newFile->getContents()
189
        );
190
    }
191
192
    public function testCallingModifyWillPassThroughOptions()
193
    {
194
        $file = $this->createFile('option_pass_through.test');
195
196
        $newFile = $this->tail->modify(
197
            $file,
198
            [
199
                'lines'       => 2,
200
                'postfix'     => 'pass',
201
                'keepOldFile' => false,
202
            ]
203
        );
204
205
        static::assertTrue($newFile->exists());
206
        static::assertFalse($file->exists());
207
        static::assertNotNull($newFile);
208
        static::assertEquals('option_pass_through-pass.test', $newFile->getFilename());
209
    }
210
211
    public function testCallingModifyWithoutLinesWillThrowAnException()
212
    {
213
        $file = $this->createFile('option_pass_through.test');
214
215
        $this->expectException(InvalidArgumentException::class);
216
217
        $this->tail->modify($file);
218
    }
219
220
    public function testCallingModifyWithANonLocalFileWillThrowAnException()
221
    {
222
        $file = m::mock(FileNodeInterface::class);
223
        $file->shouldReceive('__toString')
224
             ->andReturn('some/file/here');
225
226
        $this->expectException(InvalidArgumentException::class);
227
228
        $this->tail->modify($file, ['lines' => 1]);
229
    }
230
231
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
232
    {
233
        $process = m::mock(Process::class)->makePartial();
234
        $process->shouldReceive('isSuccessful')->andReturn(false);
235
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

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...
236
                      ->andReturn($process);
237
238
        $file = new LocalFile(static::$dir . 'failed_tail.test');
239
        $file->put('nothing interesting here');
240
241
        $this->expectException(ProcessFailedException::class);
242
243
        $this->tail->tail($file, 3);
244
    }
245
}
246