Passed
Pull Request — master (#21)
by kenny
07:05
created

FormatMapping::article()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 39
rs 9.296
1
<?php
2
3
namespace one;
4
5
use One\Model\Article;
6
7
class FormatMapping
8
{
9
10
    /**
11
     * map a single article to main attributes in Article Class
12
     * @param  string $singleJsonArticle JSON response
13
     * @return Article
14
     * @throws Exception
15
     */
16
    public function article($singleJsonArticle)
17
    {
18
        if (json_decode($singleJsonArticle, true)) {
19
            $dataArticle = json_decode($singleJsonArticle, true)['data'];
20
21
            $article = new Article(
22
23
                $title = $this->filterString($this->getValue('title', $dataArticle)),
24
25
                $body = $this->filterString($this->getValue('body', $dataArticle)),
26
27
                $source = $this->filterString($this->getValue('source', $dataArticle)),
28
29
                $uniqueId = $this->getValue('unique_id', $dataArticle),
30
31
                $typeId = $this->filterInteger($this->getValue('type_id', $dataArticle['type'])),
0 ignored issues
show
Documentation introduced by
$typeId = $this->filterI... $dataArticle['type'])) is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
33
                $categoryId = $this->filterInteger($this->getValue(
0 ignored issues
show
Documentation introduced by
$categoryId = $this->fil...taArticle['category'])) is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
                    'category_id',
35
                    $dataArticle['category']
36
                )),
37
38
                $reporter = $this->getValue('reporter', $dataArticle),
39
40
                $lead = $this->filterString($this->getValue('lead', $dataArticle)),
41
42
                $tags = $this->getValue('tag_name', $dataArticle['tags']),
43
44
                $publishedAt = $this->filterString($this->getValue('published_at', $dataArticle)),
45
46
                $identifier = $this->filterInteger($this->getValue('id', $dataArticle))
47
48
            );
49
50
            return $article;
51
        }
52
53
        throw new \Exception("Empty or invalid JSON Response", 1);
54
    }
55
56
    /**
57
     * Make sure value is integer
58
     * @param  mixed $int
59
     * @return boolean
60
     * @throws Exception
61
     */
62
    private function filterInteger($int)
63
    {
64
        if (is_int((int) $int)) {
65
            return $int;
66
        }
67
        throw new \Exception("Invalid Integer", 1);
68
    }
69
70
    /**
71
     * Make sure string is not null or empty
72
     * @param   mixed $str
73
     * @return string if it is valid or exception
74
     * @throws Exception
75
     */
76
    private function filterString($str)
77
    {
78
        if (is_string($str) && strlen($str) > 0 && !is_null($str)) {
79
            return $str;
80
        }
81
        throw new \Exception("String required", 1);
82
    }
83
84
    /**
85
     * Get value of array based on attributes(keys)
86
     * @param  supported php variables $attribute
87
     * @param  array $data
88
     * @return supported php variables
89
     */
90
    private function getValue($attribute, $data)
91
    {
92
        return isset($data[$attribute]) ? $data[$attribute] : null;
93
    }
94
}
95