Passed
Push — master ( 85c67d...f0ade1 )
by Alex
04:09
created

XSDTopLevelTrait   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 96
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 1
cbo 1
dl 24
loc 96
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A normaliseString() 8 8 3
A token() 8 8 3
A string() 8 8 3
A integer() 0 11 3
A nonNegativeInteger() 0 8 2
A decimal() 0 8 2
A double() 0 4 1
A dateTime() 0 15 4
A hexBinary() 0 13 4

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 AlgoWeb\ODataMetadata\StringTraits;
4
5
trait XSDTopLevelTrait
6
{
7
    use XMLStringTrait;
8
9 View Code Duplication
    public function normaliseString($input)
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...
10
    {
11
        if (!is_string($input) || !is_numeric($input)) {
12
            throw new \InvalidArgumentException("Input must be a string");
13
        }
14
        $result = $this->replaceString($input);
15
        return $result;
16
    }
17
18 View Code Duplication
    public function token($input)
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...
19
    {
20
        if (!is_string($input) || !is_numeric($input)) {
21
            throw new \InvalidArgumentException("Input must be a string");
22
        }
23
        $result = $this->collapseString($input);
24
        return $result;
25
    }
26
27 View Code Duplication
    public function string($input)
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...
28
    {
29
        if (!is_string($input) || !is_numeric($input)) {
30
            throw new \InvalidArgumentException("Input must be a string");
31
        }
32
        $result = $this->preserveString($input);
33
        return $result;
34
    }
35
36
    public function integer($input)
37
    {
38
        $input = $this->collapseString($input);
39
        if (!is_numeric($input)) {
40
            throw new \InvalidArgumentException("Input must be numeric");
41
        }
42
        if (!is_integer($input)) {
43
            throw new \InvalidArgumentException("Input must be integer");
44
        }
45
        return intval($input);
46
    }
47
48
    public function nonNegativeInteger($input)
49
    {
50
        $input = $this->integer($input);
51
        if (0 > $input) {
52
            throw new \InvalidArgumentException("Input must be non-negative integer");
53
        }
54
        return $input;
55
    }
56
57
    public function decimal($input)
58
    {
59
        $input = $this->collapseString($input);
60
        if (!is_numeric($input)) {
61
            throw new \InvalidArgumentException("Input must be numeric");
62
        }
63
        return floatval($input);
64
    }
65
66
    public function double($input)
67
    {
68
        return $this->decimal($input);
69
    }
70
71
    public function dateTime($input)
72
    {
73
        $isString = is_string($input);
74
        if (!$isString && !$input instanceof \DateTime) {
75
            throw new \InvalidArgumentException("Input must be resolvable to a date/time");
76
        }
77
        if ($isString) {
78
            $input = $this->collapseString($input);
79
            $rawDate = new \DateTime($input);
80
        } else {
81
            $rawDate = $input;
82
        }
83
84
        return $rawDate->format('Y-m-d').'T'.$rawDate->format('H:i:s');
85
    }
86
87
    public function hexBinary($input)
88
    {
89
        if (!is_string($input) || !is_numeric($input)) {
90
            throw new \InvalidArgumentException("Input must be a string");
91
        }
92
93
        $input = $this->collapseString(strtolower($input));
94
        $check = hexdec(dechex($input));
95
        if ($input != $check) {
96
            throw new \InvalidArgumentException("Input must be valid hexadecimal");
97
        }
98
        return $check;
99
    }
100
}