Completed
Pull Request — master (#1)
by
unknown
05:55
created

StringType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 18.56 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 18
loc 97
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeCode() 0 4 1
A isCompatibleWith() 0 4 1
A validate() 0 9 2
A getFullTypeName() 0 4 1
A convert() 18 18 2
A convertToOData() 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
3
namespace POData\Providers\Metadata\Type;
4
5
/**
6
 * Class StringType
7
 * @package POData\Providers\Metadata\Type
8
 */
9
class StringType implements IType
10
{
11
    /**
12
     * Gets the type code
13
     * Note: implementation of IType::getTypeCode
14
     *   
15
     * @return TypeCode
16
     */
17
    public function getTypeCode()
18
    {
19
        return TypeCode::STRING;
20
    }
21
22
    /**
23
     * Checks this type (String) is compatible with another type
24
     * Note: implementation of IType::isCompatibleWith
25
     * 
26
     * @param IType $type Type to check compatibility
27
     * 
28
     * @return boolean 
29
     */
30
    public function isCompatibleWith(IType $type)
31
    {
32
        return ($type->getTypeCode() == TypeCode::STRING);
33
    }
34
35
    /**
36
     * Validate a value in Astoria uri is in a format for this type
37
     * Note: implementation of IType::validate
38
     * 
39
     * @param string $value     The value to validate 
40
     * @param string &$outValue The stripped form of $value that can 
41
     *                          be used in PHP expressions
42
     * 
43
     * @return boolean
44
     */
45
    public function validate($value, &$outValue)
46
    {
47
        if (!is_string($value)) {
48
            return false;
49
        }
50
        
51
        $outValue = $value;
52
        return true;
53
    }
54
55
    /**
56
     * Gets full name of this type in EDM namespace
57
     * Note: implementation of IType::getFullTypeName
58
     * 
59
     * @return string
60
     */
61
    public function getFullTypeName()
62
    {
63
        return 'Edm.String';
64
    }
65
66
    /**
67
     * Converts the given string value to string type.
68
     * 
69
     * @param string $stringValue value to convert.
70
     * 
71
     * @return string
72
     */
73 View Code Duplication
    public function convert($stringValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
75
        //Consider the odata url option 
76
        //$filter=ShipName eq 'Antonio%20Moreno%20Taquer%C3%ADa'
77
        //WebOperationContext will do urldecode, so the clause become
78
        //$filter=ShipName eq 'Antonio Moreno Taquería', the lexer will
79
        //give the token as
80
        //Token {Text string(25):'Antonio Moreno Taquería', Id: String}, 
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
81
        //this function is used to remove the pre-post quotes from Token::Text 
82
        //i.e. 'Antonio Moreno Taquería'
83
        //to Antonio Moreno Taquería
84
        $len = strlen($stringValue);
85
        if ($len < 2) {
86
            return $stringValue;
87
        }
88
89
        return substr($stringValue, 1, $len - 2);
90
    }
91
92
    /**
93
     * Convert the given value to a form that can be used in OData uri. 
94
     * Note: The calling function should not pass null value, as this 
95
     * function will not perform any check for nullability 
96
     * 
97
     * @param mixed $value value to convert.
98
     * 
99
     * @return string
100
     */
101
    public function convertToOData($value)
102
    {
103
        return '\'' . str_replace('%27', "''", urlencode(utf8_encode($value))) . '\'';
104
    }
105
}