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

FactoryPhoto::validateString()   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\Photo;
5
6
/**
7
 * FactoryPhoto Class
8
 *
9
 * @method create
10
 * @method createPhoto
11
 * @method validateUrl
12
 * @method validateInteger
13
 * @method validateString
14
 * @method checkData
15
 */
16
class FactoryPhoto
17
{
18
19
    /**
20
     * function Create Photo Attachment
21
     *
22
     * @param String $string
23
     * @return object Uri
24
     */
25
    public static function create($data)
26
    {
27
        $url = self::validateUrl(self::checkData($data, 'url', ''));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryPhoto::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

27
        $url = self::validateUrl(self::/** @scrutinizer ignore-call */ checkData($data, 'url', ''));
Loading history...
Bug Best Practice introduced by
The method One\FactoryPhoto::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

27
        /** @scrutinizer ignore-call */ 
28
        $url = self::validateUrl(self::checkData($data, 'url', ''));
Loading history...
Bug introduced by
self::checkData($data, 'url', '') of type array is incompatible with the type string expected by parameter $string of One\FactoryPhoto::validateUrl(). ( Ignorable by Annotation )

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

27
        $url = self::validateUrl(/** @scrutinizer ignore-type */ self::checkData($data, 'url', ''));
Loading history...
28
        $ratio = self::checkData($data, 'ratio', '');
29
        $description = self::validateString(self::checkData($data, 'description', ''));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryPhoto::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

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

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

29
        $description = self::validateString(/** @scrutinizer ignore-type */ self::checkData($data, 'description', ''));
Loading history...
30
        $information = self::validateString(self::checkData($data, 'information', ''));
31
32
        return self::createPhoto($url, $ratio, $description, $information);
0 ignored issues
show
Bug introduced by
$ratio of type array is incompatible with the type string expected by parameter $ratio of One\FactoryPhoto::createPhoto(). ( Ignorable by Annotation )

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

32
        return self::createPhoto($url, /** @scrutinizer ignore-type */ $ratio, $description, $information);
Loading history...
Bug Best Practice introduced by
The method One\FactoryPhoto::createPhoto() 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
        return self::/** @scrutinizer ignore-call */ createPhoto($url, $ratio, $description, $information);
Loading history...
33
    }
34
35
    /**
36
     * Make Sure Url in string with correct url format
37
     *
38
     * @param String $string
39
     * @return string
40
     */
41
    private function validateUrl($string)
42
    {
43
        if (filter_var($string, FILTER_VALIDATE_URL) == false) {
44
            throw new \Exception("Invalid url : $string");
45
        }
46
        return $string;
47
    }
48
49
    /**
50
     * functionality to check whether a variable is set or not.
51
     *
52
     * @param array $parts
53
     * @return array
54
     */
55
    private function checkData($data, $key, $default = '')
56
    {
57
        return isset($data[$key]) ? $data[$key] : $default;
58
    }
59
60
    /**
61
     * functionality validity for int variables
62
     *
63
     * @param int $var
64
     * @return int
65
     */
66
    private function validateInteger($var)
0 ignored issues
show
Unused Code introduced by
The method validateInteger() 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...
67
    {
68
        if (filter_var($var, FILTER_VALIDATE_INT) == false) {
69
            throw new \Exception("The variable must be a integer :" . $var);
70
        }
71
        return $var;
72
    }
73
74
    /**
75
     * functionality validity for string variables
76
     *
77
     * @param String $var
78
     * @return String
79
     */
80
    private function validateString($var)
81
    {
82
        if (is_string($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_string($var) == false is always false.
Loading history...
83
            throw new \Exception("The variable must be a string :" . $var);
84
        }
85
        return $var;
86
    }
87
88
    /**
89
     * Create Photo Object
90
     *
91
     * @param String $url
92
     * @param String $ratio
93
     * @param String $description
94
     * @return Uri Photo
95
     */
96
    public function createPhoto($url, $ratio, $description, $information)
97
    {
98
        return new Photo(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new One\Model\Pho...cription, $information) returns the type One\Model\Photo which is incompatible with the documented return type One\Uri.
Loading history...
99
            $url,
100
            $ratio,
101
            $description,
102
            $information
103
        );
104
    }
105
}
106