Completed
Push — php7 ( ab40a7 )
by personal
04:06
created

TypeResolver::resolve()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 4.8197
cc 10
eloc 22
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\OOP\Resolver;
11
12
13
/**
14
 * Type resolver
15
 *
16
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
17
 */
18
class TypeResolver
19
{
20
21
    const TYPE_VOID = 'void';
22
    const TYPE_STRING = 'string';
23
    const TYPE_INTEGER = 'integer';
24
    const TYPE_FLOAT = 'float';
25
    const TYPE_ARRAY = 'array';
26
    const TYPE_NULL = 'null';
27
    const TYPE_UNKNWON = 'unknown';
28
    const TYPE_FLUENT_INTERFACE = 'fluent';
29
30
    /**
31
     * Resolves type of given string
32
     *
33
     * @param $string
34
     * @return string
35
     */
36
    public function resolve($string)
37
    {
38
        $string = $cased = trim($string, ";\t\n\r\0\x0B");
39
        $string = strtolower($string);
40
41
        if(strlen($string) == 0) {
42
            return self::TYPE_VOID;
43
        }
44
45
        if(preg_match('!^\d+$!', $string)) {
46
            return self::TYPE_INTEGER;
47
        }
48
49
        if(preg_match('!^\d+\.\d+$!', $string)) {
50
            return self::TYPE_FLOAT;
51
        }
52
53
        if('null' == $string) {
54
            return self::TYPE_NULL;
55
        }
56
57
        if(preg_match('!(^\[|^array\()!', $string)) {
58
            return self::TYPE_ARRAY;
59
        }
60
61
        if(preg_match('!(^\[|^array\()!', $string)) {
62
            return self::TYPE_ARRAY;
63
        }
64
65
        if(preg_match('!^new\s*(.*)!', $string, $matches)) {
66
            return trim(preg_replace('!^(new\s+)!', '', $cased));
67
        }
68
69
        if(preg_match('!^\$this$!', $string, $matches)) {
70
            return self::TYPE_FLUENT_INTERFACE;
71
        }
72
73
        if(preg_match('!^["\']!', $string, $matches)) {
74
            return self::TYPE_STRING;
75
        }
76
77
        return self::TYPE_UNKNWON;
78
    }
79
80
    /**
81
     * Check if type is native (not for PHP, but for this resolver)
82
     *
83
     * @param $type
84
     * @return bool
85
     */
86
    public function isNative($type) {
87
        return in_array(strtolower(trim($type)), array(
88
            self::TYPE_VOID,
89
            self::TYPE_STRING,
90
            self::TYPE_INTEGER,
91
            self::TYPE_FLOAT,
92
            self::TYPE_ARRAY,
93
            self::TYPE_NULL,
94
            self::TYPE_UNKNWON,
95
            self::TYPE_FLUENT_INTERFACE,
96
        ));
97
    }
98
99
100
}