Skip to content

Commit 22da598

Browse files
committed
refactor: Use only one class for providing helper methods
BREAKING CHANGE: Merge Convert and Assert to one TypeGuard class
1 parent 70470d6 commit 22da598

18 files changed

+127
-143
lines changed

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44

55
A PHP library to ensure correctness of types providing a readable interface.
66

7-
The library is helpfull to
7+
## Installation
8+
9+
```shell
10+
$ composer require plook/type-guard
11+
```
812

913
## Example
1014

1115
```php
16+
use function Plook\TypeGuard\asBool;
17+
use function Plook\TypeGuard\asDateTimeImmutable;
18+
use function Plook\TypeGuard\asFloat;
19+
use function Plook\TypeGuard\asInt;
20+
use function Plook\TypeGuard\asString;
21+
use function Plook\TypeGuard\notNull;
22+
1223
$row = $this->fetchProjectRow(123);
1324

1425
$project = new Project(
@@ -23,7 +34,7 @@ $project = new Project(
2334

2435
## Provided helper functions
2536

26-
### Type Conversion
37+
### Ensure Types
2738
* `asBool($value)` Converts input value to a boolean, but passes `null`.
2839
* `asFloat($value)` Converts input value to a float, but passes `null`.
2940
* `asInt($value)` Converts input value to a int, but passes `null`.
@@ -34,21 +45,19 @@ $project = new Project(
3445
### Assertions
3546
* `notNull($value)` Throws an exception if the value is `null` otherwise it passes the original value.
3647

37-
## Installation
38-
39-
```shell
40-
$ composer require plook/type-guard
41-
```
42-
4348
## Configuration
4449

4550
### Setting the default target time zone of `DateTimeImmutable` objects
4651
```php
47-
Convert::instance()->timeZone('Australia/Adelaide');
48-
Convert::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
52+
use Plook\TypeGuard\TypeGuard;
53+
54+
TypeGuard::instance()->timeZone('Australia/Adelaide');
55+
TypeGuard::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
4956
```
5057

5158
### Setting the default format of date time strings
5259
```php
53-
Convert::instance()->dateTimeFormat(DateTimeInterface::ATOM);
60+
use Plook\TypeGuard\TypeGuard;
61+
62+
TypeGuard::instance()->dateTimeFormat(DateTimeInterface::ATOM);
5463
```

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
"Plook\\TypeGuard\\": "src/"
2929
},
3030
"files": [
31-
"src/Assert/functions.php",
32-
"src/Convert/functions.php"
31+
"src/functions.php"
3332
]
3433
},
3534
"autoload-dev": {

src/Assert/Assert.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Assert/functions.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Assert/InvalidValue.php renamed to src/InvalidValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Plook\TypeGuard\Assert;
5+
namespace Plook\TypeGuard;
66

77
use InvalidArgumentException;
88

src/Convert/NotConvertable.php renamed to src/NotConvertable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Plook\TypeGuard\Convert;
5+
namespace Plook\TypeGuard;
66

77
use InvalidArgumentException;
88

src/Convert/Convert.php renamed to src/TypeGuard.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Plook\TypeGuard\Convert;
5+
namespace Plook\TypeGuard;
66

77
use DateTimeImmutable;
88
use DateTimeZone;
@@ -15,7 +15,7 @@
1515
use function is_scalar;
1616
use function is_string;
1717

18-
final class Convert
18+
final class TypeGuard
1919
{
2020
private DateTimeZone|null $dateTimeZone = null;
2121

@@ -116,7 +116,7 @@ public function asString(mixed $value): string|null
116116
return (string) $value;
117117
}
118118

119-
/** @return ($value is null ? null : DateTimeImmutable) */
119+
/** @return DateTimeImmutable */
120120
public function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
121121
{
122122
if ($value === null) {
@@ -175,4 +175,20 @@ public function dateTimeFormat(string|null $dateTimeFormat = null): string
175175

176176
return $this->dateTimeFormat;
177177
}
178+
179+
/**
180+
* @param ?T $value
181+
*
182+
* @return T
183+
*
184+
* @template T
185+
*/
186+
public function notNull(mixed $value): mixed
187+
{
188+
if ($value === null) {
189+
throw InvalidValue::null();
190+
}
191+
192+
return $value;
193+
}
178194
}

src/Convert/functions.php renamed to src/functions.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Plook\TypeGuard\Convert;
5+
namespace Plook\TypeGuard;
66

77
use DateTimeImmutable;
88

@@ -15,7 +15,7 @@
1515
/** @return ($value is null ? null : bool) */
1616
function asBool(mixed $value): bool|null
1717
{
18-
return Convert::instance()->asBool($value);
18+
return TypeGuard::instance()->asBool($value);
1919
}
2020
}
2121

@@ -24,7 +24,7 @@ function asBool(mixed $value): bool|null
2424
/** @return ($value is null ? null : float) */
2525
function asFloat(mixed $value): float|null
2626
{
27-
return Convert::instance()->asFloat($value);
27+
return TypeGuard::instance()->asFloat($value);
2828
}
2929
}
3030

@@ -33,7 +33,7 @@ function asFloat(mixed $value): float|null
3333
/** @return ($value is null ? null : int) */
3434
function asInt(mixed $value): int|null
3535
{
36-
return Convert::instance()->asInt($value);
36+
return TypeGuard::instance()->asInt($value);
3737
}
3838
}
3939

@@ -42,7 +42,7 @@ function asInt(mixed $value): int|null
4242
/** @return ($value is null ? null : string) */
4343
function asString(mixed $value): string|null
4444
{
45-
return Convert::instance()->asString($value);
45+
return TypeGuard::instance()->asString($value);
4646
}
4747
}
4848

@@ -51,7 +51,7 @@ function asString(mixed $value): string|null
5151
/** @return ($value is null ? null : DateTimeImmutable) */
5252
function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
5353
{
54-
return Convert::instance()->asDateTimeImmutable($value);
54+
return TypeGuard::instance()->asDateTimeImmutable($value);
5555
}
5656
}
5757

@@ -60,6 +60,21 @@ function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
6060
/** @return ($value is null ? null : string) */
6161
function asDateTimeString(mixed $value): string|null
6262
{
63-
return Convert::instance()->asDateTimeString($value);
63+
return TypeGuard::instance()->asDateTimeString($value);
64+
}
65+
}
66+
67+
if (!function_exists('\Plook\TypeGuard\notNull')) { // @codeCoverageIgnore
68+
69+
/**
70+
* @param ?T $value
71+
*
72+
* @return T
73+
*
74+
* @template T
75+
*/
76+
function notNull(mixed $value): mixed
77+
{
78+
return TypeGuard::instance()->notNull($value);
6479
}
6580
}

tests/Convert/AsBoolTest.php renamed to tests/AsBoolTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
declare(strict_types=1);
44

5-
namespace Plook\Tests\TypeGuard\Convert;
5+
namespace Plook\Tests\TypeGuard;
66

77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\Attributes\CoversFunction;
99
use PHPUnit\Framework\TestCase;
10-
use Plook\Tests\TypeGuard\StringableInt;
11-
use Plook\Tests\TypeGuard\StringableString;
12-
use Plook\TypeGuard\Convert\Convert;
13-
use Plook\TypeGuard\Convert\NotConvertable;
10+
use Plook\Tests\TypeGuard\Helper\StringableInt;
11+
use Plook\Tests\TypeGuard\Helper\StringableString;
12+
use Plook\TypeGuard\NotConvertable;
13+
use Plook\TypeGuard\TypeGuard;
1414

1515
use function basename;
16-
use function Plook\TypeGuard\Convert\asBool;
16+
use function Plook\TypeGuard\asBool;
1717
use function sprintf;
1818

19-
#[CoversClass(Convert::class)]
19+
#[CoversClass(TypeGuard::class)]
2020
#[CoversClass(NotConvertable::class)]
21-
#[CoversFunction('\Plook\TypeGuard\Convert\asBool')]
21+
#[CoversFunction('\Plook\TypeGuard\asBool')]
2222
final class AsBoolTest extends TestCase
2323
{
2424
public function testDoesNotTouchBools(): void

tests/Convert/AsDateTimeImmutableTest.php renamed to tests/AsDateTimeImmutableTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22

33
declare(strict_types=1);
44

5-
namespace Plook\Tests\TypeGuard\Convert;
5+
namespace Plook\Tests\TypeGuard;
66

77
use DateTimeImmutable;
88
use DateTimeZone;
99
use PHPUnit\Framework\Attributes\CoversClass;
1010
use PHPUnit\Framework\Attributes\CoversFunction;
1111
use PHPUnit\Framework\TestCase;
12-
use Plook\Tests\TypeGuard\StringableString;
13-
use Plook\TypeGuard\Convert\Convert;
14-
use Plook\TypeGuard\Convert\NotConvertable;
12+
use Plook\Tests\TypeGuard\Helper\StringableString;
13+
use Plook\TypeGuard\NotConvertable;
14+
use Plook\TypeGuard\TypeGuard;
1515

1616
use function basename;
17-
use function Plook\TypeGuard\Convert\asDateTimeImmutable;
17+
use function Plook\TypeGuard\asDateTimeImmutable;
1818
use function sprintf;
1919

20-
#[CoversClass(Convert::class)]
20+
#[CoversClass(TypeGuard::class)]
2121
#[CoversClass(NotConvertable::class)]
22-
#[CoversFunction('\Plook\TypeGuard\Convert\asDateTimeImmutable')]
23-
#[CoversFunction('\Plook\TypeGuard\Convert\asString')]
22+
#[CoversFunction('\Plook\TypeGuard\asDateTimeImmutable')]
23+
#[CoversFunction('\Plook\TypeGuard\asString')]
2424
final class AsDateTimeImmutableTest extends TestCase
2525
{
2626
private readonly DateTimeZone $originalTimeZone;
2727

2828
protected function setUp(): void
2929
{
30-
$this->originalTimeZone = Convert::instance()->timeZone();
30+
$this->originalTimeZone = TypeGuard::instance()->timeZone();
3131
}
3232

3333
protected function tearDown(): void
3434
{
35-
Convert::instance()->timeZone($this->originalTimeZone);
35+
TypeGuard::instance()->timeZone($this->originalTimeZone);
3636
}
3737

3838
public function testConvertsStrings(): void
@@ -53,7 +53,7 @@ public function testConvertsStringables(): void
5353

5454
public function testConvertsDateTimeImmutableWithSameTimeZone(): void
5555
{
56-
$result = asDateTimeImmutable(new DateTimeImmutable('2010-09-08T07:06:05', Convert::instance()->timeZone()));
56+
$result = asDateTimeImmutable(new DateTimeImmutable('2010-09-08T07:06:05', TypeGuard::instance()->timeZone()));
5757

5858
self::assertInstanceOf(DateTimeImmutable::class, $result);
5959
self::assertSame('2010-09-08T07:06:05+00:00', $result->format('c'));
@@ -71,7 +71,7 @@ public function testConvertsDateTimeImmutableWithDifferentTimeZone(): void
7171

7272
public function testConvertsDateTimeImmutableDefaultTimeZoneCanBeChangedByDateTimeZone(): void
7373
{
74-
Convert::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
74+
TypeGuard::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
7575

7676
$result = asDateTimeImmutable(new DateTimeImmutable('2010-09-08T07:06:05+00:00'));
7777

@@ -81,7 +81,7 @@ public function testConvertsDateTimeImmutableDefaultTimeZoneCanBeChangedByDateTi
8181

8282
public function testConvertsDateTimeImmutableDefaultTimeZoneCanBeChangedByTimeZoneName(): void
8383
{
84-
Convert::instance()->timeZone('Australia/Adelaide');
84+
TypeGuard::instance()->timeZone('Australia/Adelaide');
8585

8686
$result = asDateTimeImmutable(new DateTimeImmutable('2010-09-08T07:06:05+00:00'));
8787

0 commit comments

Comments
 (0)