Skip to content

Commit

Permalink
Merge pull request #8 from WendellAdriel/test-suite-actions
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
ccmiller2018 authored Feb 5, 2023
2 parents e4ff518 + 06434c2 commit 7092aed
Show file tree
Hide file tree
Showing 19 changed files with 397 additions and 319 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms

github: [WendellAdriel]
custom: ["https://www.paypal.me/wendelladriel"]
33 changes: 33 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: run-tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.2, 8.1]
dependency-version: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none

- name: Install PHP dependencies
run: composer update --${{ matrix.dependency-version }} --no-interaction --no-progress

- name: Code Style 👨‍🏭
run: composer test:lint

- name: Pest Tests 🧫
run: composer test:unit
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,29 @@ You will need to use the `Strictus` class (`use Strictus\Strictus;`) in any clas

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

| Type | Nullable | Method |
|------------|----------|------------------------------------|
| String | No | Strictus::string($value) |
| String | Yes | Strictus::string($value, true) |
| String | Yes | Strictus::nullableString($value) |
| Integer | No | Strictus::int($value) |
| Integer | Yes | Strictus::int($value, true) |
| Integer | Yes | Strictus::nullableInt($value) |
| Float | No | Strictus::float($value) |
| Float | Yes | Strictus::float($value, true) |
| Float | Yes | Strictus::nullableFloat($value, true) |
| Boolean | No | Strictus::boolean($value) |
| Boolean | Yes | Strictus::boolean($value, true) |
| Boolean | Yes | Strictus::nullableBoolean($value) |
| Array | No | Strictus::array($value) |
| Array | Yes | Strictus::array($value, true) |
| Array | Yes | Strictus::nullableArray($value) |
| Object | No | Strictus::object($value) |
| Object | Yes | Strictus::object($value, true) |
| Object | Yes | Strictus::nullableObject($value) |
| Class Type | No | Strictus::instance($value) |
| Class Type | Yes | Strictus::instance($value, true) |
| Class Type | Yes | Strictus::nullableInstance($value) |
| Type | Nullable | Method |
|------------|----------|---------------------------------------------------|
| String | No | Strictus::string($value) |
| String | Yes | Strictus::string($value, true) |
| String | Yes | Strictus::nullableString($value) |
| Integer | No | Strictus::int($value) |
| Integer | Yes | Strictus::int($value, true) |
| Integer | Yes | Strictus::nullableInt($value) |
| Float | No | Strictus::float($value) |
| Float | Yes | Strictus::float($value, true) |
| Float | Yes | Strictus::nullableFloat($value, true) |
| Boolean | No | Strictus::boolean($value) |
| Boolean | Yes | Strictus::boolean($value, true) |
| Boolean | Yes | Strictus::nullableBoolean($value) |
| Array | No | Strictus::array($value) |
| Array | Yes | Strictus::array($value, true) |
| Array | Yes | Strictus::nullableArray($value) |
| Object | No | Strictus::object($value) |
| Object | Yes | Strictus::object($value, true) |
| Object | Yes | Strictus::nullableObject($value) |
| Class Type | No | Strictus::instance($instanceType, $value) |
| Class Type | Yes | Strictus::instance($instanceType, $value, true) |
| Class Type | Yes | Strictus::nullableInstance($instanceType, $value) |

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

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

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

## Error Handling
### Error Handling

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

Expand Down
14 changes: 8 additions & 6 deletions src/Strictus.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function nullableFloat(mixed $float): StrictusFloat
* @param bool $nullable
* @return StrictusBoolean
*/
public static function boolean(mixed $boolean, bool $nullable = false): StrictusBoolean
public static function bool(mixed $boolean, bool $nullable = false): StrictusBoolean
{
return new StrictusBoolean($boolean, $nullable);
}
Expand All @@ -85,7 +85,7 @@ public static function boolean(mixed $boolean, bool $nullable = false): Strictus
* @param mixed $boolean
* @return StrictusBoolean
*/
public static function nullableBoolean(mixed $boolean): StrictusBoolean
public static function nullableBool(mixed $boolean): StrictusBoolean
{
return new StrictusBoolean($boolean, true);
}
Expand Down Expand Up @@ -129,21 +129,23 @@ public static function nullableObject(mixed $object): StrictusObject
}

/**
* @param string $instanceType
* @param mixed $instance
* @param bool $nullable
* @return StrictusInstance
*/
public static function instance(mixed $instance, bool $nullable = false): StrictusInstance
public static function instance(string $instanceType, mixed $instance, bool $nullable = false): StrictusInstance
{
return new StrictusInstance($instance, $nullable);
return new StrictusInstance($instanceType, $instance, $nullable);
}

/**
* @param string $instanceType
* @param mixed $instance
* @return StrictusInstance
*/
public static function nullableInstance(mixed $instance): StrictusInstance
public static function nullableInstance(string $instanceType, mixed $instance): StrictusInstance
{
return new StrictusInstance($instance, true);
return new StrictusInstance($instanceType, $instance, true);
}
}
48 changes: 25 additions & 23 deletions src/Traits/StrictusTyping.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@
*/
trait StrictusTyping
{
private mixed $value;
/**
* @param mixed $value
* @param bool $nullable
*/
public function __construct(private mixed $value, private bool $nullable)
{
if ($this->nullable) {
$this->errorMessage .= ' Or Null';
}

$this->validate($value);
}

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

if ($value === null && !$this->nullable) {
throw new StrictusTypeException($this->errorMessage);
}

if (gettype($value) !== $this->instanceType) {
if ($this->nullable && $value !== null) {
throw new StrictusTypeException($this->errorMessage);
}
}
$this->validate($value);

$this->value = $value;

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

if (
gettype($value) !== $this->instanceType
|| ($this->nullable && $value !== null)
) {
throw new StrictusTypeException($this->errorMessage);
}
$this->validate($value);

$this->value = $value;
}

