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

[1.x] Add phpstan rule to detect ray calls #781

Merged
merged 5 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"illuminate/support": "6.x|^8.18|^9.0",
"nesbot/carbon": "^2.63",
"pestphp/pest": "^1.22",
"phpstan/phpstan": "^0.12.92",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5",
"spatie/phpunit-snapshot-assertions": "^4.2",
"spatie/test-time": "^1.2"
Expand Down
19 changes: 19 additions & 0 deletions docs/advanced-usage/configure-phpstan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Configure PHPStan to detect ray calls
weight: 2
---

Ray allows you to specify a custom PHPStan rule that lets you detect remaining ray calls in your application

```neon
rules:
- Spatie\Ray\PHPStan\RemainingRayCallRule
```

All remaining ray calls would then be reported by phpstan

![screenshot](/docs/ray/v1/images/phpstan-failing-result.png.png)




Binary file added docs/images/phpstan-failing-result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 0 additions & 8 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Undefined variable\\: \\$this$#"
path: */tests/*.php
-
message: "#^Function base_path not found\\.$#"
count: 1
path: src/Ray.php

-
message: "#^Function app not found\\.$#"
count: 1
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ parameters:

ignoreErrors:
- '#Unsafe usage of new static\(\)#'
-
message: '#Undefined variable\: \$this#'
path: test
32 changes: 32 additions & 0 deletions src/PHPStan/RemainingRayCallRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Spatie\Ray\PHPStan;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

class RemainingRayCallRule implements Rule
{
public function getNodeType(): string
{
return Node\Expr\FuncCall::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (! $node instanceof Node\Expr\FuncCall) {
return [];
}

if ($node->name->parts[0] !== 'ray') {
return [];
}

return [
RuleErrorBuilder::message('Remaining ray call in application code')
->build(),
];
}
}
48 changes: 48 additions & 0 deletions tests/PHPStan/RemainingRayCallRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Spatie\Ray\Tests\PHPStan;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use Spatie\Ray\PHPStan\RemainingRayCallRule;

class RemainingRayCallRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
return new RemainingRayCallRule();
}

/**
* @dataProvider failingTestCasesProvider
*/
public function testTheRuleCanDetectRayCalls(string $path, int $line): void
{
$this->analyse([$path], [
[
'Remaining ray call in application code',
$line,
],
]);
}

/**
* @dataProvider passingTestCasesProvider
*/
public function testTheRuleWillNotRaiseWhenNoRayCallIsPerformed(string $path): void
{
$this->analyse([$path], []);
}

public static function failingTestCasesProvider()
{
yield [__DIR__ . '/testdata/ClassContainingARayCall.php', 9];
yield [__DIR__ . '/testdata/PHPFileContainingARayCall.php', 3];
}

public static function passingTestCasesProvider()
{
yield [__DIR__ . '/testdata/ClassNotContainingARayCall.php'];
yield [__DIR__ . '/testdata/PHPFileNotContainingARayCall.php'];
}
}
11 changes: 11 additions & 0 deletions tests/PHPStan/testdata/ClassContainingARayCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Spatie\Ray\Tests\PHPStan\testdata;

class ClassContainingARayCall
{
public function __construct()
{
ray('I am being constructed, therefore I am');
}
}
7 changes: 7 additions & 0 deletions tests/PHPStan/testdata/ClassNotContainingARayCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Spatie\Ray\Tests\PHPStan\testdata;

class ClassNotContainingARayCall
{
}
3 changes: 3 additions & 0 deletions tests/PHPStan/testdata/PHPFileContainingARayCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

ray('Why hello there!');
1 change: 1 addition & 0 deletions tests/PHPStan/testdata/PHPFileNotContainingARayCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php