Skip to content
Closed
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
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;
}

}
57 changes: 57 additions & 0 deletions src/Rules/RestrictedUsage/DeprecatedSinceVersionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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
{

/**
* @api
* @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();
}

/**
* @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;
}

}
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
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/RestrictedUsage/data/bug-14366.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace Bug14366;

/** @param non-empty-string $uri */
function retrieve($uri): void
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
if (false === $response) {
throw new \Exception('JSON schema not found');
}


if (PHP_VERSION_ID < 80000) {
curl_close($ch);
}
}

function noGuard(): void
{
$ch = curl_init();
curl_close($ch);
}
Loading