Skip to content

Commit

Permalink
cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isfedorov committed Aug 31, 2024
1 parent 6fec1fa commit 8c9a279
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 89 deletions.
4 changes: 0 additions & 4 deletions tests/AbstractBaseStubsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Model/BasePHPClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 7 additions & 23 deletions tests/Model/Tags/RemovedTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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";
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Parsers/ExpectedFunctionArgumentsInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
9 changes: 3 additions & 6 deletions tests/Parsers/MetaExpectedArgumentsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -45,9 +45,6 @@ public function __construct()
);
}

/**
* @throws RuntimeException
*/
public function enterNode(Node $node): void
{
if (property_exists($node, 'expr') && $node->expr instanceof FuncCall) {
Expand Down
20 changes: 1 addition & 19 deletions tests/Parsers/ParserUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -118,7 +108,6 @@ private static function getLatestAvailableVersionFromPhpDoc(BasePHPElement $elem

/**
* @return float[]
* @throws RuntimeException
*/
private static function getSinceVersionsFromParentClass(PHPMethod|PHPClassConstant $element): array
{
Expand All @@ -140,7 +129,6 @@ private static function getSinceVersionsFromParentClass(PHPMethod|PHPClassConsta

/**
* @return float[]
* @throws RuntimeException
*/
public static function getLatestAvailableVersionsFromParentClass(PHPMethod|PHPClassConstant $element): array
{
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Parsers/StubParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
31 changes: 10 additions & 21 deletions tests/Parsers/Visitors/ASTVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace StubTests\Parsers\Visitors;

use Exception;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Class_;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -73,43 +68,43 @@ 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;
}
$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;
Expand All @@ -118,9 +113,6 @@ public function enterNode(Node $node): void
}
}

/**
* @throws RuntimeException
*/
public function combineParentInterfaces(PHPInterface $interface): array
{
$parents = [];
Expand Down Expand Up @@ -149,9 +141,6 @@ public function combineParentInterfaces(PHPInterface $interface): array
return $parents;
}

/**
* @throws RuntimeException
*/
public function combineImplementedInterfaces(PHPClass $class): array
{
$interfaces = [];
Expand Down
1 change: 0 additions & 1 deletion tests/Parsers/Visitors/CoreStubASTVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class CoreStubASTVisitor extends ASTVisitor
{
#[Pure]
public function __construct(StubsContainer $stubs, array &$entitiesToUpdate)
{
parent::__construct($stubs, $entitiesToUpdate);
Expand Down
5 changes: 1 addition & 4 deletions tests/Parsers/Visitors/MetaOverrideFunctionsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class MetaOverrideFunctionsParser extends NodeVisitorAbstract
{
private const OVERRIDE_FUNCTION = 'override';
private const string OVERRIDE_FUNCTION = 'override';

/**
* @var string[]
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tests/TestData/Providers/EntitiesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace StubTests\TestData\Providers;

use Exception;
use RuntimeException;
use StubTests\Model\PHPClass;
use StubTests\Model\PHPEnum;
Expand Down Expand Up @@ -41,7 +42,6 @@ public static function getFiltered(array $entities, ?callable $additionalFilter

/**
* @return PHPFunction[]
* @throws RuntimeException
*/
public static function getFilteredStubsFunctions(bool $shouldSuitCurrentPhpVersion = true): array
{
Expand Down Expand Up @@ -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"),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 8c9a279

Please sign in to comment.