RemoveByteOrderMarkTransformer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 45.65 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75.68%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 42
loc 92
ccs 28
cts 37
cp 0.7568
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B transform() 31 31 3
A needsTransforming() 11 11 1
A rename() 0 6 2
A getBomRegex() 0 4 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
namespace TreeHouse\Feeder\Resource\Transformer;
4
5
use TreeHouse\Feeder\Resource\FileResource;
6
use TreeHouse\Feeder\Resource\ResourceCollection;
7
use TreeHouse\Feeder\Resource\ResourceInterface;
8
use TreeHouse\Feeder\Transport\FileTransport;
9
10
/**
11
 * Strips the BOM from the beginning of the resource.
12
 */
13
class RemoveByteOrderMarkTransformer implements ResourceTransformerInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $boms;
19
20
    /**
21
     * Constructor.
22
     */
23 10
    public function __construct()
24
    {
25 10
        $this->boms = [
26 10
            '\x00\x00\xFE\xFF', // UTF-32 (BE)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27 10
            '\xFF\xFE\x00\x00', // UTF-32 (LE)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28 10
            '\xFE\xFF',         // UTF-16 (BE)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29 10
            '\xFF\xFE',         // UTF-16 (LE)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30 10
            '\xEF\xBB\xBF',     // UTF-8
31
        ];
32 10
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37 10 View Code Duplication
    public function transform(ResourceInterface $resource, ResourceCollection $collection)
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...
38
    {
39 10
        $file = $resource->getFile()->getPathname();
40
41
        // the file could be big, so just read the
42 10
        $tmpFile = tempnam(sys_get_temp_dir(), $file);
43 10
        $old = fopen($file, 'r');
44 10
        $new = fopen($tmpFile, 'w');
45
46
        // write the beginning with the BOM stripped
47 10
        fwrite($new, preg_replace($this->getBomRegex(), '', fread($old, 16)));
48
49
        // now copy the rest of the file
50 10
        while (!feof($old)) {
51 10
            fwrite($new, fread($old, 8192));
52 10
        }
53
54 10
        fclose($old);
55 10
        fclose($new);
56
57
        // atomic write
58 10
        $this->rename($tmpFile, $file);
59
60 10
        $transport = FileTransport::create($file);
61
62 10
        if ($resource->getTransport()) {
63
            $transport->setDestinationDir($resource->getTransport()->getDestinationDir());
64
        }
65
66 10
        return new FileResource($transport);
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 View Code Duplication
    public function needsTransforming(ResourceInterface $resource)
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...
73
    {
74
        $file = $resource->getFile()->getPathname();
75
        $handle = fopen($file, 'r');
76
77
        $result = (bool) preg_match($this->getBomRegex(), fread($handle, 16));
78
79
        fclose($handle);
80
81
        return $result;
82
    }
83
84
    /**
85
     * @param string $old
86
     * @param string $new
87
     *
88
     * @throws \RuntimeException
89
     */
90 10
    protected function rename($old, $new)
91
    {
92 10
        if (!rename($old, $new)) {
93
            throw new \RuntimeException(sprintf('Could not rename %s to %s', $old, $new));
94
        }
95 10
    }
96
97
    /**
98
     * @return string
99
     */
100 10
    protected function getBomRegex()
101
    {
102 10
        return sprintf('/^(%s)/', implode('|', $this->boms));
103
    }
104
}
105