Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 43 additions & 1 deletion src/Reflection/SignatureMap/NativeFunctionReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\AttributeReflection;
use PHPStan\Reflection\AttributeReflectionFactory;
use PHPStan\Reflection\ExtendedFunctionVariant;
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\Native\ExtendedNativeParameterReflection;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
use function array_key_exists;
use function array_map;
use function is_string;
use function str_contains;
use function strtolower;

Expand Down Expand Up @@ -160,6 +163,11 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
$hasSideEffects = TrinaryLogic::createMaybe();
}

$phpstanAttributes = $this->attributeReflectionFactory->fromNativeReflection($attributes, InitializerExprContext::fromFunction($realFunctionName, $fileName));
if ($reflectionFunctionAdapter !== null) {
$phpstanAttributes = $this->addDeprecatedSinceAttribute($reflectionFunctionAdapter, $phpstanAttributes);
}

$functionReflection = new NativeFunctionReflection(
$realFunctionName,
$variantsByType['positional'],
Expand All @@ -171,7 +179,7 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
$docComment,
$returnsByReference,
$acceptsNamedArguments,
$this->attributeReflectionFactory->fromNativeReflection($attributes, InitializerExprContext::fromFunction($realFunctionName, $fileName)),
$phpstanAttributes,
);
$this->functionMap[$lowerCasedFunctionName] = $functionReflection;

Expand Down Expand Up @@ -199,4 +207,38 @@ private static function getParamOutTypeFromPhpDoc(string $paramName, ResolvedPhp
return null;
}

/**
* @param list<AttributeReflection> $phpstanAttributes
* @return list<AttributeReflection>
*/
private function addDeprecatedSinceAttribute(ReflectionFunction $reflectionFunction, array $phpstanAttributes): array
{
foreach ($phpstanAttributes as $attr) {
if (strtolower($attr->getName()) === 'deprecated') {
return $phpstanAttributes;
}
}

foreach ($reflectionFunction->getAttributes() as $brAttr) {
if ($brAttr->getName() !== 'JetBrains\\PhpStorm\\Deprecated') {
continue;
}
$arguments = $brAttr->getArguments();
if (!isset($arguments['since']) || !is_string($arguments['since'])) {
continue;
}

$argTypes = ['since' => new ConstantStringType($arguments['since'])];
if (isset($arguments['message']) && is_string($arguments['message'])) {
$argTypes['message'] = new ConstantStringType($arguments['message']);
}

$phpstanAttributes[] = new AttributeReflection('Deprecated', $argTypes);

return $phpstanAttributes;
}

return $phpstanAttributes;
}

}
56 changes: 56 additions & 0 deletions src/Rules/RestrictedUsage/DeprecatedSinceVersionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\RestrictedUsage;

use Nette\Utils\Strings;
use PHPStan\Php\PhpVersions;
use PHPStan\Reflection\AttributeReflection;
use PHPStan\Type\IntegerRangeType;
use function sprintf;
use function strtolower;

