Passed
Branch master (950424)
by Christopher
11:06
created

Guid::getFullTypeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace POData\Providers\Metadata\Type;
4
5
/**
6
 * Class Guid.
7
 */
8
class Guid implements IType
9
{
10
    /**
11
     * Gets the type code
12
     * Note: implementation of IType::getTypeCode.
13
     *
14
     * @return TypeCode
15
     */
16
    public function getTypeCode()
17
    {
18
        return TypeCode::GUID;
0 ignored issues
show
Bug Best Practice introduced by
The expression return POData\Providers\...ata\Type\TypeCode::GUID returns the type integer which is incompatible with the documented return type POData\Providers\Metadata\Type\TypeCode.
Loading history...
19
    }
20
21
    /**
22
     * Checks this type is compatible with another type
23
     * Note: implementation of IType::isCompatibleWith.
24
     *
25
     * @param IType $type Type to check compatibility
26
     *
27
     * @return bool
28
     */
29
    public function isCompatibleWith(IType $type)
30
    {
31
        return TypeCode::GUID == $type->getTypeCode();
32
    }
33
34
    /**
35
     * Validate a value in Astoria uri is in a format for this type
36
     * Note: implementation of IType::validate.
37
     *
38
     * @param string $value     The value to validate
39
     * @param string &$outValue The stripped form of $value that can be
40
     *                          used in PHP expressions
41
     *
42
     * @return bool
43
     */
44
    public function validate($value, &$outValue)
45
    {
46
        ////The GUID value present in the $filter option should have one of the following pattern.
47
        //1. '/^guid\'([0-9a-fA-F]{32}\')?$/';
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
        //2. '/^guid\'([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\')?$/';
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
        //3. '/^guid\'\{?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}?\')?$/';
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
        //4. '/^guid\'\(?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)?\')?$/';
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
52
        $length = strlen($value);
53
        if (38 != $length && 42 != $length && 44 != $length) {
54
            return false;
55
        }
56
57
        if (0 !== strpos($value, 'guid\'') && '\'' != $value[$length - 1]) {
58
            return false;
59
        }
60
61
        $value = substr($value, 4, $length - 4);
62
        if (!self::validateWithoutPrefix($value, true)) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type false; however, parameter $guid of POData\Providers\Metadat...validateWithoutPrefix() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

62
        if (!self::validateWithoutPrefix(/** @scrutinizer ignore-type */ $value, true)) {
Loading history...
63
            return false;
64
        }
65
66
        $outValue = $value;
67
68
        return true;
69
    }
70
71
    /**
72
     * Gets full name of this type in EDM namespace
73
     * Note: implementation of IType::getFullTypeName.
74
     *
75
     * @return string
76
     */
77
    public function getFullTypeName()
78
    {
79
        return 'Edm.Guid';
80
    }
81
82
    /**
83
     * Converts the given string value to guid type.
84
     *
85
     * @param string $stringValue value to convert
86
     *
87
     * @return string
88
     */
89
    public function convert($stringValue)
90
    {
91
        $len = strlen($stringValue);
92
        if (2 > $len) {
93
            return $stringValue;
94
        }
95
96
        return substr($stringValue, 1, $len - 2);
0 ignored issues
show
Bug Best Practice introduced by
The expression return substr($stringValue, 1, $len - 2) could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
97
    }
98
99
    /**
100
     * Convert the given value to a form that can be used in OData uri.
101
     * Note: The calling function should not pass null value, as this
102
     * function will not perform any check for nullability.
103
     *
104
     * @param mixed $value value to convert
105
     *
106
     * @return string
107
     */
108
    public function convertToOData($value)
109
    {
110
        return 'guid\'' . urlencode($value) . '\'';
111
    }
112
113
    /**
114
     * Validates guid.
115
     *
116
     * @param string $guid       The guid to validate
117
     * @param bool   $withQuotes Whether the above guid have quote as delimiter
118
     *
119
     * @return bool
120
     */
121
    public static function validateWithoutPrefix($guid, $withQuotes = false)
122
    {
123
        if ($withQuotes) {
124
            $patt = ['/^(\'[0-9a-fA-F]{32}\')?$/',
125
                '/^(\'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\')?$/',
126
                '/^\'\{?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}?\')?$/',
127
                '/^\'\(?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)?\')?$/', ];
128
        } else {
129
            $patt = ['/^([0-9a-fA-F]{32})?$/',
130
                '/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})?$/',
131
                '/^\{?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}?)?$/',
132
                '/^\(?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)?)?$/', ];
133
        }
134
135
        foreach ($patt as $pattern) {
136
            if (1 == preg_match($pattern, $guid)) {
137
                return true;
138
            }
139
        }
140
141
        return false;
142
    }
143
144
    /**
145
     * Check the equality of two guids. This function will not validate the
146
     * guids one should use validate or validateWithoutPrefix to validate the
147
     * guids before using them with this function.
148
     *
149
     * @param string $guid1 First guid
150
     * @param string $guid2 Second guid
151
     *
152
     * @return bool True if both guids are same else false
153
     */
154
    public static function guidEqual($guid1, $guid2)
155
    {
156
        $guid1 = str_replace(['{', '}', '(', ')', '-'], '', $guid1);
157
        $guid2 = str_replace(['{', '}', '(', ')', '-'], '', $guid2);
158
159
        return 0 === strcasecmp($guid1, $guid2);
160
    }
161
162
    /**
163
     * Gets full name of the type implementing this interface in EDM namespace
164
     * Note: implementation of IType::getFullTypeName.
165
     *
166
     * @return string
167
     */
168
    public function getName()
169
    {
170
        return $this->getFullTypeName();
171
    }
172
}
173