-
Notifications
You must be signed in to change notification settings - Fork 1
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 #1 from sasezaki/initial-setup
initial setups
- Loading branch information
Showing
13 changed files
with
185 additions
and
61 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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
rules: | ||
- Sfp\PHPStan\DontOperationInsideConstructor\Rules\ResourceOperationMethodCallRule | ||
- Sfp\PHPStan\DontOperationInsideConstructor\Rules\ResourceOperationFuncCallRule |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sfp\PHPStan\DontOperationInsideConstructor\Rules; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use Sfp\ResourceOperations\ResourceOperations; | ||
|
||
use function in_array; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\FuncCall> | ||
*/ | ||
final class ResourceOperationFuncCallRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\FuncCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($scope->getFunctionName() !== '__construct') { | ||
return []; | ||
} | ||
|
||
if (! $node->name instanceof Name) { | ||
return []; | ||
} | ||
|
||
$errors = []; | ||
if (in_array($node->name->toString(), ResourceOperations::getFunctions(), true)) { | ||
$errors[] = RuleErrorBuilder::message( | ||
sprintf("Don't resource operation inside constructor. function %s() is called.", $node->name->toString()) | ||
)->identifier('sfp-dont-operation.resourceOperationFuncCall')->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SfpTest\PHPStan\DontOperationInsideConstructor\Rules; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use Sfp\PHPStan\DontOperationInsideConstructor\Rules\ResourceOperationFuncCallRule; | ||
|
||
/** | ||
* @extends RuleTestCase<ResourceOperationFuncCallRule> | ||
* @covers \Sfp\PHPStan\DontOperationInsideConstructor\Rules\ResourceOperationFuncCallRule | ||
*/ | ||
class ResourceOperationFuncCallRuleTest extends RuleTestCase | ||
{ | ||
public function getRule(): Rule | ||
{ | ||
return new ResourceOperationFuncCallRule(); | ||
} | ||
|
||
public function testProcess(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/ResourceOperationFuncCall.php'], [ | ||
[ | ||
"Don't resource operation inside constructor. function fopen() is called.", | ||
13, | ||
], | ||
]); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SfpTest\PHPStan\DontOperationInsideConstructor\Rules\data; | ||
|
||
use function fopen; | ||
|
||
class ResourceOperationFuncCall | ||
{ | ||
public function __construct() | ||
{ | ||
$fp = fopen('test', 'r'); | ||
|
||
(function () { | ||
})(); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SfpTest\PHPStan\DontOperationInsideConstructor\Rules\data; | ||
|
||
use SplFileInfo; | ||
|
||
class ResourceOperationMethodCall | ||
{ | ||
public function __construct() | ||
{ | ||
$fileInfo = new SplFileInfo('test'); | ||
$fileInfo->openFile('r'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" failures="2" name="phpstan" tests="2" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd"> | ||
<testcase name="example/example.php:11"> | ||
<failure type="ERROR" message="Don't resource operation inside constructor. Method SplFileInfo::openfile() is called."/> | ||
</testcase> | ||
<testcase name="example/example.php:13"> | ||
<failure type="ERROR" message="Don't resource operation inside constructor. function fopen() is called."/> | ||
</testcase> | ||
</testsuite> |