Skip to content

Commit

Permalink
Merge pull request #5 from olivernybroe/types
Browse files Browse the repository at this point in the history
Add type checking via phpstan
  • Loading branch information
freekmurze authored Jun 6, 2022
2 parents 52df0e4 + 8f0a4df commit 2e4250a
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
/art export-ignore
/docs export-ignore
/UPGRADING.md export-ignore
/types export-ignore
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"require-dev": {
"pestphp/pest": "^1.20",
"phpstan/phpstan": "^1.4",
"spatie/ray": "^1.28"
},
"autoload": {
Expand Down
8 changes: 8 additions & 0 deletions src/Invader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

use ReflectionClass;

/**
* @template T of object
* @mixin T
*/
class Invader
{
/** @var T */
public object $obj;
public ReflectionClass $reflected;

/**
* @param T $obj
*/
public function __construct(object $obj)
{
$this->obj = $obj;
Expand Down
8 changes: 7 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
use Spatie\Invade\Invader;

if (! function_exists('invade')) {
function invade(object $object)
/**
* @template T of object
*
* @param T $object
* @return Invader<T>
*/
function invade(object $object): Invader
{
return new Invader($object);
}
Expand Down
16 changes: 16 additions & 0 deletions types/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class Example
{
public function __construct(
private string $mySecret,
protected int $myShared,
public string $myExposed,
) {
}

public function getMySecret(): string
{
return $this->mySecret;
}
}
4 changes: 4 additions & 0 deletions types/phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
paths:
- .
level: max
12 changes: 12 additions & 0 deletions types/src/Invader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Spatie\Invade\Invader;
use function PHPStan\Testing\assertType;

$example = new Example("password", 42, "Pestival");
$invaded = new Invader($example);

assertType(Invader::class . '<' . Example::class . '>', $invaded);
assertType('string', $invaded->myExposed);
assertType('int', $invaded->myShared);
assertType('string', $invaded->mySecret);
8 changes: 8 additions & 0 deletions types/src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Spatie\Invade\Invader;
use function PHPStan\Testing\assertType;

$example = new Example("password", 42, "Pestival");

assertType(Invader::class . '<' . Example::class . '>', invade($example));

0 comments on commit 2e4250a

Please sign in to comment.