Skip to content

Commit 0b8b337

Browse files
authored
Specify types on get_parent_class()
1 parent 17ff345 commit 0b8b337

3 files changed

Lines changed: 268 additions & 141 deletions

File tree

‎src/Analyser/TypeSpecifier.php‎

Lines changed: 212 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -185,148 +185,10 @@ public function specifyTypesInCondition(
185185
$exprNode = $expressions[0];
186186
/** @var ConstantScalarType $constantType */
187187
$constantType = $expressions[1];
188-
if (!$context->null() && $constantType->getValue() === false) {
189-
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
190-
return $types->unionWith($this->specifyTypesInCondition(
191-
$scope,
192-
$exprNode,
193-
$context->true() ? TypeSpecifierContext::createFalse() : TypeSpecifierContext::createFalse()->negate(),
194-
$rootExpr,
195-
));
196-
}
197188

198-
if (!$context->null() && $constantType->getValue() === true) {
199-
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
200-
return $types->unionWith($this->specifyTypesInCondition(
201-
$scope,
202-
$exprNode,
203-
$context->true() ? TypeSpecifierContext::createTrue() : TypeSpecifierContext::createTrue()->negate(),
204-
$rootExpr,
205-
));
206-
}
207-
208-
if ($constantType->getValue() === null) {
209-
return $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
210-
}
211-
212-
if (
213-
!$context->null()
214-
&& $exprNode instanceof FuncCall
215-
&& count($exprNode->getArgs()) === 1
216-
&& $exprNode->name instanceof Name
217-
&& in_array(strtolower((string) $exprNode->name), ['count', 'sizeof'], true)
218-
&& $constantType instanceof ConstantIntegerType
219-
) {
220-
if ($context->truthy() || $constantType->getValue() === 0) {
221-
$newContext = $context;
222-
if ($constantType->getValue() === 0) {
223-
$newContext = $newContext->negate();
224-
}
225-
$argType = $scope->getType($exprNode->getArgs()[0]->value);
226-
if ($argType->isArray()->yes()) {
227-
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
228-
$valueTypes = $this->create($exprNode->getArgs()[0]->value, new NonEmptyArrayType(), $newContext, false, $scope, $rootExpr);
229-
return $funcTypes->unionWith($valueTypes);
230-
}
231-
}
232-
}
233-
234-
if (
235-
!$context->null()
236-
&& $exprNode instanceof FuncCall
237-
&& count($exprNode->getArgs()) === 1
238-
&& $exprNode->name instanceof Name
239-
&& strtolower((string) $exprNode->name) === 'strlen'
240-
&& $constantType instanceof ConstantIntegerType
241-
) {
242-
if ($context->truthy() || $constantType->getValue() === 0) {
243-
$newContext = $context;
244-
if ($constantType->getValue() === 0) {
245-
$newContext = $newContext->negate();
246-
}
247-
$argType = $scope->getType($exprNode->getArgs()[0]->value);
248-
if ($argType->isString()->yes()) {
249-
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
250-
251-
$accessory = new AccessoryNonEmptyStringType();
252-
if ($constantType->getValue() >= 2) {
253-
$accessory = new AccessoryNonFalsyStringType();
254-
}
255-
$valueTypes = $this->create($exprNode->getArgs()[0]->value, $accessory, $newContext, false, $scope, $rootExpr);
256-
257-
return $funcTypes->unionWith($valueTypes);
258-
}
259-
}
260-
}
261-
262-
if (
263-
$context->truthy()
264-
&& $exprNode instanceof FuncCall
265-
&& $exprNode->name instanceof Name
266-
&& in_array(strtolower($exprNode->name->toString()), ['substr', 'strstr', 'stristr', 'strchr', 'strrchr', 'strtolower', 'strtoupper', 'mb_strtolower', 'mb_strtoupper', 'ucfirst', 'lcfirst', 'ucwords'], true)
267-
&& isset($exprNode->getArgs()[0])
268-
&& $constantType instanceof ConstantStringType
269-
&& $constantType->getValue() !== ''
270-
) {
271-
$argType = $scope->getType($exprNode->getArgs()[0]->value);
272-
273-
if ($argType->isString()->yes()) {
274-
if ($constantType->getValue() !== '0') {
275-
return $this->create(
276-
$exprNode->getArgs()[0]->value,
277-
TypeCombinator::intersect($argType, new AccessoryNonFalsyStringType()),
278-
$context,
279-
false,
280-
$scope,
281-
);
282-
}
283-
284-
return $this->create(
285-
$exprNode->getArgs()[0]->value,
286-
TypeCombinator::intersect($argType, new AccessoryNonEmptyStringType()),
287-
$context,
288-
false,
289-
$scope,
290-
);
291-
}
292-
}
293-
294-
if (
295-
$exprNode instanceof FuncCall
296-
&& $exprNode->name instanceof Name
297-
&& strtolower($exprNode->name->toString()) === 'gettype'
298-
&& isset($exprNode->getArgs()[0])
299-
&& $constantType instanceof ConstantStringType
300-
) {
301-
$type = null;
302-
if ($constantType->getValue() === 'string') {
303-
$type = new StringType();
304-
}
305-
if ($constantType->getValue() === 'array') {
306-
$type = new ArrayType(new MixedType(), new MixedType());
307-
}
308-
if ($constantType->getValue() === 'boolean') {
309-
$type = new BooleanType();
310-
}
311-
if ($constantType->getValue() === 'resource' || $constantType->getValue() === 'resource (closed)') {
312-
$type = new ResourceType();
313-
}
314-
if ($constantType->getValue() === 'integer') {
315-
$type = new IntegerType();
316-
}
317-
if ($constantType->getValue() === 'double') {
318-
$type = new FloatType();
319-
}
320-
if ($constantType->getValue() === 'NULL') {
321-
$type = new NullType();
322-
}
323-
if ($constantType->getValue() === 'object') {
324-
$type = new ObjectWithoutClassType();
325-
}
326-
327-
if ($type !== null) {
328-
return $this->create($exprNode->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
329-
}
189+
$specifiedType = $this->specifyTypesForConstantBinaryExpression($exprNode, $constantType, $context, $scope, $rootExpr);
190+
if ($specifiedType !== null) {
191+
return $specifiedType;
330192
}
331193
}
332194

@@ -1051,6 +913,215 @@ public function specifyTypesInCondition(
1051913
return new SpecifiedTypes([], [], false, [], $rootExpr);
1052914
}
1053915

916+
private function specifyTypesForConstantBinaryExpression(
917+
Expr $exprNode,
918+
ConstantScalarType $constantType,
919+
TypeSpecifierContext $context,
920+
Scope $scope,
921+
?Expr $rootExpr,
922+
): ?SpecifiedTypes
923+
{
924+
if (!$context->null() && $constantType->getValue() === false) {
925+
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
926+
return $types->unionWith($this->specifyTypesInCondition(
927+
$scope,
928+
$exprNode,
929+
$context->true() ? TypeSpecifierContext::createFalse() : TypeSpecifierContext::createFalse()->negate(),
930+
$rootExpr,
931+
));
932+
}
933+
934+
if (!$context->null() && $constantType->getValue() === true) {
935+
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
936+
return $types->unionWith($this->specifyTypesInCondition(
937+
$scope,
938+
$exprNode,
939+
$context->true() ? TypeSpecifierContext::createTrue() : TypeSpecifierContext::createTrue()->negate(),
940+
$rootExpr,
941+
));
942+
}
943+
944+
if ($constantType->getValue() === null) {
945+
return $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
946+
}
947+
948+
if (
949+
!$context->null()
950+
&& $exprNode instanceof FuncCall
951+
&& count($exprNode->getArgs()) === 1
952+
&& $exprNode->name instanceof Name
953+
&& in_array(strtolower((string) $exprNode->name), ['count', 'sizeof'], true)
954+
&& $constantType instanceof ConstantIntegerType
955+
) {
956+
if ($context->truthy() || $constantType->getValue() === 0) {
957+
$newContext = $context;
958+
if ($constantType->getValue() === 0) {
959+
$newContext = $newContext->negate();
960+
}
961+
$argType = $scope->getType($exprNode->getArgs()[0]->value);
962+
if ($argType->isArray()->yes()) {
963+
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
964+
$valueTypes = $this->create($exprNode->getArgs()[0]->value, new NonEmptyArrayType(), $newContext, false, $scope, $rootExpr);
965+
return $funcTypes->unionWith($valueTypes);
966+
}
967+
}
968+
}
969+
970+
if (
971+
!$context->null()
972+
&& $exprNode instanceof FuncCall
973+
&& count($exprNode->getArgs()) === 1
974+
&& $exprNode->name instanceof Name
975+
&& strtolower((string) $exprNode->name) === 'strlen'
976+
&& $constantType instanceof ConstantIntegerType
977+
) {
978+
if ($context->truthy() || $constantType->getValue() === 0) {
979+
$newContext = $context;
980+
if ($constantType->getValue() === 0) {
981+
$newContext = $newContext->negate();
982+
}
983+
$argType = $scope->getType($exprNode->getArgs()[0]->value);
984+
if ($argType->isString()->yes()) {
985+
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
986+
987+
$accessory = new AccessoryNonEmptyStringType();
988+
if ($constantType->getValue() >= 2) {
989+
$accessory = new AccessoryNonFalsyStringType();
990+
}
991+
$valueTypes = $this->create($exprNode->getArgs()[0]->value, $accessory, $newContext, false, $scope, $rootExpr);
992+
993+
return $funcTypes->unionWith($valueTypes);
994+
}
995+
}
996+
997+
}
998+
999+
if ($constantType instanceof ConstantStringType) {
1000+
return $this->specifyTypesForConstantStringBinaryExpression($exprNode, $constantType, $context, $scope, $rootExpr);
1001+
}
1002+
1003+
return null;
1004+
}
1005+
1006+
private function specifyTypesForConstantStringBinaryExpression(
1007+
Expr $exprNode,
1008+
ConstantStringType $constantType,
1009+
TypeSpecifierContext $context,
1010+
Scope $scope,
1011+
?Expr $rootExpr,
1012+
): ?SpecifiedTypes
1013+
{
1014+
if (
1015+
$context->truthy()
1016+
&& $exprNode instanceof FuncCall
1017+
&& $exprNode->name instanceof Name
1018+
&& in_array(strtolower($exprNode->name->toString()), ['substr', 'strstr', 'stristr', 'strchr', 'strrchr', 'strtolower', 'strtoupper', 'mb_strtolower', 'mb_strtoupper', 'ucfirst', 'lcfirst', 'ucwords'], true)
1019+
&& isset($exprNode->getArgs()[0])
1020+
&& $constantType->getValue() !== ''
1021+
) {
1022+
$argType = $scope->getType($exprNode->getArgs()[0]->value);
1023+
1024+
if ($argType->isString()->yes()) {
1025+
if ($constantType->getValue() !== '0') {
1026+
return $this->create(
1027+
$exprNode->getArgs()[0]->value,
1028+
TypeCombinator::intersect($argType, new AccessoryNonFalsyStringType()),
1029+
$context,
1030+
false,
1031+
$scope,
1032+
);
1033+
}
1034+
1035+
return $this->create(
1036+
$exprNode->getArgs()[0]->value,
1037+
TypeCombinator::intersect($argType, new AccessoryNonEmptyStringType()),
1038+
$context,
1039+
false,
1040+
$scope,
1041+
);
1042+
}
1043+
}
1044+
1045+
if (
1046+
$exprNode instanceof FuncCall
1047+
&& $exprNode->name instanceof Name
1048+
&& strtolower($exprNode->name->toString()) === 'gettype'
1049+
&& isset($exprNode->getArgs()[0])
1050+
) {
1051+
$type = null;
1052+
if ($constantType->getValue() === 'string') {
1053+
$type = new StringType();
1054+
}
1055+
if ($constantType->getValue() === 'array') {
1056+
$type = new ArrayType(new MixedType(), new MixedType());
1057+
}
1058+
if ($constantType->getValue() === 'boolean') {
1059+
$type = new BooleanType();
1060+
}
1061+
if ($constantType->getValue() === 'resource' || $constantType->getValue() === 'resource (closed)') {
1062+
$type = new ResourceType();
1063+
}
1064+
if ($constantType->getValue() === 'integer') {
1065+
$type = new IntegerType();
1066+
}
1067+
if ($constantType->getValue() === 'double') {
1068+
$type = new FloatType();
1069+
}
1070+
if ($constantType->getValue() === 'NULL') {
1071+
$type = new NullType();
1072+
}
1073+
if ($constantType->getValue() === 'object') {
1074+
$type = new ObjectWithoutClassType();
1075+
}
1076+
1077+
if ($type !== null) {
1078+
return $this->create($exprNode->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
1079+
}
1080+
}
1081+
1082+
if (
1083+
$context->true()
1084+
&& $exprNode instanceof FuncCall
1085+
&& $exprNode->name instanceof Name
1086+
&& strtolower((string) $exprNode->name) === 'get_parent_class'
1087+
&& isset($exprNode->getArgs()[0])
1088+
) {
1089+
$argType = $scope->getType($exprNode->getArgs()[0]->value);
1090+
$objectType = new ObjectType($constantType->getValue());
1091+
$classStringType = new GenericClassStringType($objectType);
1092+
1093+
if ($argType->isString()->yes()) {
1094+
return $this->create(
1095+
$exprNode->getArgs()[0]->value,
1096+
$classStringType,
1097+
$context,
1098+
false,
1099+
$scope,
1100+
);
1101+
}
1102+
1103+
if ((new ObjectWithoutClassType())->isSuperTypeOf($argType)->yes()) {
1104+
return $this->create(
1105+
$exprNode->getArgs()[0]->value,
1106+
$objectType,
1107+
$context,
1108+
false,
1109+
$scope,
1110+
);
1111+
}
1112+
1113+
return $this->create(
1114+
$exprNode->getArgs()[0]->value,
1115+
TypeCombinator::union($objectType, $classStringType),
1116+
$context,
1117+
false,
1118+
$scope,
1119+
);
1120+
}
1121+
1122+
return null;
1123+
}
1124+
10541125
private function handleDefaultTruthyOrFalseyContext(TypeSpecifierContext $context, ?Expr $rootExpr, Expr $expr, Scope $scope): SpecifiedTypes
10551126
{
10561127
if ($context->null()) {

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ public function dataFileAsserts(): iterable
10521052

10531053
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-offset-unset.php');
10541054
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8008.php');
1055+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5552.php');
10551056
}
10561057

10571058
/**

0 commit comments

Comments
 (0)