forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeprecatedSinceVersionHelper.php
More file actions
57 lines (49 loc) · 1.5 KB
/
DeprecatedSinceVersionHelper.php
File metadata and controls
57 lines (49 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}
}