Completed
Push — master ( f1a8d5...1a6045 )
by Dmitry
03:22
created

JSONRepresentationTrait::persistRepresentObj()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3
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
           JSON_PRETTY_PRINT
23 4
       );
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 4
        }
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