FindEncodingTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 47.15 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testGetFileEncodingForASCIIFile() 11 11 1
A testGetFileEncodingForUtf8File() 11 11 1
A testGetFileEncodingForCompressedFile() 0 16 1
A testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() 21 21 1
A testWhenTheProcessReturnsAnUnknownFileNullIsReturned() 0 14 1
A testCanModifyCanModifyLocalFiles() 7 7 1
A testUnableToModifyNonLocalFiles() 8 8 1
A testModifyWillSetTheEncoding() 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
 * 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\Encoding;
15
16
use Graze\DataFile\Helper\Builder\Builder;
17
use Graze\DataFile\Helper\Builder\BuilderInterface;
18
use Graze\DataFile\Modify\Compress\Gzip;
19
use Graze\DataFile\Modify\Encoding\FindEncoding;
20
use Graze\DataFile\Node\FileNodeInterface;
21
use Graze\DataFile\Node\LocalFile;
22
use Graze\DataFile\Test\AbstractFileTestCase;
23
use InvalidArgumentException;
24
use Mockery as m;
25
use Mockery\MockInterface;
26
use Symfony\Component\Process\Exception\ProcessFailedException;
27
use Symfony\Component\Process\Process;
28
29
class FindEncodingTest extends AbstractFileTestCase
30
{
31
    /**
32
     * @var FindEncoding
33
     */
34
    protected $findEncoding;
35
36
    /**
37
     * @var BuilderInterface|MockInterface
38
     */
39
    protected $builder;
40
41
    public function setUp()
42
    {
43
        $this->builder = m::mock(Builder::class)->makePartial();
44
        $this->findEncoding = new FindEncoding();
45
        $this->findEncoding->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...
46
    }
47
48 View Code Duplication
    public function testGetFileEncodingForASCIIFile()
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...
49
    {
50
        $asciiFile = (new LocalFile(static::$dir . 'ascii_file.test'))
51
            ->setEncoding('us-ascii');
52
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
53
54
        static::assertEquals(
55
            strtolower($asciiFile->getEncoding()),
56
            strtolower($this->findEncoding->getEncoding($asciiFile))
57
        );
58
    }
59
60 View Code Duplication
    public function testGetFileEncodingForUtf8File()
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...
61
    {
62
        $utf8file = (new LocalFile(static::$dir . 'utf8_file.test'))
63
            ->setEncoding('UTF-8');
64
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
65
66
        static::assertEquals(
67
            strtolower($utf8file->getEncoding()),
68
            strtolower($this->findEncoding->getEncoding($utf8file))
69
        );
70
    }
71
72
    public function testGetFileEncodingForCompressedFile()
73
    {
74
        $utf8file = (new LocalFile(static::$dir . 'utf8_tobegzipped_file.test'))
75
            ->setEncoding('UTF-8');
76
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
77
        $gzip = new Gzip();
78
        $gzipFile = $gzip->compress($utf8file);
79
80
        static::assertEquals(
81
            strtolower($gzipFile->getEncoding()),
82
            strtolower($this->findEncoding->getEncoding($gzipFile))
83
        );
84
85
        static::assertEquals('utf-8', $this->findEncoding->getEncoding($gzipFile));
86
        static::assertEquals($utf8file->getEncoding(), $gzipFile->getEncoding());
87
    }
88
89 View Code Duplication
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
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...
90
    {
91
        $process = m::mock(Process::class)->makePartial();
92
        $process->shouldReceive('isSuccessful')->andReturn(false);
93
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
94
        $process->shouldReceive('getExitCode')->andReturn(1);
95
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
96
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
97
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
98
        $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process));
99
100
        $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...
101
                      ->andReturn($process);
102
103
        $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test');
104
        $file->put('random stuff and things 2!');
105
106
        $this->expectException(ProcessFailedException::class);
107
108
        $this->findEncoding->getEncoding($file);
109
    }
110
111
    public function testWhenTheProcessReturnsAnUnknownFileNullIsReturned()
112
    {
113
        $process = m::mock(Process::class)->makePartial();
114
        $process->shouldReceive('mustRun');
115
        $process->shouldReceive('getOutput')->andReturn('some random stuff with no charset');
116
117
        $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...
118
                      ->andReturn($process);
119
120
        $file = new LocalFile(static::$dir . 'unknown_compression.test');
121
        $file->put('random stuff and things 2!');
122
123
        static::assertNull($this->findEncoding->getEncoding($file));
124
    }
125
126 View Code Duplication
    public function testCanModifyCanModifyLocalFiles()
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...
127
    {
128
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
129
        $file->put('some random text');
130
131
        static::assertTrue($this->findEncoding->canModify($file));
132
    }
133
134 View Code Duplication
    public function testUnableToModifyNonLocalFiles()
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...
135
    {
136
        $file = m::mock(FileNodeInterface::class);
137
        static::assertFalse($this->findEncoding->canModify($file));
138
139
        $this->expectException(InvalidArgumentException::class);
140
        $this->findEncoding->modify($file);
141
    }
142
143
    public function testModifyWillSetTheEncoding()
144
    {
145
        $utf8file = (new LocalFile(static::$dir . 'utf8_file.test'));
146
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
147
148
        $utf8file = $this->findEncoding->modify($utf8file);
149
        static::assertEquals('utf-8', $utf8file->getEncoding());
150
    }
151
}
152