public function handleInstantiation(mixed $value)
/**
* @param mixed $value
* @return void
*/
private function validate(mixed $value): void
{
if (gettype($value) !== $this->instanceType) {
if (!$this->nullable) {
throw new StrictusTypeException($this->errorMessage);
} else if ($value !== null) {
throw new StrictusTypeException($this->errorMessage);
}
if ($value === null && ! $this->nullable) {
throw new StrictusTypeException($this->errorMessage);
}

if (gettype($value) !== $this->instanceType && $value !== null) {
throw new StrictusTypeException($this->errorMessage);
}
}
}
13 changes: 0 additions & 13 deletions src/Types/StrictusArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,4 @@ final class StrictusArray implements StrictusTypeInterface
private string $instanceType = 'array';

private string $errorMessage = 'Expected Array';

/**
* @param mixed $value
* @param bool $nullable
*/
public function __construct(private mixed $value, private bool $nullable)
{
if ($this->nullable) {
$this->errorMessage .= ' Or Null';
}

$this->handleInstantiation($value);
}
}
13 changes: 0 additions & 13 deletions src/Types/StrictusBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,4 @@ final class StrictusBoolean implements StrictusTypeInterface
private string $instanceType = 'boolean';

private string $errorMessage = 'Expected Boolean';

/**
* @param mixed $value
* @param bool $nullable
*/
public function __construct(private mixed $value, private bool $nullable)
{
if ($this->nullable) {
$this->errorMessage .= ' Or Null';
}

$this->handleInstantiation($value);
}
}
13 changes: 0 additions & 13 deletions src/Types/StrictusFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,4 @@ final class StrictusFloat implements StrictusTypeInterface
private string $instanceType = 'double';

private string $errorMessage = 'Expected Float';

/**
* @param mixed $value
* @param bool $nullable
*/
public function __construct(private mixed $value, private bool $nullable)
{
if ($nullable) {
$this->errorMessage .= ' Or Null';
}

$this->handleInstantiation($value);
}
}
52 changes: 13 additions & 39 deletions src/Types/StrictusInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,49 @@

use Strictus\Exceptions\StrictusTypeException;
use Strictus\Interfaces\StrictusTypeInterface;
use Strictus\Traits\StrictusTyping;

/**
* @internal
*/
final class StrictusInstance implements StrictusTypeInterface
{
private string $instanceType;
use StrictusTyping;

private string $errorMessage;

/**
* @param string $instanceType
* @param mixed $value
* @param bool $nullable
*/
public function __construct(private mixed $value, private bool $nullable)
public function __construct(private string $instanceType, private mixed $value, private bool $nullable)
{
$this->instanceType = $value::class;
$this->errorMessage = 'Expected Instance Of '.$this->value::class;
$this->errorMessage = 'Expected Instance Of '.$this->instanceType;

if ($this->nullable) {
$this->errorMessage .= ' Or Null';
}
}

/**
* @param mixed $value
* @return mixed
*/
public function __invoke(mixed $value = new StrictusUndefined()): mixed
{
if (! $value instanceof StrictusUndefined) {
if (! $value instanceof $this->instanceType) {
if ($this->nullable && $value !== null) {
throw new StrictusTypeException($this->errorMessage);
}
}

$this->value = $value;

return $this;
}

return $this->value;
$this->validate($value);
}

/**
* @param string $value
* @return mixed
*/
public function __get(string $value): mixed
{
return $this->$value;
}

/**
* @param string $name
* @param mixed $value
* @return void
*/
public function __set(string $name, mixed $value): void
private function validate(mixed $value): void
{
if ($name !== 'value') {
return;
if ($value === null && ! $this->nullable) {
throw new StrictusTypeException($this->errorMessage);
}

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

$this->value = $value;
if ($value !== null && $value::class !== $this->instanceType) {
throw new StrictusTypeException($this->errorMessage);
}
}
}
13 changes: 0 additions & 13 deletions src/Types/StrictusInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,4 @@ final class StrictusInteger implements StrictusTypeInterface
private string $instanceType = 'integer';

private string $errorMessage = 'Expected Integer';

/**
* @param mixed $value
* @param bool $nullable
*/
public function __construct(private mixed $value, private bool $nullable)
{
if ($this->nullable) {
$this->errorMessage .= ' Or Null';
}

$this->handleInstantiation($value);
}
}
13 changes: 0 additions & 13 deletions src/Types/StrictusObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,4 @@ final class StrictusObject implements StrictusTypeInterface
private string $instanceType = 'object';

private string $errorMessage = 'Expected Object';

/**
* @param mixed $value
* @param bool $nullable
*/
public function __construct(mixed $value, private bool $nullable)
{
if ($this->nullable) {
$this->errorMessage .= ' Or Null';
}

$this->handleInstantiation($value);
}
}
Loading

0 comments on commit 7092aed

Please sign in to comment.