Skip to content

Commit

Permalink
Don't return plain false when the param is not of the type we know ho…
Browse files Browse the repository at this point in the history
…w to process (#167)

Close #166
  • Loading branch information
spaze committed Jan 23, 2023
2 parents d8b634b + 69a21d3 commit cc97bfb
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/DisallowedHelper.php
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeException;
use Spaze\PHPStan\Rules\Disallowed\Params\DisallowedCallParam;

class DisallowedHelper
Expand Down Expand Up @@ -118,8 +119,12 @@ private function hasAllowedParams(Scope $scope, ?CallLike $node, array $allowCon
$types = [$type];
}
foreach ($types as $type) {
if (!$param->matches($type)) {
return false;
try {
if (!$param->matches($type)) {
return false;
}
} catch (UnsupportedParamTypeException $e) {
return !$paramsRequired;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/UnsupportedParamTypeException.php
@@ -0,0 +1,10 @@
<?php
declare(strict_types = 1);

namespace Spaze\PHPStan\Rules\Disallowed\Exceptions;

use Exception;

class UnsupportedParamTypeException extends Exception
{
}
6 changes: 5 additions & 1 deletion src/Params/DisallowedCallParamValueCaseInsensitiveExcept.php
Expand Up @@ -6,17 +6,21 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeException;

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

/**
* @throws UnsupportedParamTypeException
*/
public function matches(Type $type): bool
{
if (!$type instanceof ConstantScalarType) {
return false;
throw new UnsupportedParamTypeException();
}
$a = is_string($this->getValue()) ? strtolower($this->getValue()) : $this->getValue();
$b = $type instanceof ConstantStringType ? strtolower($type->getValue()) : $type->getValue();
Expand Down
6 changes: 5 additions & 1 deletion src/Params/DisallowedCallParamValueExcept.php
Expand Up @@ -5,17 +5,21 @@

use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeException;

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

/**
* @throws UnsupportedParamTypeException
*/
public function matches(Type $type): bool
{
if (!$type instanceof ConstantScalarType) {
return false;
throw new UnsupportedParamTypeException();
}
return $this->getValue() !== $type->getValue();
}
Expand Down
6 changes: 5 additions & 1 deletion src/Params/DisallowedCallParamValueFlagExcept.php
Expand Up @@ -5,17 +5,21 @@

use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Type;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeException;

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

/**
* @throws UnsupportedParamTypeException
*/
public function matches(Type $type): bool
{
if (!$type instanceof ConstantIntegerType) {
return false;
throw new UnsupportedParamTypeException();
}
return ($this->getValue() & $type->getValue()) === 0;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Params/DisallowedCallParamValueFlagSpecific.php
Expand Up @@ -5,17 +5,21 @@

use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Type;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeException;

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

/**
* @throws UnsupportedParamTypeException
*/
public function matches(Type $type): bool
{
if (!$type instanceof ConstantIntegerType) {
return false;
throw new UnsupportedParamTypeException();
}
return ($this->getValue() & $type->getValue()) !== 0;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Params/DisallowedCallParamValueSpecific.php
Expand Up @@ -5,17 +5,21 @@

use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeException;

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

/**
* @throws UnsupportedParamTypeException
*/
public function matches(Type $type): bool
{
if (!$type instanceof ConstantScalarType) {
return false;
throw new UnsupportedParamTypeException();
}
return $this->getValue() === $type->getValue();
}
Expand Down
3 changes: 3 additions & 0 deletions tests/src/disallowed-allow/functionCalls.php
Expand Up @@ -81,3 +81,6 @@
// allowed when not FQCN to Blade class
mocky(\Fiction\Pulp\Royale::class);
mocky(\Waldo\Quux\Blade::class);

// not disallowed
hash((new stdClass())->property . 'foo', 'NAH');
3 changes: 3 additions & 0 deletions tests/src/disallowed/functionCalls.php
Expand Up @@ -81,3 +81,6 @@
// allowed when not FQCN to Blade class
mocky(\Fiction\Pulp\Royale::class);
mocky(\Waldo\Quux\Blade::class);

// not disallowed
hash((new stdClass())->property . 'foo', 'NAH');

0 comments on commit cc97bfb

Please sign in to comment.