generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #781 from abenerd/feature/phpstan-rule
[1.x] Add phpstan rule to detect ray calls
- Loading branch information
Showing
11 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Spatie\Ray\Tests\PHPStan\testdata; | ||
|
||
class ClassNotContainingARayCall | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
ray('Why hello there!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php |