Passed
Pull Request — master (#43)
by Yasin
01:41
created

FactoryArticle::validateUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace One;
3
4
use One\Model\Article;
5
6
/**
7
 * FactoryArticle Class
8
 *
9
 * @method create
10
 * @method createArticle
11
 * @method validateArray
12
 * @method validateUrl
13
 * @method validateInteger
14
 * @method validateString
15
 * @method checkData
16
 *
17
 */
18
class FactoryArticle
19
{
20
21
    /**
22
     * Create Article from array
23
     *
24
     * @param array $data
25
     * @return object Article
26
     */
27
    public static function create($data)
28
    {
29
        $data = self::validateArray($data);
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryArticle::validateArray() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        /** @scrutinizer ignore-call */ 
30
        $data = self::validateArray($data);
Loading history...
Bug introduced by
$data of type array is incompatible with the type integer expected by parameter $var of One\FactoryArticle::validateArray(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $data = self::validateArray(/** @scrutinizer ignore-type */ $data);
Loading history...
30
        $title = self::validateString(self::checkData($data, 'title', ''));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryArticle::validateString() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        /** @scrutinizer ignore-call */ 
31
        $title = self::validateString(self::checkData($data, 'title', ''));
Loading history...
Bug introduced by
self::checkData($data, 'title', '') of type array is incompatible with the type string expected by parameter $var of One\FactoryArticle::validateString(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        $title = self::validateString(/** @scrutinizer ignore-type */ self::checkData($data, 'title', ''));
Loading history...
Bug Best Practice introduced by
The method One\FactoryArticle::checkData() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $title = self::validateString(self::/** @scrutinizer ignore-call */ checkData($data, 'title', ''));
Loading history...
31
        $body = self::validateString(self::checkData($data, 'body', ''));
32
        $source = self::validateUrl(self::checkData($data, 'source', ''));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryArticle::validateUrl() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        /** @scrutinizer ignore-call */ 
33
        $source = self::validateUrl(self::checkData($data, 'source', ''));
Loading history...
33
        $uniqueId = self::validateString(self::checkData($data, 'unique_id', ''));
34
        $typeId = self::validateInteger(self::checkData($data, 'type_id', ''));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryArticle::validateInteger() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        /** @scrutinizer ignore-call */ 
35
        $typeId = self::validateInteger(self::checkData($data, 'type_id', ''));
Loading history...
Bug introduced by
self::checkData($data, 'type_id', '') of type array is incompatible with the type integer expected by parameter $var of One\FactoryArticle::validateInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $typeId = self::validateInteger(/** @scrutinizer ignore-type */ self::checkData($data, 'type_id', ''));
Loading history...
35
        $categoryId = self::validateInteger(self::checkData($data, 'category_id', ''));
36
        $reporter = self::validateString(self::checkData($data, 'reporter', ''));
37
        $lead = self::validateString(self::checkData($data, 'lead', ''));
38
        $tags = self::validateString(self::checkData($data, 'tags', ''));
39
        $publishedAt = self::checkData($data, 'published_at', '');
40
        $identifier = self::checkData($data, 'identifier', '');
41
        return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier);
0 ignored issues
show
Bug introduced by
$publishedAt of type array is incompatible with the type One\date expected by parameter $publishedAt of One\FactoryArticle::createArticle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, /** @scrutinizer ignore-type */ $publishedAt, $identifier);
Loading history...
Bug introduced by
$identifier of type array is incompatible with the type integer expected by parameter $identifier of One\FactoryArticle::createArticle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, /** @scrutinizer ignore-type */ $identifier);
Loading history...
Bug Best Practice introduced by
The method One\FactoryArticle::createArticle() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        return self::/** @scrutinizer ignore-call */ createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier);
Loading history...
42
    }
43
44
    /**
45
     * functionality to check whether a variable is set or not.
46
     *
47
     * @param array $parts
48
     * @return array
49
     */
50
    private function checkData($data, $key, $default = '')
51
    {
52
        return isset($data[$key]) ? $data[$key] : $default;
53
    }
54
55
    /**
56
     * Create Article Object
57
     *
58
     * @param String $title
59
     * @param string $body
60
     * @param string $source
61
     * @param int $uniqueId
62
     * @param int $typeId
63
     * @param int $categoryId
64
     * @param string $reporter
65
     * @param string $lead
66
     * @param string $tags
67
     * @param date $publishedAt
0 ignored issues
show
Bug introduced by
The type One\date was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
     * @param int $identifier
69
     * @return Article Object
70
     */
71
    public function createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier)
72
    {
73
        return new Article(
74
            $title,
75
            $body,
76
            $source,
77
            $uniqueId,
78
            $typeId,
79
            $categoryId,
80
            $reporter,
81
            $lead,
82
            $tags,
83
            $publishedAt,
84
            $identifier
85
        );
86
    }
87
88
    /**
89
     * functionality validity for array variables
90
     *
91
     * @param int $var
92
     * @return int
93
     */
94
    private function validateArray($var)
95
    {
96
        if (is_array($var) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition is_array($var) == false is always true.
Loading history...
97
            throw new \Exception("The variable type must Array :" . $var);
0 ignored issues
show
Bug introduced by
Are you sure $var of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            throw new \Exception("The variable type must Array :" . /** @scrutinizer ignore-type */ $var);
Loading history...
98
        }
99
        return $var;
100
    }
101
102
    /**
103
     * Make Sure Url in string with correct url format
104
     *
105
     * @param String $string
106
     * @return string
107
     */
108
    private function validateUrl($var)
109
    {
110
        if (filter_var($var, FILTER_VALIDATE_URL) == false) {
111
            throw new \Exception("Invalid url : $var");
112
        }
113
        return $var;
114
    }
115
    private function parseUrl($string)
0 ignored issues
show
Unused Code introduced by
The method parseUrl() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
116
    {
117
        return parse_url($string);
118
    }
119
120
    /**
121
     * functionality validity for int variables
122
     *
123
     * @param int $var
124
     * @return int
125
     */
126
    private function validateInteger($var)
127
    {
128
        if (filter_var($var, FILTER_VALIDATE_INT) == false) {
129
            throw new \Exception("The variable type must Integer :" . $var);
130
        }
131
        return $var;
132
    }
133
134
    /**
135
     * functionality validity for string variables
136
     *
137
     * @param String $var
138
     * @return String
139
     */
140
    private function validateString($var)
141
    {
142
        if (is_string($var) == false) {
0 ignored issues
show
introduced by
The condition is_string($var) == false is always false.
Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
143
            throw new \Exception("The variable type must String :" . $var);
144
        }
145
        return $var;
146
    }
147
}
148