Completed
Pull Request — master (#1)
by Harry
05:13 queued 02:07
created

FindEncoding   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 66
wmc 7
lcom 1
cbo 4
ccs 22
cts 22
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncoding() 0 17 2
A canModify() 0 5 2
A modify() 12 12 3

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 Graze\DataFile\Modify\Encoding;
4
5
use Graze\DataFile\Helper\OptionalLoggerTrait;
6
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface;
7
use Graze\DataFile\Helper\Process\ProcessTrait;
8
use Graze\DataFile\Modify\FileModifierInterface;
9
use Graze\DataFile\Node\FileNodeInterface;
10
use Graze\DataFile\Node\LocalFileNodeInterface;
11
use InvalidArgumentException;
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LogLevel;
14
use Symfony\Component\Process\Exception\ProcessFailedException;
15
16
class FindEncoding implements ProcessFactoryAwareInterface, LoggerAwareInterface, FileModifierInterface
17
{
18
    use ProcessTrait;
19
    use OptionalLoggerTrait;
20
21
    /**
22
     * Find the Encoding of a specified file
23
     *
24
     * @param LocalFileNodeInterface $file
25
     *
26
     * @return null|string
27
     * @throws ProcessFailedException
28
     */
29 6
    public function getEncoding(LocalFileNodeInterface $file)
30
    {
31 6
        $cmd = "file --brief --uncompress --mime {$file->getPath()}";
32
33 6
        $process = $this->getProcess($cmd);
34 6
        $process->mustRun();
35
36 5
        $result = $process->getOutput();
37 5
        if (preg_match('/charset=([^\s]+)/i', $result, $matches)) {
38 4
            $this->log(LogLevel::DEBUG, "Found the encoding for '{file}' as '{encoding}'", [
39 4
                'file'     => $file,
40 4
                'encoding' => $matches[1],
41 4
            ]);
42 4
            return $matches[1];
43
        }
44 1
        return null;
45
    }
46
47
    /**
48
     * Can this file be modified by this modifier
49
     *
50
     * @param FileNodeInterface $file
51
     *
52
     * @return bool
53
     */
54 3
    public function canModify(FileNodeInterface $file)
55
    {
56 3
        return ($file instanceof LocalFileNodeInterface &&
57 3
            $file->exists());
58
    }
59
60
    /**
61
     * Modify the file
62
     *
63
     * @param FileNodeInterface $file
64
     * @param array             $options
65
     *
66
     * @return FileNodeInterface
67
     * @throws InvalidArgumentException
68
     */
69 2 View Code Duplication
    public function modify(FileNodeInterface $file, array $options = [])
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 2
        if (!$this->canModify($file) || !($file instanceof LocalFileNodeInterface)) {
72 1
            throw new InvalidArgumentException(
73 1
                "The specified file: '$file' does not implement LocalFileNodeInterface'"
74 1
            );
75
        }
76
77 1
        $file->setEncoding($this->getEncoding($file));
78
79 1
        return $file;
80
    }
81
}
82