Skip to content

Commit aa83cc0

Browse files
authored
Merge pull request #11625 from hschletz/6.x
Remove redefinition of Attribute constants.
2 parents 903a66b + f1fdc52 commit aa83cc0

File tree

5 files changed

+24
-29
lines changed

5 files changed

+24
-29
lines changed

src/Psalm/Internal/Analyzer/AttributesAnalyzer.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Psalm\Internal\Analyzer;
66

7+
use Attribute as GlobalAttribute;
78
use Generator;
89
use PhpParser\Node\Arg;
910
use PhpParser\Node\Attribute;
@@ -47,16 +48,6 @@ final class AttributesAnalyzer
4748
40 => 'promoted property',
4849
];
4950

50-
// Copied from Attribute class since that class might not exist at runtime
51-
public const TARGET_CLASS = 1;
52-
public const TARGET_FUNCTION = 2;
53-
public const TARGET_METHOD = 4;
54-
public const TARGET_PROPERTY = 8;
55-
public const TARGET_CLASS_CONSTANT = 16;
56-
public const TARGET_PARAMETER = 32;
57-
public const TARGET_ALL = 63;
58-
public const IS_REPEATABLE = 64;
59-
6051
/**
6152
* @param array<array-key, AttributeGroup> $attribute_groups
6253
* @param key-of<self::TARGET_DESCRIPTIONS> $target
@@ -104,7 +95,7 @@ public static function analyze(
10495
$storage instanceof ClassLikeStorage ? $storage : null,
10596
);
10697

107-
if (($attribute_class_flags & self::IS_REPEATABLE) === 0) {
98+
if (($attribute_class_flags & GlobalAttribute::IS_REPEATABLE) === 0) {
10899
// Not IS_REPEATABLE
109100
if (isset($appearing_non_repeatable_attributes[$fq_attribute_name])) {
110101
IssueBuffer::maybeAdd(
@@ -251,17 +242,17 @@ private static function getAttributeClassFlags(
251242
if (strtolower($fq_attribute_name) === "attribute") {
252243
// We override this here because we still want to analyze attributes
253244
// for PHP 7.4 when the Attribute class doesn't yet exist.
254-
return self::TARGET_CLASS;
245+
return GlobalAttribute::TARGET_CLASS;
255246
}
256247

257248
if ($attribute_class_storage === null) {
258-
return self::TARGET_ALL; // Defaults to TARGET_ALL
249+
return GlobalAttribute::TARGET_ALL; // Defaults to TARGET_ALL
259250
}
260251

261252
foreach ($attribute_class_storage->attributes as $attribute_attribute) {
262253
if ($attribute_attribute->fq_class_name === 'Attribute') {
263254
if (!$attribute_attribute->args) {
264-
return self::TARGET_ALL; // Defaults to TARGET_ALL
255+
return GlobalAttribute::TARGET_ALL; // Defaults to TARGET_ALL
265256
}
266257

267258
$first_arg = $attribute_attribute->args[array_key_first($attribute_attribute->args)];
@@ -279,7 +270,7 @@ private static function getAttributeClassFlags(
279270
}
280271

281272
if (!$first_arg_type->isSingleIntLiteral()) {
282-
return self::TARGET_ALL; // Fall back to default if it's invalid
273+
return GlobalAttribute::TARGET_ALL; // Fall back to default if it's invalid
283274
}
284275

285276
return $first_arg_type->getSingleIntLiteral()->value;
@@ -294,7 +285,7 @@ private static function getAttributeClassFlags(
294285
$suppressed_issues,
295286
);
296287

297-
return self::TARGET_ALL; // Fall back to default if it's invalid
288+
return GlobalAttribute::TARGET_ALL; // Fall back to default if it's invalid
298289
}
299290

300291
/**
@@ -329,22 +320,22 @@ public static function analyzeGetAttributes(
329320

330321
switch ($method_id) {
331322
case "ReflectionClass::getattributes":
332-
$target = self::TARGET_CLASS;
323+
$target = GlobalAttribute::TARGET_CLASS;
333324
break;
334325
case "ReflectionFunction::getattributes":
335-
$target = self::TARGET_FUNCTION;
326+
$target = GlobalAttribute::TARGET_FUNCTION;
336327
break;
337328
case "ReflectionMethod::getattributes":
338-
$target = self::TARGET_METHOD;
329+
$target = GlobalAttribute::TARGET_METHOD;
339330
break;
340331
case "ReflectionProperty::getattributes":
341-
$target = self::TARGET_PROPERTY;
332+
$target = GlobalAttribute::TARGET_PROPERTY;
342333
break;
343334
case "ReflectionClassConstant::getattributes":
344-
$target = self::TARGET_CLASS_CONSTANT;
335+
$target = GlobalAttribute::TARGET_CLASS_CONSTANT;
345336
break;
346337
case "ReflectionParameter::getattributes":
347-
$target = self::TARGET_PARAMETER;
338+
$target = GlobalAttribute::TARGET_PARAMETER;
348339
break;
349340
default:
350341
return;

src/Psalm/Internal/Analyzer/ClassAnalyzer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Psalm\Internal\Analyzer;
66

7+
use Attribute;
78
use Exception;
89
use InvalidArgumentException;
910
use LogicException;
@@ -426,7 +427,7 @@ public function analyze(
426427
$class_context,
427428
$storage,
428429
$class->attrGroups,
429-
AttributesAnalyzer::TARGET_CLASS,
430+
Attribute::TARGET_CLASS,
430431
$storage->suppressed_issues + $this->getSuppressedIssues(),
431432
);
432433

@@ -1567,7 +1568,7 @@ private function analyzeProperty(
15671568
$context,
15681569
$property_storage,
15691570
$stmt->attrGroups,
1570-
AttributesAnalyzer::TARGET_PROPERTY,
1571+
Attribute::TARGET_PROPERTY,
15711572
$property_storage->suppressed_issues + $this->getSuppressedIssues(),
15721573
);
15731574

src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Psalm\Internal\Analyzer;
66

7+
use Attribute;
78
use Override;
89
use PhpParser;
910
use PhpParser\Node\Expr\ArrowFunction;
@@ -916,7 +917,7 @@ public function analyze(
916917
$context,
917918
$storage,
918919
$this->function->attrGroups,
919-
$storage instanceof MethodStorage ? AttributesAnalyzer::TARGET_METHOD : AttributesAnalyzer::TARGET_FUNCTION,
920+
$storage instanceof MethodStorage ? Attribute::TARGET_METHOD : Attribute::TARGET_FUNCTION,
920921
$storage->suppressed_issues + $this->getSuppressedIssues(),
921922
);
922923

@@ -1367,8 +1368,8 @@ private function processParams(
13671368
$context,
13681369
$function_param,
13691370
$param_stmts[$offset]->attrGroups,
1370-
AttributesAnalyzer::TARGET_PARAMETER
1371-
| ($function_param->promoted_property ? AttributesAnalyzer::TARGET_PROPERTY : 0),
1371+
Attribute::TARGET_PARAMETER
1372+
| ($function_param->promoted_property ? Attribute::TARGET_PROPERTY : 0),
13721373
$storage->suppressed_issues + $this->getSuppressedIssues(),
13731374
);
13741375
}

src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Psalm\Internal\Analyzer;
66

7+
use Attribute;
78
use InvalidArgumentException;
89
use LogicException;
910
use PhpParser;
@@ -146,7 +147,7 @@ public function analyze(): void
146147
$interface_context,
147148
$class_storage,
148149
$this->class->attrGroups,
149-
AttributesAnalyzer::TARGET_CLASS,
150+
Attribute::TARGET_CLASS,
150151
$class_storage->suppressed_issues + $this->getSuppressedIssues(),
151152
);
152153

src/Psalm/Internal/Analyzer/TraitAnalyzer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Psalm\Internal\Analyzer;
66

7+
use Attribute;
78
use Override;
89
use PhpParser\Node\Stmt\Trait_;
910
use Psalm\Aliases;
@@ -81,7 +82,7 @@ public static function analyze(StatementsAnalyzer $statements_analyzer, Trait_ $
8182
$context,
8283
$storage,
8384
$stmt->attrGroups,
84-
AttributesAnalyzer::TARGET_CLASS,
85+
Attribute::TARGET_CLASS,
8586
$storage->suppressed_issues + $statements_analyzer->getSuppressedIssues(),
8687
);
8788

0 commit comments

Comments
 (0)