-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
170 additions
and
0 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
15 changes: 15 additions & 0 deletions
15
src/Rule/Assertion/Relation/CanOnlyDepend/CatchBlockRule.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,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Assertion\Relation\CanOnlyDepend; | ||
|
||
use PHPat\Rule\Extractor\Relation\CatchBlockExtractor; | ||
use PhpParser\Node; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\Catch_> | ||
*/ | ||
final class CatchBlockRule extends CanOnlyDepend implements Rule | ||
{ | ||
use CatchBlockExtractor; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Rule/Assertion/Relation/ShouldNotDepend/CatchBlockRule.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,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Assertion\Relation\ShouldNotDepend; | ||
|
||
use PHPat\Rule\Extractor\Relation\CatchBlockExtractor; | ||
use PhpParser\Node; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\Catch_> | ||
*/ | ||
final class CatchBlockRule extends ShouldNotDepend implements Rule | ||
{ | ||
use CatchBlockExtractor; | ||
} |
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,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Extractor\Relation; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Catch_; | ||
use PHPStan\Analyser\Scope; | ||
|
||
trait CatchBlockExtractor | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Catch_::class; | ||
} | ||
|
||
/** | ||
* @param Catch_ $node | ||
* @return array<class-string> | ||
*/ | ||
protected function extractNodeClassNames(Node $node, Scope $scope): array | ||
{ | ||
return namesToClassStrings($node->types); | ||
} | ||
} |
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,49 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\PHPat\unit\rules\CanOnlyDepend; | ||
|
||
use PHPat\Configuration; | ||
use PHPat\Rule\Assertion\Relation\CanOnlyDepend\CanOnlyDepend; | ||
use PHPat\Rule\Assertion\Relation\CanOnlyDepend\CatchBlockRule; | ||
use PHPat\Selector\Classname; | ||
use PHPat\Statement\Builder\StatementBuilderFactory; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPStan\Type\FileTypeMapper; | ||
use Tests\PHPat\fixtures\FixtureClass; | ||
use Tests\PHPat\fixtures\Simple\SimpleException; | ||
use Tests\PHPat\unit\FakeTestParser; | ||
|
||
/** | ||
* @extends RuleTestCase<CatchBlockRule> | ||
* @internal | ||
* @coversNothing | ||
*/ | ||
class CatchBlockTest extends RuleTestCase | ||
{ | ||
public const RULE_NAME = 'test_FixtureClassCanOnlyDependSimpleException'; | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse(['tests/fixtures/FixtureClass.php'], [ | ||
[sprintf('%s should not depend on %s', FixtureClass::class, SimpleException::class), 97], | ||
]); | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$testParser = FakeTestParser::create( | ||
self::RULE_NAME, | ||
CanOnlyDepend::class, | ||
[new Classname(FixtureClass::class, false)], | ||
[new Classname(\Exception::class, false)] | ||
); | ||
|
||
return new CatchBlockRule( | ||
new StatementBuilderFactory($testParser), | ||
new Configuration(false, true, false), | ||
$this->createReflectionProvider(), | ||
self::getContainer()->getByType(FileTypeMapper::class) | ||
); | ||
} | ||
} |
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,49 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\PHPat\unit\rules\ShouldNotDepend; | ||
|
||
use PHPat\Configuration; | ||
use PHPat\Rule\Assertion\Relation\ShouldNotDepend\CatchBlockRule; | ||
use PHPat\Rule\Assertion\Relation\ShouldNotDepend\ShouldNotDepend; | ||
use PHPat\Selector\Classname; | ||
use PHPat\Statement\Builder\StatementBuilderFactory; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPStan\Type\FileTypeMapper; | ||
use Tests\PHPat\fixtures\FixtureClass; | ||
use Tests\PHPat\fixtures\Simple\SimpleException; | ||
use Tests\PHPat\unit\FakeTestParser; | ||
|
||
/** | ||
* @extends RuleTestCase<CatchBlockRule> | ||
* @internal | ||
* @coversNothing | ||
*/ | ||
class CatchBlockTest extends RuleTestCase | ||
{ | ||
public const RULE_NAME = 'test_FixtureClassShouldNotDependSimpleException'; | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse(['tests/fixtures/FixtureClass.php'], [ | ||
[sprintf('%s should not depend on %s', FixtureClass::class, SimpleException::class), 97], | ||
]); | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$testParser = FakeTestParser::create( | ||
self::RULE_NAME, | ||
ShouldNotDepend::class, | ||
[new Classname(FixtureClass::class, false)], | ||
[new Classname(SimpleException::class, false)] | ||
); | ||
|
||
return new CatchBlockRule( | ||
new StatementBuilderFactory($testParser), | ||
new Configuration(false, true, false), | ||
$this->createReflectionProvider(), | ||
self::getContainer()->getByType(FileTypeMapper::class) | ||
); | ||
} | ||
} |