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 9, 2023
1 parent fe8d164 commit 2e4a572
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
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 isset(self::FUNCTIONS_SUBJECT_POSITION[$functionReflection->getName()]);
}

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() && isset(self::FUNCTIONS_SUBJECT_POSITION[$functionReflection->getName()])) {
$replaceArgumentPosition = self::FUNCTIONS_REPLACE_POSITION[$functionReflection->getName()];

if (count($functionCall->getArgs()) > $replaceArgumentPosition) {
$replaceArgumentType = $scope->getType($functionCall->getArgs()[$replaceArgumentPosition]->value);
Expand Down
8 changes: 3 additions & 5 deletions src/Type/Php/StrContainingTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use function array_key_exists;
use function count;
use function strtolower;

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 +48,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 isset(self::STR_CONTAINING_FUNCTIONS[strtolower($functionReflection->getName())])
&& $context->true();
}

Expand All @@ -59,7 +57,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 2e4a572

Please sign in to comment.