Skip to content

Commit

Permalink
isBool
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Mar 30, 2024
1 parent 1d30315 commit 91d345e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ public function asInt(): int

return $this->variable;
}

/**
* Asserts and narrow down the type to boolean.
*/
public function isBool(): bool
{
if (! is_bool($this->variable)) {
throw new TypeError('Variable is not a boolean.');
}

return $this->variable;
}
}
15 changes: 15 additions & 0 deletions tests/IsBoolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

test('boolean type', function () {
$variable = true;

$value = type($variable)->isBool();

expect($value)->toBeBool();
});

test('not boolean type', function () {
$variable = 7415541;

type($variable)->isBool();
})->throws(TypeError::class, 'Variable is not a boolean.');

0 comments on commit 91d345e

Please sign in to comment.