Skip to content

Commit

Permalink
Move the flag config type check to paramFactory()
Browse files Browse the repository at this point in the history
There's already a similar check there and PHPUnit 11 throws the following error when the check is done when the code is analyzed. That's because PHPStan sets its own error handler in \PHPStan\Analyser\FileAnalyser::analyseFile() ($this->collectErrors($analysedFiles)) but when there's an error like this, it doesn't reset it.

The PHPUnit error:
1) Spaze\PHPStan\Rules\Disallowed\Calls\FunctionCallsTypeStringParamsInvalidFlagsConfigTest::testException
Test code or tested code did not remove its own error handlers
  • Loading branch information
spaze committed Mar 19, 2024
1 parent 104dc95 commit e3f6e67
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
9 changes: 9 additions & 0 deletions src/Allowed/Allowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use Spaze\PHPStan\Rules\Disallowed\DisallowedWithParams;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
Expand All @@ -25,6 +26,7 @@
use Spaze\PHPStan\Rules\Disallowed\Params\ParamValueCaseInsensitiveExcept;
use Spaze\PHPStan\Rules\Disallowed\Params\ParamValueExcept;
use Spaze\PHPStan\Rules\Disallowed\Params\ParamValueExceptAny;
use Spaze\PHPStan\Rules\Disallowed\Params\ParamValueFlag;
use Spaze\PHPStan\Rules\Disallowed\Params\ParamValueFlagExcept;
use Spaze\PHPStan\Rules\Disallowed\Params\ParamValueFlagSpecific;
use Spaze\PHPStan\Rules\Disallowed\Params\ParamValueSpecific;
Expand Down Expand Up @@ -302,6 +304,13 @@ private function paramFactory(string $class, $key, $value): ParamValue
} else {
throw new UnsupportedParamTypeInConfigException($paramPosition, $paramName, gettype($paramValue));
}
if (is_subclass_of($class, ParamValueFlag::class)) {
foreach ($type->getConstantScalarValues() as $value) {
if (!is_int($value)) {
throw new UnsupportedParamTypeInConfigException($paramPosition, $paramName, gettype($value) . ' of ' . $type->describe(VerbosityLevel::precise()));
}
}
}
return new $class($paramPosition, $paramName, $type);
}

Expand Down
8 changes: 1 addition & 7 deletions src/Params/ParamValueFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@

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

abstract class ParamValueFlag extends ParamValue
{

/**
* @throws UnsupportedParamTypeException
* @throws UnsupportedParamTypeInConfigException
*/
protected function isFlagSet(Type $type): bool
{
if (!$type instanceof ConstantIntegerType) {
throw new UnsupportedParamTypeException();
}
foreach ($this->getType()->getConstantScalarValues() as $value) {
if (!is_int($value)) {
throw new UnsupportedParamTypeInConfigException($this->getPosition(), $this->getName(), gettype($value) . ' of ' . $this->getType()->describe(VerbosityLevel::precise()));
}
if (($value & $type->getValue()) !== 0) {
if (is_int($value) && ($value & $type->getValue()) !== 0) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

namespace Spaze\PHPStan\Rules\Disallowed\Calls;

use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Testing\PHPStanTestCase;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;

class FunctionCallsTypeStringParamsInvalidFlagsConfigTest extends RuleTestCase
class FunctionCallsTypeStringParamsInvalidFlagsConfigTest extends PHPStanTestCase
{

/**
* @throws ShouldNotHappenException
*/
protected function getRule(): Rule
public function testException(): void
{
$this->expectException(ShouldNotHappenException::class);
$this->expectExceptionMessage("Foo\Bar\Waldo\intParam1(): Parameter #1 has an unsupported type string of 2|'bruh' specified in configuration");
$container = self::getContainer();
return new FunctionCalls(
new FunctionCalls(
$container->getByType(DisallowedCallsRuleErrors::class),
$container->getByType(DisallowedCallFactory::class),
$this->createReflectionProvider(),
Expand All @@ -38,14 +38,6 @@ protected function getRule(): Rule
}


public function testException(): void
{
$this->expectException(UnsupportedParamTypeInConfigException::class);
$this->expectExceptionMessage("Parameter #1 has an unsupported type string of 2|'bruh' specified in configuration");
$this->analyse([__DIR__ . '/../src/disallowed/functionCallsTypeStringParams.php'], []);
}


public static function getAdditionalConfigFiles(): array
{
return [
Expand Down

0 comments on commit e3f6e67

Please sign in to comment.