IsEmpty::inspect()   A
last analyzed

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
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * Copyright (c) 2016-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/TypeChecks
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2016-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\TypeChecks;
45
46
use GanbaroDigital\MissingBits\Checks\Check;
47
use GanbaroDigital\MissingBits\TypeInterfaces\CanBeEmpty;
48
use stdClass;
49
use Traversable;
50
51
/**
52
 * do we have something that is empty?
53
 */
54
class IsEmpty implements Check
55
{
56
    /**
57
     * fluent-interface entry point
58
     *
59
     * we do not support any customisations
60
     *
61
     * @return IsEmpty
62
     */
63
    public static function using()
64
    {
65
        return new static();
66
    }
67
68
    /**
69
     * check if an item is empty
70
     *
71
     * empty means one of:
72
     * - item itself is empty
73
     * - item is a data container, and only contains empty data items
74
     *
75
     * BE AWARE that this check WILL descend down into the contents of $fieldOrVar
76
     * until it finds the first piece of non-empty data. This has the potential
77
     * to be computationally expensive.
78
     *
79
     * @param  mixed $fieldOrVar
80
     *         the item to check
81
     * @return bool
82
     *         TRUE if the item is empty
83
     *         FALSE otherwise
84
     */
85
    public static function check($fieldOrVar)
86
    {
87
        // general case
88
        if (is_null($fieldOrVar)) {
89
            return true;
90
        }
91
92
        // type-specific checks
93
        if (is_array($fieldOrVar)) {
94
            return self::checkArray($fieldOrVar);
95
        }
96
        if ($fieldOrVar instanceof Traversable || $fieldOrVar instanceof stdClass) {
97
            return self::checkTraversable($fieldOrVar);
0 ignored issues
show
Documentation introduced by
$fieldOrVar is of type object<Traversable>|object<stdClass>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
        }
99
        if ($fieldOrVar instanceof CanBeEmpty) {
100
            return $fieldOrVar->isEmpty();
101
        }
102
103
        if (is_string($fieldOrVar)) {
104
            return self::checkString($fieldOrVar);
105
        }
106
107
        // if we get here, we've run out of ways to check
108
        return false;
109
    }
110
111
    /**
112
     * check if an item is empty
113
     *
114
     * empty means one of:
115
     * - item itself has no content
116
     * - item is a data container, and only contains empty data items
117
     *
118
     * BE AWARE that this check WILL descend down into the contents of $fieldOrVar
119
     * until it finds the first piece of non-empty data. This has the potential
120
     * to be computationally expensive.
121
     *
122
     * @param  array $fieldOrVar
123
     *         the item to check
124
     * @return bool
125
     *         TRUE if the item is empty
126
     *         FALSE otherwise
127
     */
128
    private static function checkArray($fieldOrVar)
129
    {
130
        if (count($fieldOrVar) === 0) {
131
            return true;
132
        }
133
134
        return self::checkTraversable($fieldOrVar);
135
    }
136
137
    /**
138
     * check if an item is empty
139
     *
140
     * empty means one of:
141
     * - the string has zero length
142
     * - the string only contains whitespace
143
     *
144
     * @param  string $fieldOrVar
145
     *         the item to check
146
     * @return bool
147
     *         TRUE if the item is empty
148
     *         FALSE otherwise
149
     */
150
    private static function checkString($fieldOrVar)
151
    {
152
        if (trim((string)$fieldOrVar) === '') {
153
            return true;
154
        }
155
156
        return false;
157
    }
158
159
    /**
160
     * check if an item is empty
161
     *
162
     * empty means one of:
163
     * - item itself has no content
164
     * - item is a data container, and only contains empty data items
165
     *
166
     * BE AWARE that this check WILL descend down into the contents of $fieldOrVar
167
     * until it finds the first piece of non-empty data. This has the potential
168
     * to be computationally expensive.
169
     *
170
     * @param  array $fieldOrVar
171
     *         the item to check
172
     * @return bool
173
     *         TRUE if the item is empty
174
     *         FALSE otherwise
175
     */
176
    private static function checkTraversable($fieldOrVar)
177
    {
178
        foreach ($fieldOrVar as $value) {
179
            if (!self::check($value)) {
180
                return false;
181
            }
182
        }
183
184
        // if we get here, item's contents are entirely empty
185
        return true;
186
    }
187
188
    /**
189
     * check if an item is empty
190
     *
191
     * empty means one of:
192
     * - item itself is empty
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 $fieldOrVar
196
     * until it finds the first piece of non-empty data. This has the potential
197
     * to be computationally expensive.
198
     *
199
     * @param  mixed $fieldOrVar
200
     *         the item to check
201
     * @return bool
202
     *         TRUE if the item is empty
203
     *         FALSE otherwise
204
     */
205
    public function inspect($fieldOrVar)
206
    {
207
        return static::check($fieldOrVar);
208
    }
209
}
210