• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / PHPUnit-Polyfills / 16579531037

28 Jul 2025 08:25PM UTC coverage: 63.368% (-32.5%) from 95.846%
16579531037

push

github

jrfnl
Merge branch '1.x' into 2.x

365 of 576 relevant lines covered (63.37%)

17.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Polyfills/AssertIsType.php
1
<?php
2

3
namespace Yoast\PHPUnitPolyfills\Polyfills;
4

5
use Traversable;
6

7
/**
8
 * Polyfill the Assert::assertIsBool(), Assert::assertIsNotBool() etc methods, which
9
 * replace the Assert::assertInternalType() and Assert::assertNotInternalType() methods.
10
 *
11
 * Introduced in PHPUnit 7.5.0.
12
 * The Assert::assertInternalType() and Assert::assertNotInternalType() methods were
13
 * deprecated in PHPUnit 7.5.0 and removed in PHPUnit 9.0.0.
14
 *
15
 * @link https://github.com/sebastianbergmann/phpunit/issues/3368
16
 * @link https://github.com/sebastianbergmann/phpunit/issues/3369
17
 *
18
 * @since 0.1.0
19
 */
20
trait AssertIsType {
21

22
        /**
23
         * Asserts that a variable is of type array.
24
         *
25
         * @param mixed  $actual  The value to test.
26
         * @param string $message Optional failure message to display.
27
         *
28
         * @return void
29
         */
30
        final public static function assertIsArray( $actual, $message = '' ) {
×
31
                static::assertInternalType( 'array', $actual, $message );
×
32
        }
33

34
        /**
35
         * Asserts that a variable is of type bool.
36
         *
37
         * @param mixed  $actual  The value to test.
38
         * @param string $message Optional failure message to display.
39
         *
40
         * @return void
41
         */
42
        final public static function assertIsBool( $actual, $message = '' ) {
×
43
                static::assertInternalType( 'bool', $actual, $message );
×
44
        }
45

46
        /**
47
         * Asserts that a variable is of type float.
48
         *
49
         * @param mixed  $actual  The value to test.
50
         * @param string $message Optional failure message to display.
51
         *
52
         * @return void
53
         */
54
        final public static function assertIsFloat( $actual, $message = '' ) {
×
55
                static::assertInternalType( 'float', $actual, $message );
×
56
        }
57

58
        /**
59
         * Asserts that a variable is of type int.
60
         *
61
         * @param mixed  $actual  The value to test.
62
         * @param string $message Optional failure message to display.
63
         *
64
         * @return void
65
         */
66
        final public static function assertIsInt( $actual, $message = '' ) {
×
67
                static::assertInternalType( 'int', $actual, $message );
×
68
        }
69

70
        /**
71
         * Asserts that a variable is of type numeric.
72
         *
73
         * @param mixed  $actual  The value to test.
74
         * @param string $message Optional failure message to display.
75
         *
76
         * @return void
77
         */
78
        final public static function assertIsNumeric( $actual, $message = '' ) {
×
79
                static::assertInternalType( 'numeric', $actual, $message );
×
80
        }
81

82
        /**
83
         * Asserts that a variable is of type object.
84
         *
85
         * @param mixed  $actual  The value to test.
86
         * @param string $message Optional failure message to display.
87
         *
88
         * @return void
89
         */
90
        final public static function assertIsObject( $actual, $message = '' ) {
×
91
                static::assertInternalType( 'object', $actual, $message );
×
92
        }
93

94
        /**
95
         * Asserts that a variable is of type resource.
96
         *
97
         * @param mixed  $actual  The value to test.
98
         * @param string $message Optional failure message to display.
99
         *
100
         * @return void
101
         */
102
        final public static function assertIsResource( $actual, $message = '' ) {
×
103
                static::assertInternalType( 'resource', $actual, $message );
×
104
        }
105

106
        /**
107
         * Asserts that a variable is of type string.
108
         *
109
         * @param mixed  $actual  The value to test.
110
         * @param string $message Optional failure message to display.
111
         *
112
         * @return void
113
         */
114
        final public static function assertIsString( $actual, $message = '' ) {
×
115
                static::assertInternalType( 'string', $actual, $message );
×
116
        }
117

118
        /**
119
         * Asserts that a variable is of type scalar.
120
         *
121
         * @param mixed  $actual  The value to test.
122
         * @param string $message Optional failure message to display.
123
         *
124
         * @return void
125
         */
126
        final public static function assertIsScalar( $actual, $message = '' ) {
×
127
                static::assertInternalType( 'scalar', $actual, $message );
×
128
        }
129

130
        /**
131
         * Asserts that a variable is of type callable.
132
         *
133
         * @param mixed  $actual  The value to test.
134
         * @param string $message Optional failure message to display.
135
         *
136
         * @return void
137
         */
138
        final public static function assertIsCallable( $actual, $message = '' ) {
×
139
                static::assertInternalType( 'callable', $actual, $message );
×
140
        }
141

142
        /**
143
         * Asserts that a variable is of type iterable.
144
         *
145
         * {@internal Support for `iterable` was only added to the `Assert::assertInternalType()` method
146
         * in PHPUnit 7.1.0, so this polyfill can't use a direct fall-through to that functionality
147
         * until the minimum supported PHPUnit version of this library would be PHPUnit 7.1.0.}
148
         *
149
         * @link https://github.com/sebastianbergmann/phpunit/pull/3035 PR which added support for `is_iterable`
150
         *                                                              to `Assert::assertInternalType()`.
151
         *
152
         * @param mixed  $actual  The value to test.
153
         * @param string $message Optional failure message to display.
154
         *
155
         * @return void
156
         */
157
        final public static function assertIsIterable( $actual, $message = '' ) {
×
158
                if ( \function_exists( 'is_iterable' ) === true ) {
×
159
                        // PHP >= 7.1.
160
                        // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.is_iterableFound
161
                        static::assertTrue( \is_iterable( $actual ), $message );
×
162
                }
163
                else {
164
                        // PHP < 7.1.
165
                        $result = ( \is_array( $actual ) || $actual instanceof Traversable );
×
166
                        static::assertTrue( $result, $message );
×
167
                }
168
        }
169

170
        /**
171
         * Asserts that a variable is not of type array.
172
         *
173
         * @param mixed  $actual  The value to test.
174
         * @param string $message Optional failure message to display.
175
         *
176
         * @return void
177
         */
178
        final public static function assertIsNotArray( $actual, $message = '' ) {
×
179
                static::assertNotInternalType( 'array', $actual, $message );
×
180
        }
181

182
        /**
183
         * Asserts that a variable is not of type bool.
184
         *
185
         * @param mixed  $actual  The value to test.
186
         * @param string $message Optional failure message to display.
187
         *
188
         * @return void
189
         */
190
        final public static function assertIsNotBool( $actual, $message = '' ) {
×
191
                static::assertNotInternalType( 'bool', $actual, $message );
×
192
        }
193

194
        /**
195
         * Asserts that a variable is not of type float.
196
         *
197
         * @param mixed  $actual  The value to test.
198
         * @param string $message Optional failure message to display.
199
         *
200
         * @return void
201
         */
202
        final public static function assertIsNotFloat( $actual, $message = '' ) {
×
203
                static::assertNotInternalType( 'float', $actual, $message );
×
204
        }
205

206
        /**
207
         * Asserts that a variable is not of type int.
208
         *
209
         * @param mixed  $actual  The value to test.
210
         * @param string $message Optional failure message to display.
211
         *
212
         * @return void
213
         */
214
        final public static function assertIsNotInt( $actual, $message = '' ) {
×
215
                static::assertNotInternalType( 'int', $actual, $message );
×
216
        }
217

218
        /**
219
         * Asserts that a variable is not of type numeric.
220
         *
221
         * @param mixed  $actual  The value to test.
222
         * @param string $message Optional failure message to display.
223
         *
224
         * @return void
225
         */
226
        final public static function assertIsNotNumeric( $actual, $message = '' ) {
×
227
                static::assertNotInternalType( 'numeric', $actual, $message );
×
228
        }
229

230
        /**
231
         * Asserts that a variable is not of type object.
232
         *
233
         * @param mixed  $actual  The value to test.
234
         * @param string $message Optional failure message to display.
235
         *
236
         * @return void
237
         */
238
        final public static function assertIsNotObject( $actual, $message = '' ) {
×
239
                static::assertNotInternalType( 'object', $actual, $message );
×
240
        }
241

242
        /**
243
         * Asserts that a variable is not of type resource.
244
         *
245
         * @param mixed  $actual  The value to test.
246
         * @param string $message Optional failure message to display.
247
         *
248
         * @return void
249
         */
250
        final public static function assertIsNotResource( $actual, $message = '' ) {
×
251
                static::assertNotInternalType( 'resource', $actual, $message );
×
252
        }
253

254
        /**
255
         * Asserts that a variable is not of type string.
256
         *
257
         * @param mixed  $actual  The value to test.
258
         * @param string $message Optional failure message to display.
259
         *
260
         * @return void
261
         */
262
        final public static function assertIsNotString( $actual, $message = '' ) {
×
263
                static::assertNotInternalType( 'string', $actual, $message );
×
264
        }
265

266
        /**
267
         * Asserts that a variable is not of type scalar.
268
         *
269
         * @param mixed  $actual  The value to test.
270
         * @param string $message Optional failure message to display.
271
         *
272
         * @return void
273
         */
274
        final public static function assertIsNotScalar( $actual, $message = '' ) {
×
275
                static::assertNotInternalType( 'scalar', $actual, $message );
×
276
        }
277

278
        /**
279
         * Asserts that a variable is not of type callable.
280
         *
281
         * @param mixed  $actual  The value to test.
282
         * @param string $message Optional failure message to display.
283
         *
284
         * @return void
285
         */
286
        final public static function assertIsNotCallable( $actual, $message = '' ) {
×
287
                static::assertNotInternalType( 'callable', $actual, $message );
×
288
        }
289

290
        /**
291
         * Asserts that a variable is not of type iterable.
292
         *
293
         * {@internal Support for `iterable` was only added to the `Assert::assertNotInternalType()` method
294
         * in PHPUnit 7.1.0, so this polyfill can't use a direct fall-through to that functionality
295
         * until the minimum supported PHPUnit version of this library would be PHPUnit 7.1.0.}
296
         *
297
         * @link https://github.com/sebastianbergmann/phpunit/pull/3035 PR which added support for `is_iterable`
298
         *                                                              to `Assert::assertNotInternalType()`.
299
         *
300
         * @param mixed  $actual  The value to test.
301
         * @param string $message Optional failure message to display.
302
         *
303
         * @return void
304
         */
305
        final public static function assertIsNotIterable( $actual, $message = '' ) {
×
306
                if ( \function_exists( 'is_iterable' ) === true ) {
×
307
                        // PHP >= 7.1.
308
                        // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.is_iterableFound
309
                        static::assertFalse( \is_iterable( $actual ), $message );
×
310
                }
311
                else {
312
                        // PHP < 7.1.
313
                        $result = ( \is_array( $actual ) || $actual instanceof Traversable );
×
314
                        static::assertFalse( $result, $message );
×
315
                }
316
        }
317
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc