Skip to content

Commit 7092aed

Browse files
authored
Merge pull request #8 from WendellAdriel/test-suite-actions
Improvements
2 parents e4ff518 + 06434c2 commit 7092aed

19 files changed

+397
-319
lines changed

.github/FUNDING.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: [WendellAdriel]
4+
custom: ["https://www.paypal.me/wendelladriel"]

.github/workflows/tests.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: run-tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
php: [8.2, 8.1]
11+
dependency-version: [prefer-lowest, prefer-stable]
12+
13+
name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php }}
23+
tools: composer:v2
24+
coverage: none
25+
26+
- name: Install PHP dependencies
27+
run: composer update --${{ matrix.dependency-version }} --no-interaction --no-progress
28+
29+
- name: Code Style 👨‍🏭
30+
run: composer test:lint
31+
32+
- name: Pest Tests 🧫
33+
run: composer test:unit

README.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,29 @@ You will need to use the `Strictus` class (`use Strictus\Strictus;`) in any clas
3939

4040
you can then strictly type a variable with any of the below methods:
4141

42-
| Type | Nullable | Method |
43-
|------------|----------|------------------------------------|
44-
| String | No | Strictus::string($value) |
45-
| String | Yes | Strictus::string($value, true) |
46-
| String | Yes | Strictus::nullableString($value) |
47-
| Integer | No | Strictus::int($value) |
48-
| Integer | Yes | Strictus::int($value, true) |
49-
| Integer | Yes | Strictus::nullableInt($value) |
50-
| Float | No | Strictus::float($value) |
51-
| Float | Yes | Strictus::float($value, true) |
52-
| Float | Yes | Strictus::nullableFloat($value, true) |
53-
| Boolean | No | Strictus::boolean($value) |
54-
| Boolean | Yes | Strictus::boolean($value, true) |
55-
| Boolean | Yes | Strictus::nullableBoolean($value) |
56-
| Array | No | Strictus::array($value) |
57-
| Array | Yes | Strictus::array($value, true) |
58-
| Array | Yes | Strictus::nullableArray($value) |
59-
| Object | No | Strictus::object($value) |
60-
| Object | Yes | Strictus::object($value, true) |
61-
| Object | Yes | Strictus::nullableObject($value) |
62-
| Class Type | No | Strictus::instance($value) |
63-
| Class Type | Yes | Strictus::instance($value, true) |
64-
| Class Type | Yes | Strictus::nullableInstance($value) |
42+
| Type | Nullable | Method |
43+
|------------|----------|---------------------------------------------------|
44+
| String | No | Strictus::string($value) |
45+
| String | Yes | Strictus::string($value, true) |
46+
| String | Yes | Strictus::nullableString($value) |
47+
| Integer | No | Strictus::int($value) |
48+
| Integer | Yes | Strictus::int($value, true) |
49+
| Integer | Yes | Strictus::nullableInt($value) |
50+
| Float | No | Strictus::float($value) |
51+
| Float | Yes | Strictus::float($value, true) |
52+
| Float | Yes | Strictus::nullableFloat($value, true) |
53+
| Boolean | No | Strictus::boolean($value) |
54+
| Boolean | Yes | Strictus::boolean($value, true) |
55+
| Boolean | Yes | Strictus::nullableBoolean($value) |
56+
| Array | No | Strictus::array($value) |
57+
| Array | Yes | Strictus::array($value, true) |
58+
| Array | Yes | Strictus::nullableArray($value) |
59+
| Object | No | Strictus::object($value) |
60+
| Object | Yes | Strictus::object($value, true) |
61+
| Object | Yes | Strictus::nullableObject($value) |
62+
| Class Type | No | Strictus::instance($instanceType, $value) |
63+
| Class Type | Yes | Strictus::instance($instanceType, $value, true) |
64+
| Class Type | Yes | Strictus::nullableInstance($instanceType, $value) |
6565

6666
Once you have your typed variable created, you also have choices on how to use this.
6767

@@ -87,7 +87,7 @@ $myString->value = 'goodbye';
8787

8888
Both of these forms will work for any of the types.
8989

90-
## Error Handling
90+
### Error Handling
9191

9292
When you are using this package, the package will throw a `Strictus\Exceptions\StrictusTypeException` if you pass it a type that is not compatible with the intended conditions
9393

src/Strictus.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static function nullableFloat(mixed $float): StrictusFloat
7676
* @param bool $nullable
7777
* @return StrictusBoolean
7878
*/
79-
public static function boolean(mixed $boolean, bool $nullable = false): StrictusBoolean
79+
public static function bool(mixed $boolean, bool $nullable = false): StrictusBoolean
8080
{
8181
return new StrictusBoolean($boolean, $nullable);
8282
}
@@ -85,7 +85,7 @@ public static function boolean(mixed $boolean, bool $nullable = false): Strictus
8585
* @param mixed $boolean
8686
* @return StrictusBoolean
8787
*/
88-
public static function nullableBoolean(mixed $boolean): StrictusBoolean
88+
public static function nullableBool(mixed $boolean): StrictusBoolean
8989
{
9090
return new StrictusBoolean($boolean, true);
9191
}
@@ -129,21 +129,23 @@ public static function nullableObject(mixed $object): StrictusObject
129129
}
130130

131131
/**
132+
* @param string $instanceType
132133
* @param mixed $instance
133134
* @param bool $nullable
134135
* @return StrictusInstance
135136
*/
136-
public static function instance(mixed $instance, bool $nullable = false): StrictusInstance
137+
public static function instance(string $instanceType, mixed $instance, bool $nullable = false): StrictusInstance
137138
{
138-
return new StrictusInstance($instance, $nullable);
139+
return new StrictusInstance($instanceType, $instance, $nullable);
139140
}
140141

141142
/**
143+
* @param string $instanceType
142144
* @param mixed $instance
143145
* @return StrictusInstance
144146
*/
145-
public static function nullableInstance(mixed $instance): StrictusInstance
147+
public static function nullableInstance(string $instanceType, mixed $instance): StrictusInstance
146148
{
147-
return new StrictusInstance($instance, true);
149+
return new StrictusInstance($instanceType, $instance, true);
148150
}
149151
}

src/Traits/StrictusTyping.php

+25-23
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@
1212
*/
1313
trait StrictusTyping
1414
{
15-
private mixed $value;
15+
/**
16+
* @param mixed $value
17+
* @param bool $nullable
18+
*/
19+
public function __construct(private mixed $value, private bool $nullable)
20+
{
21+
if ($this->nullable) {
22+
$this->errorMessage .= ' Or Null';
23+
}
24+
25+
$this->validate($value);
26+
}
1627

1728
/**
1829
* @param mixed $value
@@ -24,15 +35,7 @@ public function __invoke(mixed $value = new StrictusUndefined()): mixed
2435
return $this->value;
2536
}
2637

27-
if ($value === null && !$this->nullable) {
28-
throw new StrictusTypeException($this->errorMessage);
29-
}
30-
31-
if (gettype($value) !== $this->instanceType) {
32-
if ($this->nullable && $value !== null) {
33-
throw new StrictusTypeException($this->errorMessage);
34-
}
35-
}
38+
$this->validate($value);
3639

3740
$this->value = $value;
3841

@@ -59,24 +62,23 @@ public function __set(string $name, mixed $value): void
5962
return;
6063
}
6164

62-
if (
63-
gettype($value) !== $this->instanceType
64-
|| ($this->nullable && $value !== null)
65-
) {
66-
throw new StrictusTypeException($this->errorMessage);
67-
}
65+
$this->validate($value);
6866

6967
$this->value = $value;
7068
}
7169

72-
public function handleInstantiation(mixed $value)
70+
/**
71+
* @param mixed $value
72+
* @return void
73+
*/
74+
private function validate(mixed $value): void
7375
{
74-
if (gettype($value) !== $this->instanceType) {
75-
if (!$this->nullable) {
76-
throw new StrictusTypeException($this->errorMessage);
77-
} else if ($value !== null) {
78-
throw new StrictusTypeException($this->errorMessage);
79-
}
76+
if ($value === null && ! $this->nullable) {
77+
throw new StrictusTypeException($this->errorMessage);
78+
}
79+
80+
if (gettype($value) !== $this->instanceType && $value !== null) {
81+
throw new StrictusTypeException($this->errorMessage);
8082
}
8183
}
8284
}

src/Types/StrictusArray.php

-13
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,4 @@ final class StrictusArray implements StrictusTypeInterface
1717
private string $instanceType = 'array';
1818

1919
private string $errorMessage = 'Expected Array';
20-
21-
/**
22-
* @param mixed $value
23-
* @param bool $nullable
24-
*/
25-
public function __construct(private mixed $value, private bool $nullable)
26-
{
27-
if ($this->nullable) {
28-
$this->errorMessage .= ' Or Null';
29-
}
30-
31-
$this->handleInstantiation($value);
32-
}
3320
}

src/Types/StrictusBoolean.php

-13
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,4 @@ final class StrictusBoolean implements StrictusTypeInterface
1717
private string $instanceType = 'boolean';
1818

1919
private string $errorMessage = 'Expected Boolean';
20-
21-
/**
22-
* @param mixed $value
23-
* @param bool $nullable
24-
*/
25-
public function __construct(private mixed $value, private bool $nullable)
26-
{
27-
if ($this->nullable) {
28-
$this->errorMessage .= ' Or Null';
29-
}
30-
31-
$this->handleInstantiation($value);
32-
}
3320
}

src/Types/StrictusFloat.php

-13
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,4 @@ final class StrictusFloat implements StrictusTypeInterface
1818
private string $instanceType = 'double';
1919

2020
private string $errorMessage = 'Expected Float';
21-
22-
/**
23-
* @param mixed $value
24-
* @param bool $nullable
25-
*/
26-
public function __construct(private mixed $value, private bool $nullable)
27-
{
28-
if ($nullable) {
29-
$this->errorMessage .= ' Or Null';
30-
}
31-
32-
$this->handleInstantiation($value);
33-
}
3421
}

src/Types/StrictusInstance.php

+13-39
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,49 @@
66

77
use Strictus\Exceptions\StrictusTypeException;
88
use Strictus\Interfaces\StrictusTypeInterface;
9+
use Strictus\Traits\StrictusTyping;
910

1011
/**
1112
* @internal
1213
*/
1314
final class StrictusInstance implements StrictusTypeInterface
1415
{
15-
private string $instanceType;
16+
use StrictusTyping;
1617

1718
private string $errorMessage;
1819

1920
/**
21+
* @param string $instanceType
2022
* @param mixed $value
2123
* @param bool $nullable
2224
*/
23-
public function __construct(private mixed $value, private bool $nullable)
25+
public function __construct(private string $instanceType, private mixed $value, private bool $nullable)
2426
{
25-
$this->instanceType = $value::class;
26-
$this->errorMessage = 'Expected Instance Of '.$this->value::class;
27+
$this->errorMessage = 'Expected Instance Of '.$this->instanceType;
2728

2829
if ($this->nullable) {
2930
$this->errorMessage .= ' Or Null';
3031
}
31-
}
32-
33-
/**
34-
* @param mixed $value
35-
* @return mixed
36-
*/
37-
public function __invoke(mixed $value = new StrictusUndefined()): mixed
38-
{
39-
if (! $value instanceof StrictusUndefined) {
40-
if (! $value instanceof $this->instanceType) {
41-
if ($this->nullable && $value !== null) {
42-
throw new StrictusTypeException($this->errorMessage);
43-
}
44-
}
45-
46-
$this->value = $value;
47-
48-
return $this;
49-
}
5032

51-
return $this->value;
33+
$this->validate($value);
5234
}
5335

5436
/**
55-
* @param string $value
56-
* @return mixed
57-
*/
58-
public function __get(string $value): mixed
59-
{
60-
return $this->$value;
61-
}
62-
63-
/**
64-
* @param string $name
6537
* @param mixed $value
6638
* @return void
6739
*/
68-
public function __set(string $name, mixed $value): void
40+
private function validate(mixed $value): void
6941
{
70-
if ($name !== 'value') {
71-
return;
42+
if ($value === null && ! $this->nullable) {
43+
throw new StrictusTypeException($this->errorMessage);
7244
}
7345

74-
if (! $value instanceof $this->instanceType) {
46+
if ($value !== null && gettype($value) !== 'object') {
7547
throw new StrictusTypeException($this->errorMessage);
7648
}
7749

78-
$this->value = $value;
50+
if ($value !== null && $value::class !== $this->instanceType) {
51+
throw new StrictusTypeException($this->errorMessage);
52+
}
7953
}
8054
}

src/Types/StrictusInteger.php

-13
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,4 @@ final class StrictusInteger implements StrictusTypeInterface
1717
private string $instanceType = 'integer';
1818

1919
private string $errorMessage = 'Expected Integer';
20-
21-
/**
22-
* @param mixed $value
23-
* @param bool $nullable
24-
*/
25-
public function __construct(private mixed $value, private bool $nullable)
26-
{
27-
if ($this->nullable) {
28-
$this->errorMessage .= ' Or Null';
29-
}
30-
31-
$this->handleInstantiation($value);
32-
}
3320
}

src/Types/StrictusObject.php

-13
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,4 @@ final class StrictusObject implements StrictusTypeInterface
1717
private string $instanceType = 'object';
1818

1919
private string $errorMessage = 'Expected Object';
20-
21-
/**
22-
* @param mixed $value
23-
* @param bool $nullable
24-
*/
25-
public function __construct(mixed $value, private bool $nullable)
26-
{
27-
if ($this->nullable) {
28-
$this->errorMessage .= ' Or Null';
29-
}
30-
31-
$this->handleInstantiation($value);
32-
}
3320
}

0 commit comments

Comments
 (0)