Completed
Push — master ( 6e1c48...7d90f1 )
by John
03:00
created

StrictSimpleObjectProcessor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 8
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B hydrateObject() 8 22 4
A dehydrateObject() 0 16 3

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 declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions\Hydrator 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
9
namespace KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Object;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ObjectSchema;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class StrictSimpleObjectProcessor extends ObjectProcessor
18
{
19
    /**
20
     * @param \stdClass $input
21
     * @return \stdClass
22
     */
23
    public function hydrateObject(\stdClass $input)
24
    {
25
        $object = (object)[];
26
27
        /** @var ObjectSchema $objectSchema */
28
        $objectSchema = $this->schema;
29
30
        /**
31
         * @var string $name
32
         * @var Schema $propertySchema
33
         */
34 View Code Duplication
        foreach ($objectSchema->getPropertySchemas() as $name => $propertySchema) {
0 ignored issues
show
Bug introduced by
The expression $objectSchema->getPropertySchemas() of type object<stdClass> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
35
            if (!isset($input->$name) && isset($this->defaults[$name])) {
36
                $value = $this->defaults[$name];
37
            } else {
38
                $value = $input->$name;
39
            }
40
            $object->$name = $this->hydrateProperty($name, $value);
41
        }
42
43
        return $object;
44
    }
45
46
    /**
47
     * @param object $object
48
     * @return \stdClass
49
     */
50
    protected function dehydrateObject($object): \stdClass
51
    {
52
        $node = (object)[];
53
        /** @var ObjectSchema $objectSchema */
54
        $objectSchema = $this->schema;
55
56
        foreach ($object as $name => $value) {
57
            if ($this->shouldFilterOutputValue($objectSchema->getPropertySchema($name), $value)) {
58
                continue;
59
            } else {
60
                $node->$name = $this->dehydrateProperty($name, $value);
61
            }
62
        }
63
64
        return $node;
65
    }
66
}
67