Completed
Pull Request — master (#221)
by personal
04:09
created

TypeResolver   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 74
rs 10
c 2
b 0
f 1
wmc 14
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C resolve() 0 45 11
A isScalar() 0 4 1
A isObject() 0 4 2
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\Parser\Helper;
11
use Hal\Component\Token\Token;
12
13
14
/**
15
 * Type resolver
16
 *
17
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
18
 */
19
class TypeResolver
20
{
21
22
    /**
23
     * Resolves type of given string
24
     *
25
     * @param $token
26
     * @return string
27
     */
28
    public function resolve($token)
29
    {
30
31
        if(strlen($token) == 0) {
32
            return Token::T_VALUE_VOID;
33
        }
34
35
        if (in_array($token, array(Token::T_VALUE_BOOLEAN, Token::T_VALUE_FLOAT, Token::T_VALUE_STRING, Token::T_VALUE_INTEGER, Token::T_VALUE_NULL))) {
36
            return $token;
37
        }
38
39
        if(preg_match('!^\d+$!', $token)) {
40
            return Token::T_VALUE_INTEGER;
41
        }
42
43
        if(preg_match('!^\d+\.\d+$!', $token)) {
44
            return Token::T_VALUE_FLOAT;
45
        }
46
47
        if('null' == $token) {
48
            return Token::T_VALUE_NULL;
49
        }
50
51
        if(preg_match('!^\$\w+$!', $token)) {
52
            return Token::T_VAR;
53
        }
54
55
        if(preg_match('!(^\[|^array\()!', $token)) {
56
            return Token::T_VALUE_ARRAY;
57
        }
58
59
        if(preg_match('!^(true|false)!', $token)) {
60
            return Token::T_VALUE_BOOLEAN;
61
        }
62
63
        if(preg_match('!^function!', $token)) {
64
            return Token::T_FUNCTION;
65
        }
66
67
        if(preg_match('!^["\']!', $token)) {
68
            return Token::T_VALUE_STRING;
69
        }
70
71
        return Token::T_VALUE_UNKNWON;
72
    }
73
74
    /**
75
     * @param $value
76
     * @return bool
77
     */
78
    public function isScalar($value)
79
    {
80
        return in_array($this->resolve($value), array(Token::T_VALUE_STRING, Token::T_VALUE_BOOLEAN, Token::T_VALUE_INTEGER, Token::T_VALUE_FLOAT));
81
    }
82
83
    /**
84
     * @param $value
85
     * @return bool
86
     */
87
    public function isObject($value)
88
    {
89
        return !$this->isScalar($value) &&Token::T_VALUE_ARRAY !== $this->resolve($value);
90
    }
91
92
}