Skip to content

Commit a5dc687

Browse files
committed
Simplify expect_type, by always throwing a TypeError.
In PHP 5, you must specify.
1 parent 365f79a commit a5dc687

File tree

3 files changed

+9
-28
lines changed

3 files changed

+9
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ Turn an `stdClass` object into an associated array recursively.
7070

7171
Validate that an argument has a specific type.
7272

73-
By default a `TypeError` is thrown in PHP 7 and an [`InvalidArgumentException`](http://php.net/invalidargumentexception)
74-
is thrown in PHP 5. You can specify a class name for any `Throwable` class.
73+
By default a `TypeError` (PHP 7) is thrown. You can specify a class name for any `Throwable` class. For PHP 5 you must
74+
specify the class name.
7575

7676
The message may contain a `%s`, which is replaced by the type of `$var`.
7777

src/type_functions.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,17 @@ function arrayify($var)
9797
*
9898
* @param mixed $var
9999
* @param string|string[] $type
100-
* @param string $throwable Class name (defaults to Throwable in PHP7, InvalidArgumentException in PHP5)
100+
* @param string $throwable Class name
101101
* @param string $message
102102
* @throws \InvalidArgumentException
103103
*/
104-
function expect_type($var, $type, $throwable = null, $message = null)
104+
function expect_type($var, $type, $throwable = 'TypeError', $message = null)
105105
{
106106
$strTypes = [];
107107
$types = (array)$type;
108108

109109
foreach ($types as $type) {
110-
if ($type === 'boolean') {
111-
$type = 'bool';
112-
}
113-
114-
$fn = 'is_' . $type;
110+
$fn = $type === 'boolean' ? 'is_bool' : 'is_' . $type;
115111
$internal = function_exists($fn);
116112

117113
if ($internal ? $fn($var) : is_a($var, $type)) {
@@ -121,10 +117,6 @@ function expect_type($var, $type, $throwable = null, $message = null)
121117
$strTypes[] = $type . ($internal ? '' : ' object');
122118
}
123119

124-
if (!isset($throwable)) {
125-
$throwable = class_exists('TypeError') ? 'TypeError' : 'InvalidArgumentException';
126-
}
127-
128120
if (!isset($message)) {
129121
$message = "Expected " . array_join_pretty(', ', ' or ', $strTypes) . ", %s given";
130122
}

tests/TypeFunctionsTest.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function testArrayifyCircularReference(\stdClass $object)
184184
{
185185
arrayify($object);
186186
}
187-
187+
188188

189189
public function expectTypeProvider()
190190
{
@@ -195,7 +195,7 @@ public function expectTypeProvider()
195195
[(object)[], 'stdClass'],
196196
[10, ['int', 'boolean']],
197197
['foo', 'int', "Expected int, string given"],
198-
['foo', ['int', 'boolean'], "Expected int or bool, string given"],
198+
['foo', ['int', 'boolean'], "Expected int or boolean, string given"],
199199
[(object)[], 'Foo', "Expected Foo object, stdClass object given"],
200200
];
201201
}
@@ -211,22 +211,11 @@ public function expectTypeProvider()
211211
public function testExpectType($var, $type, $error = false)
212212
{
213213
if ($error) {
214-
$this->expectException(class_exists('TypeError') ? 'TypeError' : 'InvalidArgumentException');
214+
$this->expectException(\InvalidArgumentException::class);
215215
$this->expectExceptionMessage($error);
216216
}
217217

218-
expect_type($var, $type);
219-
}
220-
221-
/**
222-
* @covers Jasny\expect_type
223-
*
224-
* @expectedException UnexpectedValueException
225-
* @expectedExceptionMessage Expected int, string given
226-
*/
227-
public function testExpectTypeExplicitThrowable()
228-
{
229-
expect_type('foo', 'int', \UnexpectedValueException::class);
218+
expect_type($var, $type, \InvalidArgumentException::class);
230219
}
231220

232221
/**

0 commit comments

Comments
 (0)