Skip to content

Commit

Permalink
Test for #288
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Jan 22, 2025
1 parent c921da2 commit 2090224
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 206 deletions.
8 changes: 8 additions & 0 deletions src/RuleErrors/DisallowedCallableParameterRuleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ private function getErrors(CallLike $node, Scope $scope, $reflection): array
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $reflection->getVariants());
$reorderedArgs = ArgumentsNormalizer::reorderArgs($parametersAcceptor, $node->getArgs()) ?? $node->getArgs();
foreach ($parametersAcceptor->getParameters() as $key => $parameter) {
var_dump(

Check failure on line 178 in src/RuleErrors/DisallowedCallableParameterRuleErrors.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (8.0, composer phpstan, --prefer-lowest)

Calling var_dump() is forbidden, use some logger instead.

Check failure on line 178 in src/RuleErrors/DisallowedCallableParameterRuleErrors.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (7.4, composer phpstan)

Calling var_dump() is forbidden, use some logger instead.

Check failure on line 178 in src/RuleErrors/DisallowedCallableParameterRuleErrors.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (8.1, composer phpstan)

Calling var_dump() is forbidden, use some logger instead.

Check failure on line 178 in src/RuleErrors/DisallowedCallableParameterRuleErrors.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (7.4, composer phpstan, --prefer-lowest)

Calling var_dump() is forbidden, use some logger instead.

Check failure on line 178 in src/RuleErrors/DisallowedCallableParameterRuleErrors.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (8.0, composer phpstan)

Calling var_dump() is forbidden, use some logger instead.
'****',
'param->getName:',
$parameter->getName(),
'param->isCallable:',
TypeCombinator::removeNull($parameter->getType())->isCallable()->describe(),
'========================================'
);
if (!TypeCombinator::removeNull($parameter->getType())->isCallable()->yes() || !isset($reorderedArgs[$key])) {
continue;
}
Expand Down
224 changes: 18 additions & 206 deletions tests/Calls/MethodCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

namespace Spaze\PHPStan\Rules\Disallowed\Calls;

use PHPStan\Reflection\ReflectionProvider;
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;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedMethodRuleErrors;
use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver;

class MethodCallsTest extends RuleTestCase
{
Expand All @@ -19,228 +22,37 @@ class MethodCallsTest extends RuleTestCase
protected function getRule(): Rule
{
$container = self::getContainer();
return new MethodCalls(
$disallowedCallableParameterRuleErrors = new DisallowedCallableParameterRuleErrors(
$container->getByType(TypeResolver::class),
$container->getByType(DisallowedFunctionRuleErrors::class),
$container->getByType(DisallowedMethodRuleErrors::class),
$container->getByType(DisallowedCallableParameterRuleErrors::class),
$container->getByType(DisallowedCallFactory::class),
$container->getByType(ReflectionProvider::class),
[
[
'method' => 'Waldo\Quux\Blade::run*()',
'message' => "I've seen tests you people wouldn't believe",
'exclude' => 'Waldo\Quux\Blade::runway()',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
'allowParamsInAllowed' => [
1 => 42,
2 => true,
3 => '909',
],
],
[
'method' => 'Waldo\Quux\Blade::movie()',
'message' => 'was good',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
[
'method' => 'Waldo\Quux\Blade::sequel()',
'message' => 'too',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
[
'method' => 'Waldo\Quux\Blade::Trinity()',
'message' => 'holy trinity',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
[
'method' => 'Inheritance\Base::x*()',
'message' => 'Base::x*() methods are dangerous',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
[
'method' => 'Interfaces\BaseInterface::x*()',
'message' => 'BaseInterface::x*() methods are dangerous',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
[
'method' => 'Traits\TestTrait::*',
'message' => 'all TestTrait methods are dangerous',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
[
'method' => 'Traits\AnotherTestClass::zzTop()',
'message' => 'method AnotherTestClass::zzTop() is dangerous',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
[
'method' => 'PhpOption\None::getIterator()',
'message' => 'no PhpOption',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
[
'method' => 'PhpOption\Some::getIterator()',
'message' => 'no PhpOption',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
// test disallowed param values
[
'function' => 'DateTime::format()',
'message' => 'why too kay',
'function' => 'optional()',
'allowIn' => [
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
'allowExceptParams' => [
1 => 'y',
],
'errorTip' => '2038 is approaching fast',
],
// test disallowed paths
[
'method' => 'Waldo\Quux\Blade::andSorcery()',
'message' => 'use magic',
'allowExceptIn' => [
__DIR__ . '/../src/disallowed/*.php',
],
],
]
],
[],
[],
);
return new MethodCalls(
$container->getByType(DisallowedMethodRuleErrors::class),
$disallowedCallableParameterRuleErrors,
$container->getByType(DisallowedCallFactory::class),
[]
);
}


public function testRule(): void
{
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../src/disallowed/methodCalls.php'], [
[
// expect this error message:
"Calling Waldo\Quux\Blade::runner() is forbidden, I've seen tests you people wouldn't believe. [Waldo\Quux\Blade::runner() matches Waldo\Quux\Blade::run*()]",
// on this line:
10,
],
[
"Calling Waldo\Quux\Blade::runner() is forbidden, I've seen tests you people wouldn't believe. [Waldo\Quux\Blade::runner() matches Waldo\Quux\Blade::run*()]",
11,
],
[
"Calling Waldo\Quux\Blade::runner() is forbidden, I've seen tests you people wouldn't believe. [Waldo\Quux\Blade::runner() matches Waldo\Quux\Blade::run*()]",
14,
],
[
'Calling Inheritance\Base::x() (as Inheritance\Sub::x()) is forbidden, Base::x*() methods are dangerous. [Inheritance\Base::x() matches Inheritance\Base::x*()]',
22,
],
[
'Calling Traits\TestTrait::x() (as Traits\TestClass::x()) is forbidden, all TestTrait methods are dangerous. [Traits\TestTrait::x() matches Traits\TestTrait::*()]',
26,
],
[
'Calling Traits\TestTrait::y() (as Traits\AnotherTestClass::y()) is forbidden, all TestTrait methods are dangerous. [Traits\TestTrait::y() matches Traits\TestTrait::*()]',
28,
],
[
'Calling Traits\AnotherTestClass::zzTop() is forbidden, method AnotherTestClass::zzTop() is dangerous.',
29,
],
[
'Calling PhpOption\None::getIterator() is forbidden, no PhpOption.',
46,
],
[
'Calling PhpOption\Some::getIterator() is forbidden, no PhpOption.',
52,
],
[
'Calling DateTime::format() is forbidden, why too kay.',
55,
'2038 is approaching fast',
],
[
'Calling Waldo\Quux\Blade::movie() is forbidden, was good.',
60,
],
[
'Calling Waldo\Quux\Blade::movie() is forbidden, was good.',
61,
],
[
'Calling Waldo\Quux\Blade::Sequel() is forbidden, too. [Waldo\Quux\Blade::Sequel() matches Waldo\Quux\Blade::sequel()]',
62,
],
[
'Calling Waldo\Quux\Blade::Sequel() is forbidden, too. [Waldo\Quux\Blade::Sequel() matches Waldo\Quux\Blade::sequel()]',
63,
],
[
'Calling Waldo\Quux\Blade::trinity() is forbidden, holy trinity. [Waldo\Quux\Blade::trinity() matches Waldo\Quux\Blade::Trinity()]',
64,
],
[
'Calling Waldo\Quux\Blade::trinity() is forbidden, holy trinity. [Waldo\Quux\Blade::trinity() matches Waldo\Quux\Blade::Trinity()]',
65,
],
[
'Calling Waldo\Quux\Blade::andSorcery() is forbidden, use magic.',
68,
],
[
'Calling Interfaces\BaseInterface::x() (as Interfaces\Implementation::x()) is forbidden, BaseInterface::x*() methods are dangerous. [Interfaces\BaseInterface::x() matches Interfaces\BaseInterface::x*()]',
74,
],
[
'Calling Interfaces\BaseInterface::x() (as class@anonymous::x()) is forbidden, BaseInterface::x*() methods are dangerous. [Interfaces\BaseInterface::x() matches Interfaces\BaseInterface::x*()]',
87,
],
[
'Calling Inheritance\Base::x() (as class@anonymous::x()) is forbidden, Base::x*() methods are dangerous. [Inheritance\Base::x() matches Inheritance\Base::x*()]',
91,
],
[
"Calling Waldo\Quux\Blade::runner() is forbidden, I've seen tests you people wouldn't believe. [Waldo\Quux\Blade::runner() matches Waldo\Quux\Blade::run*()]",
95,
],
[
"Calling Waldo\Quux\Blade::runner() is forbidden, I've seen tests you people wouldn't believe. [Waldo\Quux\Blade::runner() matches Waldo\Quux\Blade::run*()]",
96,
],
]);
$this->analyse([__DIR__ . '/../src/disallowed-allow/methodCalls.php'], [
[
"Calling Waldo\Quux\Blade::runner() is forbidden, I've seen tests you people wouldn't believe. [Waldo\Quux\Blade::runner() matches Waldo\Quux\Blade::run*()]",
10,
],
[
"Calling Waldo\Quux\Blade::runner() is forbidden, I've seen tests you people wouldn't believe. [Waldo\Quux\Blade::runner() matches Waldo\Quux\Blade::run*()]",
11,
],
$this->analyse([__DIR__ . '/../src/disallowed/testCase.php'], [
]);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/src/disallowed/testCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types = 1);

use PHPUnit\Framework\TestCase;

function foobar() {}

final class NothingTest extends TestCase
{
public function testItDoesNothing(): void
{
$this->assertSame('foobar', 'whatever');
}
}

0 comments on commit 2090224

Please sign in to comment.