Skip to content

Commit 95bea51

Browse files
zonuexeondrejmirtes
authored andcommitted
Use const instead of properties
1 parent 256ff3d commit 95bea51

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

src/Type/Php/ArgumentBasedFunctionReturnTypeExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
use PHPStan\Type\GeneralizePrecision;
1212
use PHPStan\Type\Type;
1313
use PHPStan\Type\TypeCombinator;
14+
use function array_key_exists;
1415

1516
class ArgumentBasedFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
1617
{
1718

18-
/** @var int[] */
19-
private array $functionNames = [
19+
private const FUNCTION_NAMES = [
2020
'array_unique' => 0,
2121
'array_change_key_case' => 0,
2222
'array_diff_assoc' => 0,
@@ -38,12 +38,12 @@ class ArgumentBasedFunctionReturnTypeExtension implements DynamicFunctionReturnT
3838

3939
public function isFunctionSupported(FunctionReflection $functionReflection): bool
4040
{
41-
return isset($this->functionNames[$functionReflection->getName()]);
41+
return array_key_exists($functionReflection->getName(), self::FUNCTION_NAMES);
4242
}
4343

4444
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
4545
{
46-
$argumentPosition = $this->functionNames[$functionReflection->getName()];
46+
$argumentPosition = self::FUNCTION_NAMES[$functionReflection->getName()];
4747

4848
if (!isset($functionCall->getArgs()[$argumentPosition])) {
4949
return null;

src/Type/Php/JsonThrowTypeExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
class JsonThrowTypeExtension implements DynamicFunctionThrowTypeExtension
1717
{
1818

19-
/** @var array<string, int> */
20-
private array $argumentPositions = [
19+
private const ARGUMENTS_POSITIONS = [
2120
'json_encode' => 1,
2221
'json_decode' => 3,
2322
];
@@ -49,7 +48,7 @@ public function getThrowTypeFromFunctionCall(
4948
Scope $scope,
5049
): ?Type
5150
{
52-
$argumentPosition = $this->argumentPositions[$functionReflection->getName()];
51+
$argumentPosition = self::ARGUMENTS_POSITIONS[$functionReflection->getName()];
5352
if (!isset($functionCall->getArgs()[$argumentPosition])) {
5453
return null;
5554
}

src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
class ReplaceFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
2222
{
2323

24-
/** @var array<string, int> */
25-
private array $functionsSubjectPosition = [
24+
private const FUNCTIONS_SUBJECT_POSITION = [
2625
'preg_replace' => 2,
2726
'preg_replace_callback' => 2,
2827
'preg_replace_callback_array' => 1,
@@ -32,8 +31,7 @@ class ReplaceFunctionsDynamicReturnTypeExtension implements DynamicFunctionRetur
3231
'strtr' => 0,
3332
];
3433

35-
/** @var array<string, int> */
36-
private array $functionsReplacePosition = [
34+
private const FUNCTIONS_REPLACE_POSITION = [
3735
'preg_replace' => 1,
3836
'str_replace' => 1,
3937
'str_ireplace' => 1,
@@ -43,7 +41,7 @@ class ReplaceFunctionsDynamicReturnTypeExtension implements DynamicFunctionRetur
4341

4442
public function isFunctionSupported(FunctionReflection $functionReflection): bool
4543
{
46-
return array_key_exists($functionReflection->getName(), $this->functionsSubjectPosition);
44+
return array_key_exists($functionReflection->getName(), self::FUNCTIONS_SUBJECT_POSITION);
4745
}
4846

4947
public function getTypeFromFunctionCall(
@@ -75,7 +73,7 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
7573
Scope $scope,
7674
): Type
7775
{
78-
$argumentPosition = $this->functionsSubjectPosition[$functionReflection->getName()];
76+
$argumentPosition = self::FUNCTIONS_SUBJECT_POSITION[$functionReflection->getName()];
7977
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
8078
$scope,
8179
$functionCall->getArgs(),
@@ -91,8 +89,8 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
9189
return TypeUtils::toBenevolentUnion($defaultReturnType);
9290
}
9391

94-
if ($subjectArgumentType->isNonEmptyString()->yes() && array_key_exists($functionReflection->getName(), $this->functionsReplacePosition)) {
95-
$replaceArgumentPosition = $this->functionsReplacePosition[$functionReflection->getName()];
92+
if ($subjectArgumentType->isNonEmptyString()->yes() && array_key_exists($functionReflection->getName(), self::FUNCTIONS_REPLACE_POSITION)) {
93+
$replaceArgumentPosition = self::FUNCTIONS_REPLACE_POSITION[$functionReflection->getName()];
9694

9795
if (count($functionCall->getArgs()) > $replaceArgumentPosition) {
9896
$replaceArgumentType = $scope->getType($functionCall->getArgs()[$replaceArgumentPosition]->value);

src/Type/Php/StrContainingTypeSpecifyingExtension.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
final class StrContainingTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
2929
{
3030

31-
/** @var array<string, array{0: int, 1: int}> */
32-
private array $strContainingFunctions = [
31+
private const STR_CONTAINING_FUNCTIONS = [
3332
'fnmatch' => [1, 0],
3433
'str_contains' => [0, 1],
3534
'str_starts_with' => [0, 1],
@@ -50,7 +49,7 @@ public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
5049

5150
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
5251
{
53-
return array_key_exists(strtolower($functionReflection->getName()), $this->strContainingFunctions)
52+
return array_key_exists(strtolower($functionReflection->getName()), self::STR_CONTAINING_FUNCTIONS)
5453
&& $context->true();
5554
}
5655

@@ -59,7 +58,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
5958
$args = $node->getArgs();
6059

6160
if (count($args) >= 2) {
62-
[$hackstackArg, $needleArg] = $this->strContainingFunctions[strtolower($functionReflection->getName())];
61+
[$hackstackArg, $needleArg] = self::STR_CONTAINING_FUNCTIONS[strtolower($functionReflection->getName())];
6362

6463
$haystackType = $scope->getType($args[$hackstackArg]->value);
6564
$needleType = $scope->getType($args[$needleArg]->value);

0 commit comments

Comments
 (0)