GetDuckTypes   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDouble() 0 7 1
A fromInteger() 0 7 1
A getDuckTypes() 0 4 1
A getObjectSpecialTypes() 0 10 2
A fromString() 0 4 1
A from() 0 11 2
A fromArray() 0 14 1
A fromObject() 0 10 1
1
<?php
2
3
/**
4
 * Copyright (c) 2015-present Ganbaro Digital Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   MissingBits/Types
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://ganbarodigital.github.io/php-the-missing-bits
42
 */
43
44
namespace GanbaroDigital\MissingBits\TypeInspectors;
45
46
use stdClass;
47
48
/**
49
 * return a practical list of data types for any value or variable
50
 */
51
class GetDuckTypes
52
{
53
    /**
54
     * which method do we want to call for a given type?
55
     *
56
     * @var array
57
     */
58
    private static $dispatchTable = [
59
        'array' => 'fromArray',
60
        'double' => 'fromDouble',
61
        'integer' => 'fromInteger',
62
        'object' => 'fromObject',
63
        'string' => 'fromString'
64
    ];
65
66
    /**
67
     * return a practical list of data types for any value or variable
68
     *
69
     * @param  mixed $item
70
     *         the item to examine
71
     * @return string[]
72
     *         the list of type(s) that this item can be
73
     */
74
    public function getDuckTypes($item)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
75
    {
76
        return self::from($item);
77
    }
78
79
    /**
80
     * return a practical list of data types for any value or variable
81
     *
82
     * @param  mixed $item
83
     *         the item to examine
84
     * @return string[]
85
     *         the list of type(s) that this item can be
86
     */
87
    public static function from($item)
88
    {
89
        $type = gettype($item);
90
        if (isset(self::$dispatchTable[$type])) {
91
            $method = self::$dispatchTable[$type];
92
            return self::$method($item);
93
        }
94
95
        // if we get here, then we just return the PHP scalar type
96
        return [ $type => $type ];
97
    }
98
99
    /**
100
     * get the list of possible types that could match an array
101
     *
102
     * @param  array $item
103
     *         the item to examine
104
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<*,string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
105
     *         a list of matching types
106
     */
107
    private static function fromArray($item)
108
    {
109
        // our return type
110
        $retval = array_merge(
111
            array_slice(GetArrayTypes::from($item), 0, -1),
112
            [
113
                'Traversable' => 'Traversable',
114
                'array' => 'array'
115
            ]
116
        );
117
118
        // all done
119
        return $retval;
120
    }
121
122
    /**
123
     * get the list of possible types that could match a floating point
124
     * number
125
     *
126
     * @param  double $item
127
     *         the item to examine
128
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
129
     *         a list of matching types
130
     */
131
    private static function fromDouble($item)
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
    {
133
        return [
134
            "double" => "double",
135
            "numeric" => "numeric",
136
        ];
137
    }
138
139
    /**
140
     * get the list of possible types that could match an integer
141
     *
142
     * @param  integer $item
143
     *         the item to examine
144
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
145
     *         a list of matching types
146
     */
147
    private static function fromInteger($item)
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        return [
150
            "integer" => "integer",
151
            "numeric" => "numeric",
152
        ];
153
    }
154
155
    /**
156
     * get the list of possible types that could match an object
157
     *
158
     * @param  object $item
159
     *         the item to examine
160
     * @return string[]
161
     *         a list of matching objects
162
     */
163
    private static function fromObject($item)
164
    {
165
        $retval = array_merge(
166
            GetObjectTypes::from($item),
167
            self::getObjectSpecialTypes($item),
168
            [ 'object' => 'object' ]
169
        );
170
171
        return $retval;
172
    }
173
174
    /**
175
     * hard-coded rules for dealing with PHP's built-in classes
176
     *
177
     * @param  object $object
178
     *         object to examine
179
     * @return array
180
     */
181
    private static function getObjectSpecialTypes($object)
182
    {
183
        $retval = [];
184
185
        if ($object instanceof stdClass) {
186
            $retval['Traversable'] = 'Traversable';
187
        }
188
189
        return $retval;
190
    }
191
192
    /**
193
     * return any data type's type name
194
     *
195
     * @param  mixed $item
196
     *         the item to examine
197
     * @return array
198
     *         the basic type of the examined item
199
     */
200
    private static function fromString($item)
201
    {
202
        return GetStringDuckTypes::from($item);
203
    }
204
}
205