OverwriteXmlDeclarationTransformer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 50.6 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 63.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 42
loc 83
ccs 19
cts 30
cp 0.6333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B transform() 31 31 3
A needsTransforming() 11 11 1
A rename() 0 6 2

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
 * Replaces xml declaration with a fixed one.
12
 */
13
class OverwriteXmlDeclarationTransformer implements ResourceTransformerInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $xmlDeclaration;
19
20
    /**
21
     * @var string regexp
22
     */
23
    protected $xmlDeclarationRegEx = '/^\<\?xml.*\?\>/i';
24
25
    /**
26
     * @param string $xmlDeclaration
27
     */
28 6
    public function __construct($xmlDeclaration = '<?xml version="1.0" encoding="UTF-8"?>')
29
    {
30 6
        $this->xmlDeclaration = $xmlDeclaration;
31 6
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 6 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...
37
    {
38 6
        $file = $resource->getFile()->getPathname();
39
40
        // the file could be big, so just read the
41 6
        $tmpFile = tempnam(sys_get_temp_dir(), $file);
42 6
        $old = fopen($file, 'r');
43 6
        $new = fopen($tmpFile, 'w');
44
45
        // write the beginning with the xml declaration replaced
46 6
        fwrite($new, preg_replace($this->xmlDeclarationRegEx, $this->xmlDeclaration, fread($old, 96)));
47
48
        // now copy the rest of the file
49 6
        while (!feof($old)) {
50
            fwrite($new, fread($old, 8192));
51
        }
52
53 6
        fclose($old);
54 6
        fclose($new);
55
56
        // atomic write
57 6
        $this->rename($tmpFile, $file);
58
59 6
        $transport = FileTransport::create($file);
60
61 6
        if ($resource->getTransport()) {
62
            $transport->setDestinationDir($resource->getTransport()->getDestinationDir());
63
        }
64
65 6
        return new FileResource($transport);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 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...
72
    {
73
        $file = $resource->getFile()->getPathname();
74
        $handle = fopen($file, 'r');
75
76
        $result = (bool) preg_match($this->xmlDeclarationRegEx, fread($handle, 96));
77
78
        fclose($handle);
79
80
        return $result;
81
    }
82
83
    /**
84
     * @param string $old
85
     * @param string $new
86
     *
87
     * @throws \RuntimeException
88
     */
89 6
    protected function rename($old, $new)
90
    {
91 6
        if (!rename($old, $new)) {
92
            throw new \RuntimeException(sprintf('Could not rename %s to %s', $old, $new));
93
        }
94 6
    }
95
}
96