Completed
Push — develop ( da7534...183659 )
by Stuart
01:59
created

IsEmpty   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A check() 0 5 1
A nothingMatchesTheInputType() 0 8 1
A checkArray() 0 8 2
A checkNull() 0 4 1
A askObject() 0 4 1
A checkString() 0 8 2
A checkTraversable() 0 11 3
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
use GanbaroDigital\Reflection\Specifications\CheckableForEmpty;
48
49
class IsEmpty
50
{
51
    /**
52
     * check if an item is empty
53
     *
54
     * empty means one of:
55
     * - item itself is empty
56
     * - item is a data container, and only contains empty data items
57
     *
58
     * BE AWARE that this check WILL descend down into the contents of $item
59
     * until it finds the first piece of non-empty data. This has the potential
60
     * to be computationally expensive.
61
     *
62
     * @param  mixed $item
63
     *         the item to check
64
     * @return boolean
65
     *         TRUE if the item is empty
66
     *         FALSE otherwise
67
     */
68
    public function __invoke($item)
69
    {
70
        return self::check($item);
71
    }
72
73
    /**
74
     * check if an item is empty
75
     *
76
     * empty means one of:
77
     * - item itself is empty
78
     * - item is a data container, and only contains empty data items
79
     *
80
     * BE AWARE that this check WILL descend down into the contents of $item
81
     * until it finds the first piece of non-empty data. This has the potential
82
     * to be computationally expensive.
83
     *
84
     * @param  mixed $item
85
     *         the item to check
86
     * @return boolean
87
     *         TRUE if the item is empty
88
     *         FALSE otherwise
89
     */
90
    public static function check($item)
91
    {
92
        $method = MapTypeToMethod::using($item, self::$dispatchMap);
93
        return self::$method($item);
94
    }
95
96
    /**
97
     * a list of the data types that we support, and how to process each
98
     * supported data type
99
     *
100
     * @var array
101
     */
102
    private static $dispatchMap = [
103
        'Array' => 'checkArray',
104
        CheckableForEmpty::class => 'askObject',
105
        'NULL' => 'checkNull',
106
        'String' => 'checkString',
107
        'Traversable' => 'checkTraversable'
108
    ];
109
110
    /**
111
     * called when we have a data type that we do not know how to support
112
     *
113
     * @param  mixed $item
114
     *         the unsupported data
115
     * @return boolean
116
     *         always FALSE
117
     */
118
    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...
119
    {
120
        // we don't know how to reason about this data type
121
        //
122
        // we assume that a false negative will do less harm than
123
        // a false positive might
124
        return false;
125
    }
126
127
    /**
128
     * check if an item is empty
129
     *
130
     * empty means one of:
131
     * - item itself has no content
132
     * - item is a data container, and only contains empty data items
133
     *
134
     * BE AWARE that this check WILL descend down into the contents of $item
135
     * until it finds the first piece of non-empty data. This has the potential
136
     * to be computationally expensive.
137
     *
138
     * @param  array $item
139
     *         the item to check
140
     * @return boolean
141
     *         TRUE if the item is empty
142
     *         FALSE otherwise
143
     */
144
    private static function checkArray($item)
145
    {
146
        if (count($item) === 0) {
147
            return true;
148
        }
149
150
        return self::checkTraversable($item);
151
    }
152
153
    /**
154
     * check if an item is empty
155
     *
156
     * NULL is always treated as an empty value
157
     *
158
     * @param  null $item
159
     *         the item to check
160
     * @return boolean
161
     *         always TRUE
162
     */
163
    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...
164
    {
165
        return true;
166
    }
167
168
    /**
169
     * check if an item is empty
170
     *
171
     * NULL is always treated as an empty value
172
     *
173
     * @param  null $item
174
     *         the item to check
175
     * @return boolean
176
     *         always TRUE
177
     */
178
    private static function askObject($item)
179
    {
180
        return $item->isEmpty();
0 ignored issues
show
Bug introduced by
The method isEmpty cannot be called on $item (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
181
    }
182
183
    /**
184
     * check if an item is empty
185
     *
186
     * empty means one of:
187
     * - the string has zero length
188
     * - the string only contains whitespace
189
     *
190
     * @param  string $item
191
     *         the item to check
192
     * @return boolean
193
     *         TRUE if the item is empty
194
     *         FALSE otherwise
195
     */
196
    private static function checkString($item)
197
    {
198
        if (trim((string)$item) === '') {
199
            return true;
200
        }
201
202
        return false;
203
    }
204
205
    /**
206
     * check if an item is empty
207
     *
208
     * empty means one of:
209
     * - item itself has no content
210
     * - item is a data container, and only contains empty data items
211
     *
212
     * BE AWARE that this check WILL descend down into the contents of $item
213
     * until it finds the first piece of non-empty data. This has the potential
214
     * to be computationally expensive.
215
     *
216
     * @param  array $item
217
     *         the item to check
218
     * @return boolean
219
     *         TRUE if the item is empty
220
     *         FALSE otherwise
221
     */
222
    private static function checkTraversable($item)
223
    {
224
        foreach ($item as $value) {
225
            if (!self::check($value)) {
226
                return false;
227
            }
228
        }
229
230
        // if we get here, item's contents are entirely empty
231
        return true;
232
    }
233
}
234