Skip to content

Commit 0307aa9

Browse files
committed
Renamed Type::castToBoolByValueOrType(). Test update.
1 parent e9a09db commit 0307aa9

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

Src/Enum/Type.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ public static function set( mixed $value, string $type ): mixed {
4646
}
4747

4848
public static function castToFalseIfNotTrue( mixed $value ): string {
49-
return in_array( $value, array( self::TRUTHY, self::FALSY ), strict: true )
50-
? $value
51-
: self::FALSY;
49+
return in_array( $value, array( self::TRUTHY, self::FALSY ), strict: true ) ? $value : self::FALSY;
5250
}
5351

5452
public static function match( mixed $value ): mixed {
@@ -59,12 +57,12 @@ public static function match( mixed $value ): mixed {
5957
};
6058
}
6159

62-
public static function toBoolByValueOrType( mixed $value, ?string $type = null ): ?string {
60+
public static function castToBoolByValueOrType( mixed $value, ?string $type = null ): ?string {
6361
return self::isBool( $type ) ? self::getBoolFrom( $value ) : null;
6462
}
6563

6664
public static function castImplicitBool( string $value, string $type ): string {
67-
return null === ( $bool = self::toBoolByValueOrType( $value, $type ) ) ? $value : $bool;
65+
return null === ( $bool = self::castToBoolByValueOrType( $value, $type ) ) ? $value : $bool;
6866
}
6967

7068
public static function toBool( mixed $value ): bool {

Tests/TypeTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function itConvertsToBooleanStringIfEitherValueOrTypeIsValidElseNull(
1818
?string $type,
1919
?string $expected = null
2020
): void {
21-
$this->assertSame( $expected, actual: Type::toBoolByValueOrType( $value, $type ) );
21+
$this->assertSame( $expected, actual: Type::castToBoolByValueOrType( $value, $type ) );
2222
}
2323

2424
public static function provideBoolValueAndType(): array {
@@ -195,23 +195,23 @@ public function itReturnsValueBasedOnValueType( mixed $value, mixed $expected ):
195195

196196
#[Test]
197197
#[DataProvider( 'provideValueToBeMatched' )]
198-
public function itConvertsToBooleanType( mixed $value, mixed $expected ): void {
199-
$this->assertSame( true === $expected, actual: Type::toBool( $value ) );
198+
public function itConvertsToBooleanType( mixed $value, mixed $expected, ?bool $converted = null ): void {
199+
$this->assertSame( $converted ?? $expected, actual: Type::toBool( $value ) );
200200
}
201201

202202
public static function provideValueToBeMatched(): array {
203203
return array(
204204
array( true, true ),
205205
array( false, false ),
206-
array( null, null ),
207-
array( array(), array() ),
208-
array( 25, 25 ),
206+
array( null, null, false ),
207+
array( array(), array(), false ),
208+
array( 25, 25, false ),
209209
array( 'true', true ),
210210
array( 'false', false ),
211-
array( 'array()', array() ),
212-
array( fn(): int => 1, fn(): int => 1 ),
213-
array( 'array', 'array' ),
214-
array( 'everything-else', 'everything-else' ),
211+
array( 'array()', array(), false ),
212+
array( fn(): int => 1, fn(): int => 1, false ),
213+
array( 'array', 'array', false ),
214+
array( 'everything-else', 'everything-else', false ),
215215
);
216216
}
217217
}

0 commit comments

Comments
 (0)