JSONRepresentationTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 46.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 0
dl 12
loc 99
ccs 15
cts 32
cp 0.4688
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A persistRepresentJSON() 0 7 1
A loadRepresentJSON() 0 6 1
A persistRepresentObj() 0 15 3
A loadRepresentObj() 12 12 3
A cameCaseTransformToUnderscores() 0 4 1
A underscoresTransformToCameCase() 0 13 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 TonicForHealth\PagerDutyClient\RepresentProcessor\Representation\Traits;
4
5
use stdClass;
6
use TonicForHealth\PagerDutyClient\Entity\PagerDutyEntityInterface;
7
8
/**
9
 * Trait JSONRepresentationTrait.
10
 */
11
trait JSONRepresentationTrait
12
{
13
    /**
14
     * @param PagerDutyEntityInterface $pagerDutyEntity
15
     *
16
     * @return string
17
     */
18 4
    public function persistRepresentJSON(PagerDutyEntityInterface $pagerDutyEntity)
19
    {
20 4
        return json_encode(
21 4
           $this->persistRepresentObj($pagerDutyEntity),
22 4
           JSON_PRETTY_PRINT
23
       );
24
    }
25
26
    /**
27
     * @param string                        $jsonString
28
     * @param PagerDutyEntityInterface|null $pagerDutyEntity
29
     *
30
     * @return PagerDutyEntityInterface
31
     */
32
    public function loadRepresentJSON($jsonString, PagerDutyEntityInterface $pagerDutyEntity = null)
0 ignored issues
show
Unused Code introduced by
The parameter $pagerDutyEntity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        return $this->loadRepresentObj(
35
            json_decode($jsonString)
36
        );
37
    }
38
39
    /**
40
     * @param PagerDutyEntityInterface $pagerDutyEntity
41
     *
42
     * @return stdClass
43
     */
44 4
    public function persistRepresentObj(PagerDutyEntityInterface $pagerDutyEntity)
45
    {
46 4
        $representObject = new stdClass();
47
48 4
        $objArray = (array) $pagerDutyEntity;
49 4
        foreach ($objArray as $key => $value) {
50 4
            if (null === $value) {
51 4
                continue;
52
            }
53 4
            $key = $this->cameCaseTransformToUnderscores($key);
54 4
            $representObject->$key = $value;
55
        }
56
57 4
        return $representObject;
58
    }
59
60
    /**
61
     * @param stdClass                      $obj
62
     * @param PagerDutyEntityInterface|null $pagerDutyEntity
63
     */
64 View Code Duplication
    public function loadRepresentObj(stdClass $obj, PagerDutyEntityInterface $pagerDutyEntity = null)
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...
65
    {
66
        if (null === $pagerDutyEntity) {
67
            $entityClassName = static::getEntityClassName();
68
            $pagerDutyEntity = new $entityClassName();
0 ignored issues
show
Unused Code introduced by
$pagerDutyEntity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
        }
70
        $objArray = (array) $obj;
71
        foreach ($objArray as $key => $value) {
72
            $key = $this->underscoresTransformToCameCase($key);
73
            $representObject->$key = $value;
0 ignored issues
show
Bug introduced by
The variable $representObject does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
74
        }
75
    }
76
77
    /**
78
     * Decamelize str cameCase string to underscored.
79
     *
80
     * @param string $cameCaseStr
81
     *
82
     * @return string
83
     */
84 4
    protected function cameCaseTransformToUnderscores($cameCaseStr)
85
    {
86 4
        return ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', $cameCaseStr)), '_');
87
    }
88
89
    /**
90
     * Decamelize str cameCase string to underscored.
91
     *
92
     * @param string $underscores
93
     *
94
     * @return string
95
     */
96
    protected function underscoresTransformToCameCase($underscores)
97
    {
98
        return ltrim(
99
            preg_replace_callback(
100
                '/_[a-z]/',
101
                function ($matches) {
102
                    return substr(strtolower($matches[0]), 1);
103
                },
104
                $underscores
105
            ),
106
            '_'
107
        );
108
    }
109
}
110