final class DeprecatedSinceVersionHelper
{

/**
* @param list<AttributeReflection> $attributes
*/
public static function isScopeVersionBeforeDeprecation(array $attributes, PhpVersions $phpVersions): bool
{
$sinceVersionId = self::getDeprecatedSincePhpVersionId($attributes);
if ($sinceVersionId === null) {
return false;
}

return !IntegerRangeType::fromInterval($sinceVersionId, null)->isSuperTypeOf($phpVersions->getType())->yes();

Check warning on line 25 in src/Rules/RestrictedUsage/DeprecatedSinceVersionHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return false; } - return !IntegerRangeType::fromInterval($sinceVersionId, null)->isSuperTypeOf($phpVersions->getType())->yes(); + return IntegerRangeType::fromInterval($sinceVersionId, null)->isSuperTypeOf($phpVersions->getType())->no(); } /**

Check warning on line 25 in src/Rules/RestrictedUsage/DeprecatedSinceVersionHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return false; } - return !IntegerRangeType::fromInterval($sinceVersionId, null)->isSuperTypeOf($phpVersions->getType())->yes(); + return !$phpVersions->getType()->isSuperTypeOf(IntegerRangeType::fromInterval($sinceVersionId, null))->yes(); } /**

Check warning on line 25 in src/Rules/RestrictedUsage/DeprecatedSinceVersionHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return false; } - return !IntegerRangeType::fromInterval($sinceVersionId, null)->isSuperTypeOf($phpVersions->getType())->yes(); + return IntegerRangeType::fromInterval($sinceVersionId, null)->isSuperTypeOf($phpVersions->getType())->no(); } /**

Check warning on line 25 in src/Rules/RestrictedUsage/DeprecatedSinceVersionHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return false; } - return !IntegerRangeType::fromInterval($sinceVersionId, null)->isSuperTypeOf($phpVersions->getType())->yes(); + return !$phpVersions->getType()->isSuperTypeOf(IntegerRangeType::fromInterval($sinceVersionId, null))->yes(); } /**
}

/**
* @param list<AttributeReflection> $attributes
*/
private static function getDeprecatedSincePhpVersionId(array $attributes): ?int
{
foreach ($attributes as $attribute) {
if (strtolower($attribute->getName()) !== 'deprecated') {
continue;
}
$argumentTypes = $attribute->getArgumentTypes();
if (!isset($argumentTypes['since'])) {
continue;
}
$sinceType = $argumentTypes['since'];
foreach ($sinceType->getConstantStrings() as $constantString) {
$matches = Strings::match($constantString->getValue(), '#^(\d+)\.(\d+)(?:\.(\d+))?$#');
if ($matches !== null) {
$major = (int) $matches[1];
$minor = (int) $matches[2];
$patch = (int) ($matches[3] ?? 0);
return (int) sprintf('%d%02d%02d', $major, $minor, $patch);
}
}
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function str_ends_with;

/**
* @implements Rule<Node\Expr\ClassConstFetch>
Expand Down Expand Up @@ -91,6 +92,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($constantReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

deprecations is only one use-case of Restricted* classes. we should not mix this use-case into the this abstract base-classes

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That was the initial make phpstan run which failed due to the dead method detection. The re-run after adding @api passed cleanly, and the commit has been pushed.


if ($classReflection->getName() !== $constantReflection->getDeclaringClass()->getName()) {
$rewrittenConstantReflection = new RewrittenDeclaringClassClassConstantReflection($classReflection, $constantReflection);
$rewrittenRestrictedUsage = $extension->isRestrictedClassConstantUsage($rewrittenConstantReflection, $scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function str_ends_with;

/**
* @implements Rule<FunctionCallableNode>
Expand Down Expand Up @@ -59,6 +60,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($functionReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}

$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
->identifier($restrictedUsage->identifier)
->build();
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/RestrictedUsage/RestrictedFunctionUsageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function str_ends_with;

/**
* @implements Rule<Node\Expr\FuncCall>
Expand Down Expand Up @@ -58,6 +59,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($functionReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}

$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
->identifier($restrictedUsage->identifier)
->build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function str_ends_with;

/**
* @implements Rule<MethodCallableNode>
Expand Down Expand Up @@ -72,6 +73,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($methodReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}

$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
->identifier($restrictedUsage->identifier)
->build();
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/RestrictedUsage/RestrictedMethodUsageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function str_ends_with;

/**
* @implements Rule<MethodCall>
Expand Down Expand Up @@ -72,6 +73,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($methodReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}

$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
->identifier($restrictedUsage->identifier)
->build();
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/RestrictedUsage/RestrictedPropertyUsageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function str_ends_with;

/**
* @implements Rule<Node\Expr\PropertyFetch>
Expand Down Expand Up @@ -71,6 +72,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($propertyReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}

$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
->identifier($restrictedUsage->identifier)
->build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function str_ends_with;

/**
* @implements Rule<StaticMethodCallableNode>
Expand Down Expand Up @@ -92,6 +93,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($methodReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}

if ($classReflection->getName() !== $methodReflection->getDeclaringClass()->getName()) {
$rewrittenMethodReflection = new RewrittenDeclaringClassMethodReflection($classReflection, $methodReflection);
$rewrittenRestrictedUsage = $extension->isRestrictedMethodUsage($rewrittenMethodReflection, $scope);
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/RestrictedUsage/RestrictedStaticMethodUsageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function str_ends_with;

/**
* @implements Rule<Node\Expr\StaticCall>
Expand Down Expand Up @@ -91,6 +92,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($methodReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}

if ($classReflection->getName() !== $methodReflection->getDeclaringClass()->getName()) {
$rewrittenMethodReflection = new RewrittenDeclaringClassMethodReflection($classReflection, $methodReflection);
$rewrittenRestrictedUsage = $extension->isRestrictedMethodUsage($rewrittenMethodReflection, $scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function str_ends_with;

/**
* @implements Rule<Node\Expr\StaticPropertyFetch>
Expand Down Expand Up @@ -91,6 +92,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (
str_ends_with($restrictedUsage->identifier, '.deprecated')
&& DeprecatedSinceVersionHelper::isScopeVersionBeforeDeprecation($propertyReflection->getAttributes(), $scope->getPhpVersion())
) {
continue;
}

if ($classReflection->getName() !== $propertyReflection->getDeclaringClass()->getName()) {
$rewrittenPropertyReflection = new RewrittenDeclaringClassPropertyReflection($classReflection, $propertyReflection);
$rewrittenRestrictedUsage = $extension->isRestrictedPropertyUsage($rewrittenPropertyReflection, $scope);
Expand Down
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/RestrictedUsage/Bug14366Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\RestrictedUsage;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<RestrictedFunctionUsageRule>
*/
class Bug14366Test extends RuleTestCase
{

protected function getRule(): Rule
{
return new RestrictedFunctionUsageRule(
self::getContainer(),
self::createReflectionProvider(),
);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/bug-14366.php'], [
[
'Call to deprecated function curl_close().',
28,
],
]);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/bug-14366.neon',
...parent::getAdditionalConfigFiles(),
];
}

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/RestrictedUsage/bug-14366.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
phpVersion: 80500

includes:
- ../../../../vendor/phpstan/phpstan-deprecation-rules/rules.neon
Loading
Loading