Completed
Pull Request — master (#162)
by personal
10:09 queued 05:21
created

TypeResolver::resolve()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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
    const TYPE_ANONYMOUS_CLASS = 'anonymous@class';
30
31
    /**
32
     * Resolves type of given string
33
     *
34
     * @param $string
35
     * @return string
36
     */
37
    public function resolve($string)
38
    {
39
        $string = $cased = trim($string, ";\t\n\r\0\x0B");
40
        $string = strtolower($string);
41
42
        if(strlen($string) == 0) {
43
            return self::TYPE_VOID;
44
        }
45
46
        if(preg_match('!^\d+$!', $string)) {
47
            return self::TYPE_INTEGER;
48
        }
49
50
        if(preg_match('!^\d+\.\d+$!', $string)) {
51
            return self::TYPE_FLOAT;
52
        }
53
54
        if('null' == $string) {
55
            return self::TYPE_NULL;
56
        }
57
58
        if(preg_match('!(^\[|^array\()!', $string)) {
59
            return self::TYPE_ARRAY;
60
        }
61
62
        if(preg_match('!^new\s+class\s+!', $string, $matches)) {
63
            return self::TYPE_ANONYMOUS_CLASS;
64
        }
65
66
        if(preg_match('!^(new\s+)(.*?)([\(;].*|$)!', $cased, $matches)) {
67
            return $matches[2];
68
        }
69
70
        if(preg_match('!^\$this$!', $string, $matches)) {
71
            return self::TYPE_FLUENT_INTERFACE;
72
        }
73
74
        if(preg_match('!^["\']!', $string, $matches)) {
75
            return self::TYPE_STRING;
76
        }
77
78
        return self::TYPE_UNKNWON;
79
    }
80
81
    /**
82
     * Check if type is native (not for PHP, but for this resolver)
83
     *
84
     * @param $type
85
     * @return bool
86
     */
87
    public function isNative($type) {
88
        return in_array(strtolower(trim($type)), array(
89
            self::TYPE_VOID,
90
            self::TYPE_STRING,
91
            self::TYPE_INTEGER,
92
            self::TYPE_FLOAT,
93
            self::TYPE_ARRAY,
94
            self::TYPE_NULL,
95
            self::TYPE_UNKNWON,
96
            self::TYPE_FLUENT_INTERFACE,
97
        ));
98
    }
99
100
101
}