Skip to content

Commit 60d26a3

Browse files
staabmondrejmirtes
authored andcommitted
Enable reportPossiblyNonexistentConstantArrayOffset
1 parent 6abeff3 commit 60d26a3

8 files changed

+40
-7
lines changed

build/phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ parameters:
2323
nodesByStringCountMax: 128
2424
checkUninitializedProperties: true
2525
checkMissingCallableSignature: true
26+
reportPossiblyNonexistentConstantArrayOffset: true
2627
excludePaths:
2728
- ../tests/*/data/*
2829
- ../tests/tmp/*

src/Rules/Functions/PrintfParameterTypeRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
1010
use PHPStan\Rules\RuleLevelHelper;
11+
use PHPStan\ShouldNotHappenException;
1112
use PHPStan\Type\BooleanType;
1213
use PHPStan\Type\ErrorType;
1314
use PHPStan\Type\FloatType;
@@ -144,6 +145,10 @@ public function processNode(Node $node, Scope $scope): array
144145
continue;
145146
}
146147

148+
if (!array_key_exists($placeholder->acceptingType, $allowedTypeNameMap)) {
149+
throw new ShouldNotHappenException();
150+
}
151+
147152
$errors[] = RuleErrorBuilder::message(
148153
sprintf(
149154
'Parameter #%d of function %s is expected to be %s by placeholder #%d (%s), %s given.',

src/Type/Php/ArgumentBasedFunctionReturnTypeExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\FunctionReflection;
9+
use PHPStan\ShouldNotHappenException;
910
use PHPStan\Type\Accessory\NonEmptyArrayType;
1011
use PHPStan\Type\ArrayType;
1112
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
@@ -44,6 +45,9 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
4445

4546
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
4647
{
48+
if (!array_key_exists($functionReflection->getName(), self::FUNCTION_NAMES)) {
49+
throw new ShouldNotHappenException();
50+
}
4751
$argumentPosition = self::FUNCTION_NAMES[$functionReflection->getName()];
4852

4953
if (!isset($functionCall->getArgs()[$argumentPosition])) {

src/Type/Php/HashFunctionsReturnTypeExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Php\PhpVersion;
99
use PHPStan\Reflection\FunctionReflection;
10+
use PHPStan\ShouldNotHappenException;
1011
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
1112
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
1213
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -18,6 +19,7 @@
1819
use PHPStan\Type\Type;
1920
use PHPStan\Type\TypeCombinator;
2021
use PHPStan\Type\TypeUtils;
22+
use function array_key_exists;
2123
use function array_map;
2224
use function count;
2325
use function hash_algos;
@@ -101,7 +103,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
101103
return null;
102104
}
103105

104-
$functionData = self::SUPPORTED_FUNCTIONS[strtolower($functionReflection->getName())];
106+
$lowerFunctionName = strtolower($functionReflection->getName());
107+
if (!array_key_exists($lowerFunctionName, self::SUPPORTED_FUNCTIONS)) {
108+
throw new ShouldNotHappenException();
109+
}
110+
$functionData = self::SUPPORTED_FUNCTIONS[$lowerFunctionName];
105111
if (is_bool($functionData['binary'])) {
106112
$binaryType = new ConstantBooleanType($functionData['binary']);
107113
} elseif (isset($functionCall->getArgs()[$functionData['binary']])) {

src/Type/Php/JsonThrowTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use PHPStan\DependencyInjection\AutowiredService;
99
use PHPStan\Reflection\FunctionReflection;
1010
use PHPStan\Reflection\ReflectionProvider;
11+
use PHPStan\ShouldNotHappenException;
1112
use PHPStan\Type\BitwiseFlagHelper;
1213
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
1314
use PHPStan\Type\ObjectType;
1415
use PHPStan\Type\Type;
16+
use function array_key_exists;
1517
use function in_array;
1618

1719
#[AutowiredService]
@@ -50,6 +52,9 @@ public function getThrowTypeFromFunctionCall(
5052
Scope $scope,
5153
): ?Type
5254
{
55+
if (!array_key_exists($functionReflection->getName(), self::ARGUMENTS_POSITIONS)) {
56+
throw new ShouldNotHappenException();
57+
}
5358
$argumentPosition = self::ARGUMENTS_POSITIONS[$functionReflection->getName()];
5459
if (!isset($functionCall->getArgs()[$argumentPosition])) {
5560
return null;

src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\FunctionReflection;
99
use PHPStan\Reflection\ParametersAcceptorSelector;
10+
use PHPStan\ShouldNotHappenException;
1011
use PHPStan\Type\Accessory\AccessoryArrayListType;
1112
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
1213
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
@@ -196,6 +197,10 @@ private function getSubjectType(
196197
Scope $scope,
197198
): ?Type
198199
{
200+
if (!array_key_exists($functionReflection->getName(), self::FUNCTIONS_SUBJECT_POSITION)) {
201+
throw new ShouldNotHappenException();
202+
}
203+
199204
$argumentPosition = self::FUNCTIONS_SUBJECT_POSITION[$functionReflection->getName()];
200205
if (count($functionCall->getArgs()) <= $argumentPosition) {
201206
return null;

src/Type/Php/StrCaseFunctionsReturnTypeExtension.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\FunctionReflection;
9+
use PHPStan\ShouldNotHappenException;
910
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
1011
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
1112
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
@@ -19,10 +20,10 @@
1920
use PHPStan\Type\TypeCombinator;
2021
use PHPStan\Type\TypeUtils;
2122
use function array_diff;
23+
use function array_key_exists;
2224
use function array_map;
2325
use function count;
2426
use function in_array;
25-
use function is_callable;
2627
use function mb_check_encoding;
2728
use const MB_CASE_LOWER;
2829
use const MB_CASE_UPPER;
@@ -62,14 +63,15 @@ public function getTypeFromFunctionCall(
6263
$fnName = $functionReflection->getName();
6364
$args = $functionCall->getArgs();
6465

65-
if (count($args) < self::FUNCTIONS[$fnName]) {
66-
return null;
66+
if (!array_key_exists($fnName, self::FUNCTIONS)) {
67+
throw new ShouldNotHappenException();
6768
}
69+
$requiredArgs = self::FUNCTIONS[$fnName];
6870

69-
$argType = $scope->getType($args[0]->value);
70-
if (!is_callable($fnName)) {
71+
if (count($args) < $requiredArgs) {
7172
return null;
7273
}
74+
$argType = $scope->getType($args[0]->value);
7375

7476
$modes = [];
7577
$keepLowercase = false;

src/Type/Php/StrContainingTypeSpecifyingExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Analyser\TypeSpecifierContext;
1616
use PHPStan\DependencyInjection\AutowiredService;
1717
use PHPStan\Reflection\FunctionReflection;
18+
use PHPStan\ShouldNotHappenException;
1819
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
1920
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
2021
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
@@ -65,7 +66,11 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
6566
$args = $node->getArgs();
6667

6768
if (count($args) >= 2) {
68-
[$hackstackArg, $needleArg] = self::STR_CONTAINING_FUNCTIONS[strtolower($functionReflection->getName())];
69+
$lowerFunctionName = strtolower($functionReflection->getName());
70+
if (!array_key_exists($lowerFunctionName, self::STR_CONTAINING_FUNCTIONS)) {
71+
throw new ShouldNotHappenException();
72+
}
73+
[$hackstackArg, $needleArg] = self::STR_CONTAINING_FUNCTIONS[$lowerFunctionName];
6974

7075
$haystackType = $scope->getType($args[$hackstackArg]->value);
7176
$needleType = $scope->getType($args[$needleArg]->value)->toString();

0 commit comments

Comments
 (0)