Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added asResource() method #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ composer require std-library/type-guard
- [`asCallable()`](#ascallable)
- [`not()->null()`](#notnull)
- [`asArray()`](#asarray)
- [`asResource()`](#asresource)

### `as`

Expand Down Expand Up @@ -140,6 +141,14 @@ Asserts and narrows down the type of the given variable to an array.
$variable = type($variable)->asArray();
```

### `asResource()`

Asserts and narrows down the type of the given variable to a resource.

```php
$variable = type($variable)->asResource();
```

------

**Type Guard** is part of the [PHP's Standard Library](https://github.com/std-library) project. It was created by **[Nuno Maduro](https://twitter.com/enunomaduro)** and open-sourced under the **[MIT license](https://opensource.org/licenses/MIT)**.
16 changes: 16 additions & 0 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ public function asArray(): array
return $this->variable;
}

/**
* Asserts and narrow down the type to a resource.
*
* @phpstan-assert-if-true resource $this->variable
*
* @return resource
*/
public function asResource(): mixed
{
if (! is_resource($this->variable)) {
throw new TypeError('Variable is not a [resource].');
}

return $this->variable;
}

/**
* Asserts and narrow down the type to a callable.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/AsResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

test('resource type', function (): void {
$variable = fopen('php://memory', 'r');

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

expect($value)->toBeResource();

fclose($variable);
});

test('not resource', function (): void {
$variable = [];

type($variable)->asResource();
})->throws(TypeError::class, 'Variable is not a [resource].');
10 changes: 10 additions & 0 deletions types/AsResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use function PHPStan\Testing\assertType;

$resource = fopen('php://memory', 'r');
$variable = random_int(0, 1) !== 0 ? 'string' : $resource;
assertType('resource', $variable = type($variable)->asResource());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the typed variable as the resource can also be false

fclose($variable);
Loading