Skip to content

Commit

Permalink
Merge pull request #24 from enricodelazzari/main
Browse files Browse the repository at this point in the history
invade without reflection
  • Loading branch information
freekmurze authored Jul 19, 2023
2 parents 6c471ed + fcd85b0 commit 7b20a25
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 389 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: PHPStan

on:
push:
paths:
- '**.php'
- 'phpstan.neon.dist'

jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v2

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.0, 8.1]
php: [8.0, 8.1, 8.2]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,10 @@ Using `invade` you can also call private functions.
invade($myClass)->privateMethod(); // returns 'private return value'
```

### Making PHPStan understand Invade

PHPStan will report errors for every invaded private method and property as it is not aware that you can now access them. To remove these errors install the [PHPStan extension installer](https://github.com/phpstan/extension-installer) or add the invade PHPStan extension manually to your PHPStan configuration:

```yaml
includes:
- ./vendor/spatie/invade/phpstan-extension.neon
```
## Testing

```bash
composer test
vendor/bin/phpstan analyse -c types/phpstan.neon.dist
```

## Changelog
Expand Down
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,16 @@
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
"test-coverage": "vendor/bin/pest --coverage"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"phpstan": {
"includes": [
"phpstan-extension.neon"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
Empty file added phpstan-baseline.neon
Empty file.
8 changes: 0 additions & 8 deletions phpstan-extension.neon

This file was deleted.

9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- phpstan-baseline.neon

parameters:
level: 4
paths:
- src
tmpDir: build/phpstan
checkMissingIterableValueType: false
31 changes: 6 additions & 25 deletions src/Invader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,32 @@

namespace Spatie\Invade;

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;
$this->reflected = new ReflectionClass($obj);
public function __construct(
public object $obj
) {
}

public function __get(string $name): mixed
{
$property = $this->reflected->getProperty($name);

$property->setAccessible(true);

return $property->getValue($this->obj);
return (fn () => $this->{$name})->call($this->obj);
}

public function __set(string $name, mixed $value): void
{
$property = $this->reflected->getProperty($name);

$property->setAccessible(true);

$property->setValue($this->obj, $value);
(fn () => $this->{$name} = $value)->call($this->obj);
}

public function __call(string $name, array $params = []): mixed
{
$method = $this->reflected->getMethod($name);

$method->setAccessible(true);

return $method->invoke($this->obj, ...$params);
return (fn () => $this->{$name}(...$params))->call($this->obj);
}
}
35 changes: 0 additions & 35 deletions src/PHPStan/InvadeMethodsReflectionExtension.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/PHPStan/InvadePropertiesReflectionExtension.php

This file was deleted.

87 changes: 0 additions & 87 deletions src/PHPStan/InvadedMethodReflection.php

This file was deleted.

Loading

0 comments on commit 7b20a25

Please sign in to comment.