Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can add more rules for the same call to have different messages for v…
…arious params (#232) The rules have to be sorted from the most specific to the most general to have different messages for different params, see `FunctionCallsParamsMessagesTest`. This makes sense mostly|only when you want to show a different message when various params have been disallowed. For which the new config options `allowExceptParamsAnyValue` and `disallowParamsAnyValue` (which is an alias for the former) have been added. Close #155
- Loading branch information
Showing
8 changed files
with
208 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\Params; | ||
|
||
use PHPStan\Type\Type; | ||
|
||
/** | ||
* @extends ParamValue<int|bool|string|null> | ||
*/ | ||
final class ParamValueExceptAny extends ParamValue | ||
{ | ||
|
||
public function matches(Type $type): bool | ||
{ | ||
return false; | ||
} | ||
|
||
} |
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,136 @@ | ||
<?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\DisallowedCallsRuleErrors; | ||
|
||
class FunctionCallsParamsMessagesTest extends RuleTestCase | ||
{ | ||
|
||
/** | ||
* @throws ShouldNotHappenException | ||
*/ | ||
protected function getRule(): Rule | ||
{ | ||
$container = self::getContainer(); | ||
return new FunctionCalls( | ||
$container->getByType(DisallowedCallsRuleErrors::class), | ||
$container->getByType(DisallowedCallFactory::class), | ||
$this->createReflectionProvider(), | ||
[ | ||
[ | ||
'function' => '\Foo\Bar\Waldo\config()', | ||
'message' => 'foo & bar', | ||
'allowIn' => [ | ||
__DIR__ . '/../src/disallowed-allow/*.php', | ||
__DIR__ . '/../src/*-allow/*.*', | ||
], | ||
'disallowParamsAnyValue' => [ | ||
1, | ||
2, | ||
], | ||
], | ||
[ | ||
'function' => '\Foo\Bar\Waldo\config()', | ||
'message' => 'foo', | ||
'allowIn' => [ | ||
__DIR__ . '/../src/disallowed-allow/*.php', | ||
__DIR__ . '/../src/*-allow/*.*', | ||
], | ||
'disallowParamsAnyValue' => [ | ||
1, | ||
], | ||
], | ||
[ | ||
'function' => '\Foo\Bar\Waldo\config()', | ||
'message' => 'nothing', | ||
'allowIn' => [ | ||
__DIR__ . '/../src/disallowed-allow/*.php', | ||
__DIR__ . '/../src/*-allow/*.*', | ||
], | ||
], | ||
[ | ||
'function' => '\Foo\Bar\Waldo\bar()', | ||
'message' => 'foo & bar', | ||
'allowIn' => [ | ||
__DIR__ . '/../src/disallowed-allow/*.php', | ||
__DIR__ . '/../src/*-allow/*.*', | ||
], | ||
'disallowParams' => [ | ||
1 => 'foo', | ||
2 => 'bar', | ||
], | ||
], | ||
[ | ||
'function' => '\Foo\Bar\Waldo\bar()', | ||
'message' => 'foo', | ||
'allowIn' => [ | ||
__DIR__ . '/../src/disallowed-allow/*.php', | ||
__DIR__ . '/../src/*-allow/*.*', | ||
], | ||
'disallowParams' => [ | ||
1 => 'foo', | ||
], | ||
], | ||
[ | ||
'function' => '\Foo\Bar\Waldo\bar()', | ||
'message' => 'nothing', | ||
'allowIn' => [ | ||
__DIR__ . '/../src/disallowed-allow/*.php', | ||
__DIR__ . '/../src/*-allow/*.*', | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
|
||
public function testRule(): void | ||
{ | ||
// Based on the configuration above, in this file: | ||
$this->analyse([__DIR__ . '/../src/disallowed/functionCallsParamsMessages.php'], [ | ||
[ | ||
// expect this error message: | ||
'Calling Foo\Bar\Waldo\config() is forbidden, nothing.', | ||
// on this line: | ||
5, | ||
], | ||
[ | ||
'Calling Foo\Bar\Waldo\config() is forbidden, foo.', | ||
6, | ||
], | ||
[ | ||
'Calling Foo\Bar\Waldo\config() is forbidden, foo & bar.', | ||
7, | ||
], | ||
[ | ||
'Calling Foo\Bar\Waldo\bar() is forbidden, nothing.', | ||
8, | ||
], | ||
[ | ||
'Calling Foo\Bar\Waldo\bar() is forbidden, foo.', | ||
9, | ||
], | ||
[ | ||
'Calling Foo\Bar\Waldo\bar() is forbidden, foo & bar.', | ||
10, | ||
], | ||
]); | ||
// Based on the configuration above, no errors in this file: | ||
$this->analyse([__DIR__ . '/../src/disallowed-allow/functionCallsParamsMessages.php'], []); | ||
} | ||
|
||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../extension.neon', | ||
]; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/src/disallowed-allow/functionCallsParamsMessages.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,10 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
// allowed by path | ||
\Foo\Bar\Waldo\config(); | ||
\Foo\Bar\Waldo\config('foo'); | ||
\Foo\Bar\Waldo\config('foo', 'bar'); | ||
\Foo\Bar\Waldo\bar(); | ||
\Foo\Bar\Waldo\bar('foo'); | ||
\Foo\Bar\Waldo\bar('foo', 'bar'); |
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,10 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
// disallowed with different messages | ||
\Foo\Bar\Waldo\config(); | ||
\Foo\Bar\Waldo\config('foo'); | ||
\Foo\Bar\Waldo\config('foo', 'bar'); | ||
\Foo\Bar\Waldo\bar(); | ||
\Foo\Bar\Waldo\bar('foo'); | ||
\Foo\Bar\Waldo\bar('foo', 'bar'); |