From 9ab3944f840a02e06fcdfc66aa1eee9dac1b944e Mon Sep 17 00:00:00 2001 From: Ivan Fedorov Date: Tue, 27 Aug 2024 11:50:54 +0000 Subject: [PATCH] cleanup tests --- tests/AbstractBaseStubsTestCase.php | 4 --- tests/Model/BasePHPClass.php | 4 +-- tests/Model/Tags/RemovedTag.php | 30 +++++------------- .../Parsers/ExpectedFunctionArgumentsInfo.php | 2 +- .../MetaExpectedArgumentsCollector.php | 9 ++---- tests/Parsers/ParserUtils.php | 20 +----------- tests/Parsers/StubParser.php | 2 +- tests/Parsers/Visitors/ASTVisitor.php | 31 ++++++------------- tests/Parsers/Visitors/CoreStubASTVisitor.php | 1 - .../Visitors/MetaOverrideFunctionsParser.php | 5 +-- tests/TestData/Providers/EntitiesFilter.php | 4 +-- .../ReflectionParametersProvider.php | 2 +- .../Providers/Stubs/StubMethodsProvider.php | 5 +-- .../Stubs/StubsParametersProvider.php | 5 +-- 14 files changed, 35 insertions(+), 89 deletions(-) diff --git a/tests/AbstractBaseStubsTestCase.php b/tests/AbstractBaseStubsTestCase.php index 90940076d..a12fcd236 100644 --- a/tests/AbstractBaseStubsTestCase.php +++ b/tests/AbstractBaseStubsTestCase.php @@ -31,9 +31,6 @@ abstract class AbstractBaseStubsTestCase extends TestCase { protected string $emptyDataSetMessage = "Data provider returned empty set"; - /** - * @throws Exception|RuntimeException - */ public static function getStringRepresentationOfDefaultParameterValue(mixed $defaultValue, PHPClass|PHPInterface|null $contextClass = null): float|bool|int|string|null { if ($defaultValue instanceof ConstFetch) { @@ -130,7 +127,6 @@ public static function getParameterRepresentation(PHPFunction $function): string /** * @param PHPFunction[] $filtered * @return PHPFunction[] - * @throws RuntimeException */ protected static function getDuplicatedFunctions(array $filtered): array { diff --git a/tests/Model/BasePHPClass.php b/tests/Model/BasePHPClass.php index 506bf43c4..451facf60 100644 --- a/tests/Model/BasePHPClass.php +++ b/tests/Model/BasePHPClass.php @@ -49,9 +49,9 @@ public function getConstant($constantName, $shouldSuitCurrentPhpVersion = true, return $constant->name === $constantName && $constant->stubObjectHash == null; }); } else { - $constants = array_filter($this->constants, function (PHPClassConstant $constant) use ($constantName) { + $constants = array_filter($this->constants, function (PHPClassConstant $constant) use ($constantName, $shouldSuitCurrentPhpVersion) { return $constant->name === $constantName && $constant->duplicateOtherElement === false - && ParserUtils::entitySuitsCurrentPhpVersion($constant); + && (!$shouldSuitCurrentPhpVersion || ParserUtils::entitySuitsCurrentPhpVersion($constant)); }); } return array_pop($constants); diff --git a/tests/Model/Tags/RemovedTag.php b/tests/Model/Tags/RemovedTag.php index 12a8e9fb3..e03a2fa7c 100644 --- a/tests/Model/Tags/RemovedTag.php +++ b/tests/Model/Tags/RemovedTag.php @@ -9,27 +9,17 @@ class RemovedTag extends BaseTag { - const REGEX_VECTOR = '(?:\d\S*|[^\s\:]+\:\s*\$[^\$]+\$)'; - private $version; + const string REGEX_VECTOR = '(?:\d\S*|[^\s\:]+\:\s*\$[^\$]+\$)'; + private ?string $version; - /** - * @param string|null $version - * @param Description|null $description - */ - public function __construct($version = null, ?Description $description = null) + public function __construct(?string $version = null, ?Description $description = null) { $this->version = $version; $this->name = 'removed'; $this->description = $description; } - /** - * @param string|null $body - * @param DescriptionFactory|null $descriptionFactory - * @param Context|null $context - * @return RemovedTag - */ - public static function create($body, $descriptionFactory = null, $context = null) + public static function create(?string $body, ?DescriptionFactory $descriptionFactory = null, ?Context $context = null): RemovedTag { if (empty($body)) { return new self(); @@ -43,24 +33,18 @@ public static function create($body, $descriptionFactory = null, $context = null return new self( $matches[1], - $descriptionFactory->create(isset($matches[2]) ? $matches[2] : '', $context) + $descriptionFactory->create($matches[2] ?? '', $context) ); } return new self(); } - /** - * @return string|null - */ - public function getVersion() + public function getVersion(): ?string { return $this->version; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return "PhpStorm internal '@removed' tag"; } diff --git a/tests/Parsers/ExpectedFunctionArgumentsInfo.php b/tests/Parsers/ExpectedFunctionArgumentsInfo.php index e8fd6cd60..6ebb4b6e1 100644 --- a/tests/Parsers/ExpectedFunctionArgumentsInfo.php +++ b/tests/Parsers/ExpectedFunctionArgumentsInfo.php @@ -14,7 +14,7 @@ class ExpectedFunctionArgumentsInfo * @param Expr[] $expectedArguments * @param int $index */ - public function __construct(private ?Expr $functionReference, private array $expectedArguments, private int $index) {} + public function __construct(private ?Expr $functionReference, private array $expectedArguments, private readonly int $index) {} public function getFunctionReference(): ?Expr { diff --git a/tests/Parsers/MetaExpectedArgumentsCollector.php b/tests/Parsers/MetaExpectedArgumentsCollector.php index 796210ca8..8ff11543c 100644 --- a/tests/Parsers/MetaExpectedArgumentsCollector.php +++ b/tests/Parsers/MetaExpectedArgumentsCollector.php @@ -18,9 +18,9 @@ class MetaExpectedArgumentsCollector extends NodeVisitorAbstract { - private const EXPECTED_ARGUMENTS = 'expectedArguments'; - private const EXPECTED_RETURN_VALUES = 'expectedReturnValues'; - private const REGISTER_ARGUMENTS_SET_NAME = 'registerArgumentsSet'; + private const string EXPECTED_ARGUMENTS = 'expectedArguments'; + private const string EXPECTED_RETURN_VALUES = 'expectedReturnValues'; + private const string REGISTER_ARGUMENTS_SET_NAME = 'registerArgumentsSet'; /** * @var ExpectedFunctionArgumentsInfo[] @@ -45,9 +45,6 @@ public function __construct() ); } - /** - * @throws RuntimeException - */ public function enterNode(Node $node): void { if (property_exists($node, 'expr') && $node->expr instanceof FuncCall) { diff --git a/tests/Parsers/ParserUtils.php b/tests/Parsers/ParserUtils.php index d7174a901..0bf79a6a1 100644 --- a/tests/Parsers/ParserUtils.php +++ b/tests/Parsers/ParserUtils.php @@ -5,7 +5,6 @@ use phpDocumentor\Reflection\DocBlock\Tags\Deprecated; use phpDocumentor\Reflection\DocBlock\Tags\Since; -use RuntimeException; use StubTests\Model\BasePHPElement; use StubTests\Model\CommonUtils; use StubTests\Model\PHPClassConstant; @@ -21,9 +20,6 @@ public static function tagDoesNotHaveZeroPatchVersion(Since|RemovedTag|Deprecate return (bool)preg_match('/^[1-9]+\.\d+(\.[1-9]+\d*)*$/', $tag->getVersion()); //find version like any but 7.4.0 } - /** - * @throws RuntimeException - */ public static function getDeclaredSinceVersion(BasePHPElement $element): ?float { $allSinceVersions = self::getSinceVersionsFromPhpDoc($element); @@ -41,9 +37,6 @@ public static function getDeclaredSinceVersion(BasePHPElement $element): ?float return array_pop($flattenedArray) ?: 5.3; } - /** - * @throws RuntimeException - */ public static function getLatestAvailableVersion(BasePHPElement $element): ?float { $latestVersionsFromPhpDoc = self::getLatestAvailableVersionFromPhpDoc($element); @@ -65,9 +58,6 @@ public static function getLatestAvailableVersion(BasePHPElement $element): ?floa return array_pop($flattenedArray); } - /** - * @throws RuntimeException - */ public static function getAvailableInVersions(?BasePHPElement $element): array { if ($element !== null) { @@ -118,7 +108,6 @@ private static function getLatestAvailableVersionFromPhpDoc(BasePHPElement $elem /** * @return float[] - * @throws RuntimeException */ private static function getSinceVersionsFromParentClass(PHPMethod|PHPClassConstant $element): array { @@ -140,7 +129,6 @@ private static function getSinceVersionsFromParentClass(PHPMethod|PHPClassConsta /** * @return float[] - * @throws RuntimeException */ public static function getLatestAvailableVersionsFromParentClass(PHPMethod|PHPClassConstant $element): array { @@ -184,13 +172,7 @@ public static function getLatestAvailableVersionsFromAttribute(BasePHPElement $e return $latestAvailableVersions; } - /** - * @param BasePHPElement $element - * - * @return bool - * @throws RuntimeException - */ - public static function entitySuitsCurrentPhpVersion(BasePHPElement $element) + public static function entitySuitsCurrentPhpVersion(BasePHPElement $element):bool { return in_array((float)getenv('PHP_VERSION'), ParserUtils::getAvailableInVersions($element), true); } diff --git a/tests/Parsers/StubParser.php b/tests/Parsers/StubParser.php index 0256ac856..3143c56c3 100644 --- a/tests/Parsers/StubParser.php +++ b/tests/Parsers/StubParser.php @@ -120,7 +120,7 @@ public static function getPhpStormStubs(): StubsContainer */ public static function processStubs(NodeVisitorAbstract $visitor, ?CoreStubASTVisitor $coreStubASTVisitor, callable $fileCondition): void { - $parser = (new ParserFactory())->createForNewestSupportedVersion(); + $parser = new ParserFactory()->createForNewestSupportedVersion(); $nameResolver = new NameResolver(null, ['preserveOriginalNames' => true]); $stubsIterator = diff --git a/tests/Parsers/Visitors/ASTVisitor.php b/tests/Parsers/Visitors/ASTVisitor.php index 78ae61cf2..976666f59 100644 --- a/tests/Parsers/Visitors/ASTVisitor.php +++ b/tests/Parsers/Visitors/ASTVisitor.php @@ -3,7 +3,6 @@ namespace StubTests\Parsers\Visitors; -use Exception; use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Stmt\Class_; @@ -12,7 +11,6 @@ use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Interface_; use PhpParser\NodeVisitorAbstract; -use RuntimeException; use StubTests\Model\CommonUtils; use StubTests\Model\PHPClass; use StubTests\Model\PHPConstantDeclaration; @@ -34,27 +32,24 @@ public function __construct( public ?string $sourceFilePath = null ) {} - /** - * @throws Exception - */ public function enterNode(Node $node): void { if ($node instanceof Function_) { - $function = (new PHPFunction())->readObjectFromStubNode($node); + $function = new PHPFunction()->readObjectFromStubNode($node); $function->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { $function->stubBelongsToCore = true; } $this->stubs->addFunction($function); } elseif ($node instanceof Node\Stmt\EnumCase) { - $constant = (new PHPEnumCase())->readObjectFromStubNode($node); + $constant = new PHPEnumCase()->readObjectFromStubNode($node); $constant->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { $constant->stubBelongsToCore = true; } $this->childEntitiesToAdd['enumCases'][] = $constant; } elseif ($node instanceof Node\Stmt\ClassConst) { - $constantDeclaration = (new PHPConstantDeclaration())->readObjectFromStubNode($node); + $constantDeclaration = new PHPConstantDeclaration()->readObjectFromStubNode($node); foreach ($constantDeclaration->constants as $constant) { $constant->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { @@ -63,7 +58,7 @@ public function enterNode(Node $node): void $this->childEntitiesToAdd['classConstants'][] = $constant; } } elseif ($node instanceof Node\Stmt\Const_) { - $constantDeclaration = (new PHPConstantDeclaration())->readObjectFromStubNode($node); + $constantDeclaration = new PHPConstantDeclaration()->readObjectFromStubNode($node); foreach ($constantDeclaration->constants as $constant) { $constant->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { @@ -73,7 +68,7 @@ public function enterNode(Node $node): void } } elseif ($node instanceof FuncCall) { if ((string)$node->name === 'define') { - $constant = (new PHPDefineConstant())->readObjectFromStubNode($node); + $constant = new PHPDefineConstant()->readObjectFromStubNode($node); $constant->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { $constant->stubBelongsToCore = true; @@ -81,35 +76,35 @@ public function enterNode(Node $node): void $this->stubs->addConstant($constant); } } elseif ($node instanceof ClassMethod) { - $method = (new PHPMethod())->readObjectFromStubNode($node); + $method = new PHPMethod()->readObjectFromStubNode($node); $method->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { $method->stubBelongsToCore = true; } $this->childEntitiesToAdd['methods'][] = $method; } elseif ($node instanceof Interface_) { - $interface = (new PHPInterface())->readObjectFromStubNode($node); + $interface = new PHPInterface()->readObjectFromStubNode($node); $interface->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { $interface->stubBelongsToCore = true; } $this->stubs->addInterface($interface); } elseif ($node instanceof Class_) { - $class = (new PHPClass())->readObjectFromStubNode($node); + $class = new PHPClass()->readObjectFromStubNode($node); $class->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { $class->stubBelongsToCore = true; } $this->stubs->addClass($class); } elseif ($node instanceof Enum_) { - $enum = (new PHPEnum())->readObjectFromStubNode($node); + $enum = new PHPEnum()->readObjectFromStubNode($node); $enum->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { $enum->stubBelongsToCore = true; } $this->stubs->addEnum($enum); } elseif ($node instanceof Node\Stmt\Property) { - $property = (new PHPProperty())->readObjectFromStubNode($node); + $property = new PHPProperty()->readObjectFromStubNode($node); $property->sourceFilePath = $this->sourceFilePath; if ($this->isStubCore) { $property->stubBelongsToCore = true; @@ -118,9 +113,6 @@ public function enterNode(Node $node): void } } - /** - * @throws RuntimeException - */ public function combineParentInterfaces(PHPInterface $interface): array { $parents = []; @@ -149,9 +141,6 @@ public function combineParentInterfaces(PHPInterface $interface): array return $parents; } - /** - * @throws RuntimeException - */ public function combineImplementedInterfaces(PHPClass $class): array { $interfaces = []; diff --git a/tests/Parsers/Visitors/CoreStubASTVisitor.php b/tests/Parsers/Visitors/CoreStubASTVisitor.php index fc19c2b0b..897f606a2 100644 --- a/tests/Parsers/Visitors/CoreStubASTVisitor.php +++ b/tests/Parsers/Visitors/CoreStubASTVisitor.php @@ -8,7 +8,6 @@ class CoreStubASTVisitor extends ASTVisitor { - #[Pure] public function __construct(StubsContainer $stubs, array &$entitiesToUpdate) { parent::__construct($stubs, $entitiesToUpdate); diff --git a/tests/Parsers/Visitors/MetaOverrideFunctionsParser.php b/tests/Parsers/Visitors/MetaOverrideFunctionsParser.php index 04254610d..7ab606c6e 100644 --- a/tests/Parsers/Visitors/MetaOverrideFunctionsParser.php +++ b/tests/Parsers/Visitors/MetaOverrideFunctionsParser.php @@ -15,7 +15,7 @@ class MetaOverrideFunctionsParser extends NodeVisitorAbstract { - private const OVERRIDE_FUNCTION = 'override'; + private const string OVERRIDE_FUNCTION = 'override'; /** * @var string[] @@ -36,9 +36,6 @@ public function __construct() ); } - /** - * @throws RuntimeException - */ public function enterNode(Node $node): void { if ($node instanceof Node\Expr\FuncCall && (string)$node->name === self::OVERRIDE_FUNCTION) { diff --git a/tests/TestData/Providers/EntitiesFilter.php b/tests/TestData/Providers/EntitiesFilter.php index ca3573f69..620dcc5c2 100644 --- a/tests/TestData/Providers/EntitiesFilter.php +++ b/tests/TestData/Providers/EntitiesFilter.php @@ -3,6 +3,7 @@ namespace StubTests\TestData\Providers; +use Exception; use RuntimeException; use StubTests\Model\PHPClass; use StubTests\Model\PHPEnum; @@ -41,7 +42,6 @@ public static function getFiltered(array $entities, ?callable $additionalFilter /** * @return PHPFunction[] - * @throws RuntimeException */ public static function getFilteredStubsFunctions(bool $shouldSuitCurrentPhpVersion = true): array { @@ -173,7 +173,7 @@ public static function getFilterFunctionForAllowedTypeHintsInLanguageLevel(strin return $reflectionMethod !== null && ($stubMethod->isFinal || $stubClass->isFinal || $firstSinceVersion !== null && $firstSinceVersion > $languageVersion); }, - default => throw new \Exception("Unknown class type"), + default => throw new Exception("Unknown class type"), }; } } diff --git a/tests/TestData/Providers/Reflection/ReflectionParametersProvider.php b/tests/TestData/Providers/Reflection/ReflectionParametersProvider.php index 9405306e8..22c49708d 100644 --- a/tests/TestData/Providers/Reflection/ReflectionParametersProvider.php +++ b/tests/TestData/Providers/Reflection/ReflectionParametersProvider.php @@ -312,7 +312,7 @@ public static function classMethodOptionalParametersProvider(): ?Generator ); }, EntitiesFilter::getFilteredReflectionMethods($class)), fn ($arr) => !empty($arr)); }, $filtered), fn ($arr) => !empty($arr)); - if (empty($filtered)) { + if (empty($array)) { yield [null, null, null]; } else { foreach ($filtered as $class) { diff --git a/tests/TestData/Providers/Stubs/StubMethodsProvider.php b/tests/TestData/Providers/Stubs/StubMethodsProvider.php index 4310aeacc..066a8cbb8 100644 --- a/tests/TestData/Providers/Stubs/StubMethodsProvider.php +++ b/tests/TestData/Providers/Stubs/StubMethodsProvider.php @@ -3,6 +3,7 @@ namespace StubTests\TestData\Providers\Stubs; +use Exception; use Generator; use RuntimeException; use StubTests\Model\PHPClass; @@ -234,7 +235,7 @@ private static function getFilterFunctionForLanguageLevel(string $classType, flo !$class->isFinal && $firstSinceVersion !== null && $firstSinceVersion < $languageVersion && !$method->isReturnTypeTentative, PHPEnum::class => fn (PHPEnum $class, PHPMethod $method, ?float $firstSinceVersion) => !$method->isFinal && !$class->isFinal && $firstSinceVersion !== null && $firstSinceVersion < $languageVersion && !$method->isReturnTypeTentative, - default => throw new \Exception("Unknown class type"), + default => throw new Exception("Unknown class type"), }; } @@ -247,7 +248,7 @@ private static function yieldFilteredMethods(string $classType, callable $filter PHPClass::class => PhpStormStubsSingleton::getPhpStormStubs()->getCoreClasses(), PHPInterface::class => PhpStormStubsSingleton::getPhpStormStubs()->getCoreInterfaces(), PHPEnum::class => PhpStormStubsSingleton::getPhpStormStubs()->getCoreEnums(), - default => throw new \Exception("Unknown class type") + default => throw new Exception("Unknown class type") }; $filtered = EntitiesFilter::getFiltered($classes); $array = array_filter(array_map(function ($class) use ($problemTypes, $filterFunction) { diff --git a/tests/TestData/Providers/Stubs/StubsParametersProvider.php b/tests/TestData/Providers/Stubs/StubsParametersProvider.php index 3016b9340..31bb3aff5 100644 --- a/tests/TestData/Providers/Stubs/StubsParametersProvider.php +++ b/tests/TestData/Providers/Stubs/StubsParametersProvider.php @@ -3,6 +3,7 @@ namespace StubTests\TestData\Providers\Stubs; +use Exception; use Generator; use StubTests\Model\PHPClass; use StubTests\Model\PHPEnum; @@ -129,7 +130,7 @@ private static function yieldFilteredMethodParameters(string $classType, callabl PHPClass::class => PhpStormStubsSingleton::getPhpStormStubs()->getCoreClasses(), PHPInterface::class => PhpStormStubsSingleton::getPhpStormStubs()->getCoreInterfaces(), PHPEnum::class => PhpStormStubsSingleton::getPhpStormStubs()->getCoreEnums(), - default => throw new \Exception("Unkown class type"), + default => throw new Exception("Unkown class type"), }; $filtered = EntitiesFilter::getFiltered($classes); $toYield = array_filter( @@ -184,7 +185,7 @@ private static function getFilterFunctionForLanguageLevel(string $classType, flo !$class->isFinal && $firstSinceVersion !== null && $firstSinceVersion < $languageVersion, PHPEnum::class => fn (PHPEnum $class, PHPMethod $method, ?float $firstSinceVersion) => !$method->isFinal && !$class->isFinal && $firstSinceVersion !== null && $firstSinceVersion < $languageVersion, - default => throw new \Exception("Unknown class type"), + default => throw new Exception("Unknown class type"), }; } }