Passed
Push — master ( b9ec10...5392bd )
by Adrian Florin
02:43
created

JsonContent   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 35.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 20
loc 57
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 1
A getArray() 10 10 2
A getObject() 10 10 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 NeedleProject\FileIo package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace NeedleProject\FileIo\Content;
9
10
use NeedleProject\FileIo\Exception\ContentException;
11
12
/**
13
 * Class JsonContent
14
 *
15
 * @package NeedleProject\FileIo\Content
16
 * @author Adrian Tilita <[email protected]>
17
 * @copyright 2017 Adrian Tilita
18
 * @license https://opensource.org/licenses/MIT MIT Licence
19
 */
20
class JsonContent implements ContentInterface
21
{
22
    /**
23
     * @var null|string
24
     */
25
    private $content = null;
26
27
    /**
28
     * Content constructor.
29
     *
30
     * @param string $content
31
     */
32 6
    public function __construct(string $content)
33
    {
34 6
        $this->content = $content;
35 6
    }
36
37
    /**
38
     * @return string
39
     */
40 1
    public function get(): string
41
    {
42 1
        return $this->content;
43
    }
44
45
    /**
46
     * Return the content as an array
47
     *
48
     * @return array
49
     * @throws \NeedleProject\FileIo\Exception\ContentException
50
     */
51 3 View Code Duplication
    public function getArray(): 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...
52
    {
53 3
        $content = json_decode($this->content, true);
54 3
        if (json_last_error() !== JSON_ERROR_NONE) {
55 2
            throw new ContentException(
56 2
                sprintf("Could not decode content, got %s", json_last_error_msg())
57
            );
58
        }
59 1
        return $content;
60
    }
61
62
    /**
63
     * @return \stdClass
64
     * @throws \NeedleProject\FileIo\Exception\ContentException
65
     */
66 3 View Code Duplication
    public function getObject(): \stdClass
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...
67
    {
68 3
        $content = json_decode($this->content);
69 3
        if (json_last_error() !== JSON_ERROR_NONE) {
70 2
            throw new ContentException(
71 2
                sprintf("Could not decode content, got %s", json_last_error_msg())
72
            );
73
        }
74 1
        return $content;
75
    }
76
}
77