FindEncoding   A
last analyzed

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 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 12
loc 66
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

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