Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ConstantScalarType to Type in DisallowedCallParam #159

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/DisallowedHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
Expand Down Expand Up @@ -109,7 +108,7 @@ private function hasAllowedParams(Scope $scope, ?CallLike $node, array $allowCon

foreach ($allowConfig as $param) {
$type = $this->getArgType($node, $scope, $param);
if (!$type instanceof ConstantScalarType) {
if ($type === null) {
return !$paramsRequired;
}
if (!$param->matches($type)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Params/DisallowedCallParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Spaze\PHPStan\Rules\Disallowed\Params;

use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

interface DisallowedCallParam
{

public function matches(ConstantScalarType $type): bool;
public function matches(Type $type): bool;


public function getPosition(): ?int;
Expand Down
4 changes: 2 additions & 2 deletions src/Params/DisallowedCallParamValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Spaze\PHPStan\Rules\Disallowed\Params;

use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

/**
* @template T of int|bool|string|null
Expand All @@ -21,7 +21,7 @@ abstract class DisallowedCallParamValue implements DisallowedCallParam
private $value;


abstract public function matches(ConstantScalarType $type): bool;
abstract public function matches(Type $type): bool;


/**
Expand Down
4 changes: 2 additions & 2 deletions src/Params/DisallowedCallParamValueAny.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace Spaze\PHPStan\Rules\Disallowed\Params;

use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

/**
* @extends DisallowedCallParamValue<int|bool|string|null>
*/
final class DisallowedCallParamValueAny extends DisallowedCallParamValue
{

public function matches(ConstantScalarType $type): bool
public function matches(Type $type): bool
{
return true;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Params/DisallowedCallParamValueCaseInsensitiveExcept.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@

use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

/**
* @extends DisallowedCallParamValue<int|bool|string|null>
*/
class DisallowedCallParamValueCaseInsensitiveExcept extends DisallowedCallParamValue
{

public function matches(ConstantScalarType $type): bool
public function matches(Type $type): bool
{
if (!$type instanceof ConstantScalarType) {
return false;
}
$a = is_string($this->getValue()) ? strtolower($this->getValue()) : $this->getValue();
$b = $type instanceof ConstantStringType ? strtolower($type->getValue()) : $type->getValue();
return $a !== $b;
Expand Down
6 changes: 5 additions & 1 deletion src/Params/DisallowedCallParamValueExcept.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
namespace Spaze\PHPStan\Rules\Disallowed\Params;

use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

/**
* @extends DisallowedCallParamValue<int|bool|string|null>
*/
class DisallowedCallParamValueExcept extends DisallowedCallParamValue
{

public function matches(ConstantScalarType $type): bool
public function matches(Type $type): bool
{
if (!$type instanceof ConstantScalarType) {
return false;
}
return $this->getValue() !== $type->getValue();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Params/DisallowedCallParamValueFlagExcept.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
namespace Spaze\PHPStan\Rules\Disallowed\Params;

use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

/**
* @extends DisallowedCallParamValue<int>
*/
class DisallowedCallParamValueFlagExcept extends DisallowedCallParamValue
{

public function matches(ConstantScalarType $type): bool
public function matches(Type $type): bool
{
if (!$type instanceof ConstantIntegerType) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Params/DisallowedCallParamValueFlagSpecific.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
namespace Spaze\PHPStan\Rules\Disallowed\Params;

use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

/**
* @extends DisallowedCallParamValue<int>
*/
class DisallowedCallParamValueFlagSpecific extends DisallowedCallParamValue
{

public function matches(ConstantScalarType $type): bool
public function matches(Type $type): bool
{
if (!$type instanceof ConstantIntegerType) {
return false;
Expand Down
6 changes: 5 additions & 1 deletion src/Params/DisallowedCallParamValueSpecific.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
namespace Spaze\PHPStan\Rules\Disallowed\Params;

use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

/**
* @extends DisallowedCallParamValue<int|bool|string|null>
*/
class DisallowedCallParamValueSpecific extends DisallowedCallParamValue
{

public function matches(ConstantScalarType $type): bool
public function matches(Type $type): bool
{
if (!$type instanceof ConstantScalarType) {
return false;
}
return $this->getValue() === $type->getValue();
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Calls/FunctionCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ protected function getRule(): Rule
],
],
],
[
'function' => 'array_filter()',
'message' => 'callback parameter must be given.',
'allowIn' => [
'../src/disallowed-allowed/*.php',
'../src/*-allow/*.*',
],
'allowParamsAnywhereAnyValue' => [
2,
],
],
]
);
}
Expand Down Expand Up @@ -252,6 +263,10 @@ public function testRule(): void
'Calling htmlspecialchars() is forbidden, because reasons',
71,
],
[
'Calling array_filter() is forbidden, callback parameter must be given.',
74,
],
]);
// Based on the configuration above, no errors in this file:
$this->analyse([__DIR__ . '/../src/disallowed-allow/functionCalls.php'], [
Expand Down
6 changes: 6 additions & 0 deletions tests/src/disallowed-allow/functionCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@
htmlspecialchars('foo', ENT_QUOTES);
htmlspecialchars('foo', ENT_DISALLOWED | ENT_QUOTES);
htmlspecialchars('foo', ENT_DISALLOWED | ENT_QUOTES | ENT_HTML5);

// allowed only with callback
array_filter(['1', '2']);
array_filter(['1', '2'], function () {
return true;
});
6 changes: 6 additions & 0 deletions tests/src/disallowed/functionCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@
htmlspecialchars('foo', ENT_QUOTES);
htmlspecialchars('foo', ENT_DISALLOWED | ENT_QUOTES);
htmlspecialchars('foo', ENT_DISALLOWED | ENT_QUOTES | ENT_HTML5);

// allowed only with callback
array_filter(['1', '2']);
array_filter(['1', '2'], function () {
return true;
});