Skip to content

Commit

Permalink
Use const instead of properties
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe committed Dec 23, 2023
1 parent 981c7ba commit 5ea900c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/Type/Php/ArgumentBasedFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_key_exists;

class ArgumentBasedFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

/** @var int[] */
private array $functionNames = [
private const FUNCTION_NAMES = [
'array_unique' => 0,
'array_change_key_case' => 0,
'array_diff_assoc' => 0,
Expand All @@ -38,12 +38,12 @@ class ArgumentBasedFunctionReturnTypeExtension implements DynamicFunctionReturnT

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

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

if (!isset($functionCall->getArgs()[$argumentPosition])) {
return null;
Expand Down
5 changes: 2 additions & 3 deletions src/Type/Php/JsonThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
class JsonThrowTypeExtension implements DynamicFunctionThrowTypeExtension
{

/** @var array<string, int> */
private array $argumentPositions = [
private const ARGUMENTS_POSITIONS = [
'json_encode' => 1,
'json_decode' => 3,
];
Expand Down Expand Up @@ -49,7 +48,7 @@ public function getThrowTypeFromFunctionCall(
Scope $scope,
): ?Type
{
$argumentPosition = $this->argumentPositions[$functionReflection->getName()];
$argumentPosition = self::ARGUMENTS_POSITIONS[$functionReflection->getName()];
if (!isset($functionCall->getArgs()[$argumentPosition])) {
return null;
}
Expand Down
14 changes: 6 additions & 8 deletions src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
class ReplaceFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

/** @var array<string, int> */
private array $functionsSubjectPosition = [
private const FUNCTIONS_SUBJECT_POSITION = [
'preg_replace' => 2,
'preg_replace_callback' => 2,
'preg_replace_callback_array' => 1,
Expand All @@ -32,8 +31,7 @@ class ReplaceFunctionsDynamicReturnTypeExtension implements DynamicFunctionRetur
'strtr' => 0,
];

/** @var array<string, int> */
private array $functionsReplacePosition = [
private const FUNCTIONS_REPLACE_POSITION = [
'preg_replace' => 1,
'str_replace' => 1,
'str_ireplace' => 1,
Expand All @@ -43,7 +41,7 @@ class ReplaceFunctionsDynamicReturnTypeExtension implements DynamicFunctionRetur

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return array_key_exists($functionReflection->getName(), $this->functionsSubjectPosition);
return array_key_exists($functionReflection->getName(), self::FUNCTIONS_SUBJECT_POSITION);
}

public function getTypeFromFunctionCall(
Expand Down Expand Up @@ -75,7 +73,7 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
Scope $scope,
): Type
{
$argumentPosition = $this->functionsSubjectPosition[$functionReflection->getName()];
$argumentPosition = self::FUNCTIONS_SUBJECT_POSITION[$functionReflection->getName()];
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
Expand All @@ -91,8 +89,8 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
return TypeUtils::toBenevolentUnion($defaultReturnType);
}

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

if (count($functionCall->getArgs()) > $replaceArgumentPosition) {
$replaceArgumentType = $scope->getType($functionCall->getArgs()[$replaceArgumentPosition]->value);
Expand Down
7 changes: 3 additions & 4 deletions src/Type/Php/StrContainingTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
final class StrContainingTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

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

public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
{
return array_key_exists(strtolower($functionReflection->getName()), $this->strContainingFunctions)
return array_key_exists(strtolower($functionReflection->getName()), self::STR_CONTAINING_FUNCTIONS)
&& $context->true();
}

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

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

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

0 comments on commit 5ea900c

Please sign in to comment.