Completed
Push — master ( d14e53...2de3b1 )
by Nikola
04:53
created

SimpleXmlAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 38.71 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 24
loc 62
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
B convert() 24 24 4
A __construct() 0 7 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
 * This file is part of the runopencode/sax, an RunOpenCode project.
4
 *
5
 * (c) 2017 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 RunOpenCode\Sax\Contract\StreamAdapterInterface;
13
use RunOpenCode\Sax\Exception\RuntimeException;
14
use RunOpenCode\Sax\Exception\StreamAdapterException;
15
16
/**
17
 * Class SimpleXmlAdapter
18
 *
19
 * SimpleXml adapter
20
 *
21
 * @package RunOpenCode\Sax\StreamAdapter
22
 */
23
class SimpleXmlAdapter implements StreamAdapterInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $streamClass;
29
30
    /**
31
     * @var array
32
     */
33
    private $options;
34
35
    /**
36
     * SimpleXmlAdapter constructor.
37
     *
38
     * @param string $streamClass FQCN of StreamInterface implementation, GuzzleHttp\Psr7\Stream is used by default.
39
     * @param array $options Adapter options.
40
     */
41 4
    public function __construct($streamClass = 'GuzzleHttp\\Psr7\\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...
42
    {
43 4
        $this->streamClass = $streamClass;
44 4
        $this->options = array_merge(array(
45 4
            'stream' => 'php://memory',
46 4
        ), $options);
47 4
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 4
    public function supports($xmlDocument)
53
    {
54 4
        return $xmlDocument instanceof \SimpleXMLElement;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3 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 \SimpleXMLElement $xmlDocument
64
         */
65 3
        if (class_exists($this->streamClass)) {
66
67 3
            $stream = fopen($this->options['stream'], 'r+b');
68
69 3
            if (false === $stream) {
70
                throw new StreamAdapterException(sprintf('Unable to acquire resource handler on "%s".', $this->options['stream']));
71
            }
72
73 3
            fwrite($stream, $xmlDocument->asXML());
74
75 3
            if (false === rewind($stream)) {
76
                throw new StreamAdapterException('Unable to to rewind stream.');
77
            }
78
79 3
            return new $this->streamClass($stream);
80
        }
81
82
        throw new RuntimeException(sprintf('Provided StreamInterface implementation "%s" is not available on this system.', $this->streamClass));
83
    }
84
}
85