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

Always require PHP version for PhpStormStubsSourceStubber - PHP_VERSION_ID as default #874

Merged
merged 2 commits into from
Nov 23, 2021
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
12 changes: 3 additions & 9 deletions src/SourceLocator/SourceStubber/PhpStormStubsSourceStubber.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
use function str_replace;
use function strtolower;

use const PHP_VERSION_ID;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, this uses "the PHP version BR is running under", rather than "the PHP version of the code being analysed" as the default? What is the mechanism for overriding this? e.g. if we are using PHP 8.0 to analyse a codebase that runs on PHP 7.4?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* @internal
*/
Expand Down Expand Up @@ -164,7 +166,7 @@ final class PhpStormStubsSourceStubber implements SourceStubber
/** @var array<lowercase-string, string> */
private static array $constantMap;

public function __construct(private Parser $phpParser, private ?int $phpVersion = null)
public function __construct(private Parser $phpParser, private int $phpVersion = PHP_VERSION_ID)
{
$this->builderFactory = new BuilderFactory();
$this->prettyPrinter = new Standard(self::BUILDER_OPTIONS);
Expand Down Expand Up @@ -425,10 +427,6 @@ private function isDeprecatedInPhpVersion(Node\Stmt\ClassLike|Node\Stmt\ClassCon
continue;
}

if ($this->phpVersion === null) {
return true;
}

foreach ($attributeNode->args as $attributeArg) {
if ($attributeArg->name?->toString() === 'since') {
assert($attributeArg->value instanceof Node\Scalar\String_);
Expand All @@ -448,10 +446,6 @@ private function isSupportedInPhpVersion(
Node\Stmt\ClassLike|Node\Stmt\Function_|Node\Stmt\Const_|Node\Expr\FuncCall|Node\Stmt\ClassConst|Node\Stmt\Property|Node\Stmt\ClassMethod $node,
bool $isCoreExtension,
): bool {
if ($this->phpVersion === null) {
return true;
}

// "@since" and "@removed" annotations in some cases do not contain a PHP version, but an extension version - e.g. "@since 1.3.0"
if (! $isCoreExtension) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function testToString(): void
);
}

/**
* @requires PHP >= 8.1
*/
public function testPureEnumToString(): void
{
$reflector = new DefaultReflector(new AggregateSourceLocator([
Expand All @@ -66,6 +69,9 @@ public function testPureEnumToString(): void
);
}

/**
* @requires PHP >= 8.1
*/
public function testBackedEnumToString(): void
{
$reflector = new DefaultReflector(new AggregateSourceLocator([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,6 @@ public function testUpdateConstantValue(): void
public function dataClassInPhpVersion(): array
{
return [
[CoreReflectionNamedType::class, null, true],
[CoreReflectionNamedType::class, 70000, false],
[CoreReflectionNamedType::class, 70100, true],
[CoreReflectionNamedType::class, 70200, true],
Expand All @@ -769,7 +768,7 @@ public function dataClassInPhpVersion(): array
/**
* @dataProvider dataClassInPhpVersion
*/
public function testClassInPhpVersion(string $className, ?int $phpVersion, bool $isSupported): void
public function testClassInPhpVersion(string $className, int $phpVersion, bool $isSupported): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);

Expand All @@ -785,7 +784,6 @@ public function testClassInPhpVersion(string $className, ?int $phpVersion, bool
public function dataClassConstantInPhpVersion(): array
{
return [
[DateTimeInterface::class, 'ATOM', null, true],
[DateTimeInterface::class, 'ATOM', 70200, true],
[DateTimeInterface::class, 'ATOM', 70100, false],
[DateTime::class, 'ATOM', 70100, true],
Expand All @@ -801,7 +799,7 @@ public function dataClassConstantInPhpVersion(): array
/**
* @dataProvider dataClassConstantInPhpVersion
*/
public function testClassConstantInPhpVersion(string $className, string $constantName, ?int $phpVersion, bool $isSupported): void
public function testClassConstantInPhpVersion(string $className, string $constantName, int $phpVersion, bool $isSupported): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);
$phpInternalSourceLocator = new PhpInternalSourceLocator($this->astLocator, $sourceStubber);
Expand All @@ -815,7 +813,6 @@ public function testClassConstantInPhpVersion(string $className, string $constan
public function dataMethodInPhpVersion(): array
{
return [
[CoreReflectionProperty::class, 'hasType', null, true],
[CoreReflectionProperty::class, 'hasType', 70400, true],
[CoreReflectionProperty::class, 'hasType', 70300, false],
[CoreReflectionProperty::class, 'getType', 70400, true],
Expand All @@ -832,7 +829,7 @@ public function dataMethodInPhpVersion(): array
/**
* @dataProvider dataMethodInPhpVersion
*/
public function testMethodInPhpVersion(string $className, string $methodName, ?int $phpVersion, bool $isSupported): void
public function testMethodInPhpVersion(string $className, string $methodName, int $phpVersion, bool $isSupported): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);
$sourceLocator = new AggregateSourceLocator([
Expand All @@ -848,17 +845,16 @@ public function testMethodInPhpVersion(string $className, string $methodName, ?i
public function dataPropertyInPhpVersion(): array
{
return [
[DateInterval::class, 'f', null, true],
[DateInterval::class, 'f', 70100, true],
[DateInterval::class, 'f', 70000, false],
[DateInterval::class, 'f', 70099, false],
[DateInterval::class, 'f', 70100, true],
];
}

/**
* @dataProvider dataPropertyInPhpVersion
*/
public function testPropertyInPhpVersion(string $className, string $propertyName, ?int $phpVersion, bool $isSupported): void
public function testPropertyInPhpVersion(string $className, string $propertyName, int $phpVersion, bool $isSupported): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);
$phpInternalSourceLocator = new PhpInternalSourceLocator($this->astLocator, $sourceStubber);
Expand All @@ -870,7 +866,6 @@ public function testPropertyInPhpVersion(string $className, string $propertyName
public function dataFunctionInPhpVersion(): array
{
return [
['password_algos', null, true],
['password_algos', 70400, true],
['password_algos', 70300, false],
['array_key_first', 70300, true],
Expand All @@ -891,7 +886,7 @@ public function dataFunctionInPhpVersion(): array
/**
* @dataProvider dataFunctionInPhpVersion
*/
public function testFunctionInPhpVersion(string $functionName, ?int $phpVersion, bool $isSupported): void
public function testFunctionInPhpVersion(string $functionName, int $phpVersion, bool $isSupported): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);

Expand Down Expand Up @@ -931,7 +926,6 @@ public function testFunctionWithDifferentParameterInPhpVersion80(): void
public function dataConstantInPhpVersion(): array
{
return [
['PHP_OS_FAMILY', null, true],
['PHP_OS_FAMILY', 70200, true],
['PHP_OS_FAMILY', 70100, false],
['INPUT_SESSION', 70400, true],
Expand All @@ -940,15 +934,15 @@ public function dataConstantInPhpVersion(): array
['CURL_VERSION_ALTSVC', 70306, true],
['CURL_VERSION_ALTSVC', 70305, false],
// Not core constants
['RADIUS_DISCONNECT_ACK', null, true],
['RADIUS_DISCONNECT_ACK', 10000, true],
['RADIUS_DISCONNECT_ACK', 80000, true],
];
}

/**
* @dataProvider dataConstantInPhpVersion
*/
public function testConstantInPhpVersion(string $constantName, ?int $phpVersion, bool $isSupported): void
public function testConstantInPhpVersion(string $constantName, int $phpVersion, bool $isSupported): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);

Expand All @@ -964,17 +958,17 @@ public function testConstantInPhpVersion(string $constantName, ?int $phpVersion,
public function dataClassIsDeprecatedInPhpVersion(): array
{
return [
['Mongo', null, true],
['Mongo', 70000, true],
['Mongo', 80000, true],
[DateTimeInterface::class, null, false],
[DateTimeInterface::class, 70400, false],
[DateTimeInterface::class, 80000, false],
];
}

/**
* @dataProvider dataClassIsDeprecatedInPhpVersion
*/
public function testClassIsDeprecatedInPhpVersion(string $className, ?int $phpVersion, bool $isDeprecated): void
public function testClassIsDeprecatedInPhpVersion(string $className, int $phpVersion, bool $isDeprecated): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);
$reflector = new DefaultReflector(new PhpInternalSourceLocator($this->astLocator, $sourceStubber));
Expand All @@ -987,17 +981,17 @@ public function testClassIsDeprecatedInPhpVersion(string $className, ?int $phpVe
public function dataClassConstantIsDeprecatedInPhpVersion(): array
{
return [
['PDO', 'PARAM_BOOL', null, false],
['PDO', 'PARAM_BOOL', 70000, false],
['PDO', 'PARAM_BOOL', 80000, false],
['PDO', 'PGSQL_ASSOC', null, true],
['PDO', 'PGSQL_ASSOC', 70000, true],
['PDO', 'PGSQL_ASSOC', 80000, true],
];
}

/**
* @dataProvider dataClassConstantIsDeprecatedInPhpVersion
*/
public function testClassConstantIsDeprecatedInPhpVersion(string $className, string $constantName, ?int $phpVersion, bool $isDeprecated): void
public function testClassConstantIsDeprecatedInPhpVersion(string $className, string $constantName, int $phpVersion, bool $isDeprecated): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);
$reflector = new DefaultReflector(new PhpInternalSourceLocator($this->astLocator, $sourceStubber));
Expand All @@ -1011,19 +1005,17 @@ public function testClassConstantIsDeprecatedInPhpVersion(string $className, str
public function dataMethodIsDeprecatedInPhpVersion(): array
{
return [
[CoreReflectionClass::class, 'getName', null, false],
[CoreReflectionClass::class, 'getName', 70400, false],
[CoreReflectionClass::class, 'export', null, true],
[CoreReflectionClass::class, 'export', 70400, true],
[CoreReflectionClass::class, 'export', 70300, false],
[CoreReflectionClass::class, 'export', 70399, false],
[CoreReflectionClass::class, 'export', 70400, true],
];
}

/**
* @dataProvider dataMethodIsDeprecatedInPhpVersion
*/
public function testMethodIsDeprecatedInPhpVersion(string $className, string $methodName, ?int $phpVersion, bool $isDeprecated): void
public function testMethodIsDeprecatedInPhpVersion(string $className, string $methodName, int $phpVersion, bool $isDeprecated): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);
$sourceLocator = new AggregateSourceLocator([
Expand All @@ -1042,17 +1034,17 @@ public function testMethodIsDeprecatedInPhpVersion(string $className, string $me
public function dataPropertyIsDeprecatedInPhpVersion(): array
{
return [
['DateInterval', 'y', null, false],
['DateInterval', 'y', 70000, false],
['DateInterval', 'y', 80000, false],
['DOMDocument', 'actualEncoding', null, true],
['DOMDocument', 'actualEncoding', 70000, true],
['DOMDocument', 'actualEncoding', 80000, true],
];
}

/**
* @dataProvider dataPropertyIsDeprecatedInPhpVersion
*/
public function testPropertyIsDeprecatedInPhpVersion(string $className, string $propertyName, ?int $phpVersion, bool $isDeprecated): void
public function testPropertyIsDeprecatedInPhpVersion(string $className, string $propertyName, int $phpVersion, bool $isDeprecated): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);
$reflector = new DefaultReflector(new PhpInternalSourceLocator($this->astLocator, $sourceStubber));
Expand All @@ -1066,19 +1058,18 @@ public function testPropertyIsDeprecatedInPhpVersion(string $className, string $
public function dataFunctionIsDeprecatedInPhpVersion(): array
{
return [
['strpos', null, false],
['strpos', 70000, false],
['strpos', 80000, false],
['create_function', null, true],
['create_function', 70200, true],
['create_function', 70100, false],
['create_function', 70199, false],
['create_function', 70200, true],
];
}

/**
* @dataProvider dataFunctionIsDeprecatedInPhpVersion
*/
public function testFunctionIsDeprecatedInPhpVersion(string $functionName, ?int $phpVersion, bool $isDeprecated): void
public function testFunctionIsDeprecatedInPhpVersion(string $functionName, int $phpVersion, bool $isDeprecated): void
{
$sourceStubber = new PhpStormStubsSourceStubber($this->phpParser, $phpVersion);
$reflector = new DefaultReflector(new PhpInternalSourceLocator($this->astLocator, $sourceStubber));
Expand Down