Completed
Push — develop ( ec34e2...da7534 )
by Stuart
02:17
created

IsEmpty::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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   Reflection/Checks
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://code.ganbarodigital.com/php-reflection
42
 */
43
44
namespace GanbaroDigital\Reflection\Checks;
45
46
use GanbaroDigital\Reflection\Maps\MapTypeToMethod;
47
48
class IsEmpty
49
{
50
    /**
51
     * check if an item is empty
52
     *
53
     * empty means one of:
54
     * - item itself is empty
55
     * - item is a data container, and only contains empty data items
56
     *
57
     * BE AWARE that this check WILL descend down into the contents of $item
58
     * until it finds the first piece of non-empty data. This has the potential
59
     * to be computationally expensive.
60
     *
61
     * @param  mixed $item
62
     *         the item to check
63
     * @return boolean
64
     *         TRUE if the item is empty
65
     *         FALSE otherwise
66
     */
67
    public function __invoke($item)
68
    {
69
        return self::check($item);
70
    }
71
72
    /**
73
     * check if an item is empty
74
     *
75
     * empty means one of:
76
     * - item itself is empty
77
     * - item is a data container, and only contains empty data items
78
     *
79
     * BE AWARE that this check WILL descend down into the contents of $item
80
     * until it finds the first piece of non-empty data. This has the potential
81
     * to be computationally expensive.
82
     *
83
     * @param  mixed $item
84
     *         the item to check
85
     * @return boolean
86
     *         TRUE if the item is empty
87
     *         FALSE otherwise
88
     */
89
    public static function check($item)
90
    {
91
        $method = MapTypeToMethod::using($item, self::$dispatchMap);
92
        return self::$method($item);
93
    }
94
95
    /**
96
     * a list of the data types that we support, and how to process each
97
     * supported data type
98
     *
99
     * @var array
100
     */
101
    private static $dispatchMap = [
102
        'Array' => 'checkArray',
103
        'NULL' => 'checkNull',
104
        'String' => 'checkString',
105
        'Traversable' => 'checkTraversable'
106
    ];
107
108
    /**
109
     * called when we have a data type that we do not know how to support
110
     *
111
     * @param  mixed $item
112
     *         the unsupported data
113
     * @return boolean
114
     *         always FALSE
115
     */
116
    private static function nothingMatchesTheInputType($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...
117
    {
118
        // we don't know how to reason about this data type
119
        //
120
        // we assume that a false negative will do less harm than
121
        // a false positive might
122
        return false;
123
    }
124
125
    /**
126
     * check if an item is empty
127
     *
128
     * empty means one of:
129
     * - item itself has no content
130
     * - item is a data container, and only contains empty data items
131
     *
132
     * BE AWARE that this check WILL descend down into the contents of $item
133
     * until it finds the first piece of non-empty data. This has the potential
134
     * to be computationally expensive.
135
     *
136
     * @param  array $item
137
     *         the item to check
138
     * @return boolean
139
     *         TRUE if the item is empty
140
     *         FALSE otherwise
141
     */
142
    private static function checkArray($item)
143
    {
144
        if (count($item) === 0) {
145
            return true;
146
        }
147
148
        return self::checkTraversable($item);
149
    }
150
151
    /**
152
     * check if an item is empty
153
     *
154
     * NULL is always treated as an empty value
155
     *
156
     * @param  null $item
157
     *         the item to check
158
     * @return boolean
159
     *         always TRUE
160
     */
161
    private static function checkNull($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...
162
    {
163
        return true;
164
    }
165
166
    /**
167
     * check if an item is empty
168
     *
169
     * empty means one of:
170
     * - the string has zero length
171
     * - the string only contains whitespace
172
     *
173
     * @param  string $item
174
     *         the item to check
175
     * @return boolean
176
     *         TRUE if the item is empty
177
     *         FALSE otherwise
178
     */
179
    private static function checkString($item)
180
    {
181
        if (trim((string)$item) === '') {
182
            return true;
183
        }
184
185
        return false;
186
    }
187
188
    /**
189
     * check if an item is empty
190
     *
191
     * empty means one of:
192
     * - item itself has no content
193
     * - item is a data container, and only contains empty data items
194
     *
195
     * BE AWARE that this check WILL descend down into the contents of $item
196
     * until it finds the first piece of non-empty data. This has the potential
197
     * to be computationally expensive.
198
     *
199
     * @param  array $item
200
     *         the item to check
201
     * @return boolean
202
     *         TRUE if the item is empty
203
     *         FALSE otherwise
204
     */
205
    private static function checkTraversable($item)
206
    {
207
        foreach ($item as $value) {
208
            if (!self::check($value)) {
209
                return false;
210
            }
211
        }
212
213
        // if we get here, item's contents are entirely empty
214
        return true;
215
    }
216
}
217