Completed
Push — master ( 91bb83...325361 )
by Nikola
05:28
created

DomDocumentAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 43.64 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A supports() 0 4 1
A convert() 16 16 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
 * This file is part of the runopencode/sax, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Sax\StreamAdapter;
11
12
use Psr\Http\Message\StreamInterface;
13
use RunOpenCode\Sax\Contract\StreamAdapterInterface;
14
15
/**
16
 * Class DomAdapter
17
 *
18
 * DOMDocument to stream adapter.
19
 *
20
 * @package RunOpenCode\Sax\Contract
21
 */
22
class DomDocumentAdapter implements StreamAdapterInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $streamClass;
28
29
    /**
30
     * @var array
31
     */
32
    private $options;
33
34
    /**
35
     * DomDocumentAdapter constructor.
36
     *
37
     * @param string $streamClass FQCN of StreamInterface implementation, GuzzleHttp\Stream\Stream is used by default.
38
     * @param array $options Adapter options.
39
     */
40 View Code Duplication
    public function __construct($streamClass = 'GuzzleHttp\\Stream\\Stream', array $options = array())
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...
41
    {
42
        $this->streamClass = $streamClass;
43
        $this->options = array_merge(array(
44
            'stream' => 'php://memory',
45
            'save_xml_options' => null
46
        ), $options);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function supports($xmlDocument)
53
    {
54
        return $xmlDocument instanceof \DOMDocument;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 View Code Duplication
    public function convert($xmlDocument)
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
        /**
63
         * @var \DOMDocument $xmlDocument
64
         */
65
        if (class_exists($this->streamClass)) {
66
67
            $stream = fopen($this->options['stream'],'r+');
68
            fwrite($stream, $xmlDocument->saveXML($this->options['save_xml_options']));
69
            rewind($stream);
70
71
            return new $this->streamClass($xmlDocument);
72
        }
73
74
        throw new \RuntimeException(sprintf('Provided StreamInterface implementation "%s" is not available on this system.', $this->streamClass));
75
    }
76
}
77