-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Possibility to re-allow disallowed call or attribute when used in a c…
…lass with specified attribute
- Loading branch information
Showing
11 changed files
with
298 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
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
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
73 changes: 73 additions & 0 deletions
73
tests/Calls/FunctionCallsAllowInClassWithAttributesTest.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,73 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\Calls; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Testing\RuleTestCase; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallableParameterRuleErrors; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedFunctionRuleErrors; | ||
|
||
class FunctionCallsAllowInClassWithAttributesTest extends RuleTestCase | ||
{ | ||
|
||
/** | ||
* @throws ShouldNotHappenException | ||
*/ | ||
protected function getRule(): Rule | ||
{ | ||
$container = self::getContainer(); | ||
return new FunctionCalls( | ||
$container->getByType(DisallowedFunctionRuleErrors::class), | ||
$container->getByType(DisallowedCallableParameterRuleErrors::class), | ||
$container->getByType(DisallowedCallFactory::class), | ||
[ | ||
[ | ||
'function' => 'md5()', | ||
'allowInClassWithAttributes' => [ | ||
'\Attribute', | ||
], | ||
], | ||
[ | ||
'function' => 'sha1()', | ||
'allowInClassWithAttributes' => [ | ||
'\Attributes\Attribute2', | ||
'\Attributes\Attribute3', | ||
], | ||
], | ||
[ | ||
'function' => 'strlen()', | ||
'allowExceptInClassWithAttributes' => [ | ||
'\Attributes\Attribute3', | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/../src/AttributeClass.php'], [ | ||
[ | ||
'Calling strlen() is forbidden.', | ||
30, | ||
], | ||
[ | ||
'Calling md5() is forbidden.', | ||
41, | ||
], | ||
]); | ||
} | ||
|
||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../extension.neon', | ||
]; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
tests/Usages/AttributeUsagesAllowInClassWithAttributesTest.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,60 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\Usages; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedAttributeFactory; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedAttributeRuleErrors; | ||
|
||
class AttributeUsagesAllowInClassWithAttributesTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$container = self::getContainer(); | ||
return new AttributeUsages( | ||
$container->getByType(DisallowedAttributeRuleErrors::class), | ||
$container->getByType(DisallowedAttributeFactory::class), | ||
[ | ||
[ | ||
'attribute' => '\Attributes\Attribute4', | ||
'allowInClassWithAttributes' => [ | ||
'\Attributes\Attribute2', | ||
], | ||
], | ||
[ | ||
'attribute' => '\Attributes\Attribute5', | ||
'allowExceptInClassWithAttributes' => [ | ||
'\Attributes\Attribute3', | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/../src/AttributeClass.php'], [ | ||
[ | ||
'Attribute Attributes\Attribute5 is forbidden.', | ||
27, | ||
], | ||
[ | ||
'Attribute Attributes\Attribute4 is forbidden.', | ||
38, | ||
], | ||
]); | ||
} | ||
|
||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../extension.neon', | ||
]; | ||
} | ||
|
||
} |
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