Completed
Push — master ( 5251c6...854e3d )
by Maik
01:23
created

QueryResultParser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 100
Duplicated Lines 18 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 18
loc 100
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B parseQueryResult() 9 23 4
A assignPropertyValue() 0 6 2
A getPropertyName() 0 8 2
B parseRelatedTopics() 9 20 5
B parseIcon() 0 23 6
A parseMeta() 0 4 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
namespace Nkey\DDG\API;
3
4
use Nkey\DDG\API\Model\Icon;
5
use Nkey\DDG\API\Model\QueryResult;
6
use Nkey\DDG\API\Model\RelatedTopic;
7
use Webmozart\Json\JsonDecoder;
8
use ReflectionClass;
9
use ReflectionMethod;
10
11
/**
12
 * This class provides means for parsing json from duckduckgo api into a local model
13
 *
14
 * @author Maik Greubel <[email protected]>
15
 * @license Apache 2.0
16
 */
17
class QueryResultParser
18
{
19
20
    /**
21
     * Parse a payload json string
22
     *
23
     * @param string $payload
24
     *            The payload to parse
25
     * @return QueryResult
26
     */
27 2
    public function parseQueryResult(string $payload): QueryResult
28
    {
29 2
        $decoder = new JsonDecoder();
30
        
31 2
        $json = $decoder->decode($payload);
32
        
33 2
        $result = new QueryResult();
34
        
35 2
        $ref = new ReflectionClass($result);
36 2
        $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
37 2 View Code Duplication
        foreach ($methods as $method) {
0 ignored issues
show
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...
38 2
            if (($propertyName = $this->getPropertyName($method))) {
39 2
                if ($propertyName === 'Meta') {
40 2
                    $result = $this->parseMeta($result, $json->meta);
41 2
                    continue;
42
                }
43 2
                $this->assignPropertyValue($method, $result, $json, $propertyName);
44
            }
45
        }
46
        
47 2
        $result = $this->parseRelatedTopics($result, $json->RelatedTopics);
48 2
        return $result;
49
    }
50
    
51 2
    private function assignPropertyValue(ReflectionMethod $method, $destObject, $object, $propertyName) {
52 2
        if (property_exists($object, $propertyName)) {
53 2
            $property = $object->$propertyName;
54 2
            $method->invoke($destObject, $property);
55
        }
56 2
    }
57
    
58 2
    private function getPropertyName(ReflectionMethod $method) {
59 2
        $propertyName = false;
60 2
        if (substr($method->name, 0, 3) === 'set') {
61 2
            $propertyName = substr($method->name, 3);
62
        }
63
        
64 2
        return $propertyName;
65
    }
66
67 2
    private function parseRelatedTopics(QueryResult $result, $relatedTopics)
68
    {
69 2
        foreach ($relatedTopics as $relatedTopic) {
70 2
            $topic = new RelatedTopic();
71 2
            $ref = new ReflectionClass($topic);
72 2
            $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
73 2 View Code Duplication
            foreach ($methods as $method) {
0 ignored issues
show
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...
74 2
                if (($propertyName = $this->getPropertyName($method))) {
75 2
                    if ($propertyName === 'Icon') {
76 2
                        $topic = $this->parseIcon($topic, $relatedTopic->Icon);
77 2
                        continue;
78
                    }
79 2
                    $this->assignPropertyValue($method, $topic, $relatedTopic, $propertyName);
80
                }
81
            }
82 2
            $result->addRelatedTopic($topic);
83
        }
84
        
85 2
        return $result;
86
    }
87
88 2
    private function parseIcon(RelatedTopic $topic, $icon): RelatedTopic
89
    {
90 2
        $ico = new Icon();
91
        
92 2
        $ref = new ReflectionClass($ico);
93 2
        $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
94 2
        foreach ($methods as $method) {
95 2
            if (($propertyName = $this->getPropertyName($method))) {
96 2
                if (property_exists($icon, $propertyName)) {
97 2
                    $property = $icon->$propertyName;
98
                    
99 2
                    if ($propertyName === 'Height' || $propertyName === 'Width') {
100 2
                        $property = intval($property);
101
                    }
102 2
                    $method->invoke($ico, $property);
103
                }
104
            }
105
        }
106
        
107 2
        $topic->setIcon($ico);
108
        
109 2
        return $topic;
110
    }
111
112 2
    private function parseMeta(QueryResult $result, $meta): QueryResult
0 ignored issues
show
Unused Code introduced by
The parameter $meta 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...
113
    {
114 2
        return $result;
115
    }
116
}