Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 4, 2024
1 parent 76c050d commit 9d6b4b6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 68 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^1.7"
"phpstan/phpstan": "^1.12"
},
"conflict": {
"azjezz/psl": "<1.6||>=4.0"
Expand Down
22 changes: 1 addition & 21 deletions src/Type/MatchesTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\TypeWithClassName;
use Psl\Type\TypeInterface;

class MatchesTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
Expand Down Expand Up @@ -41,29 +40,10 @@ public function specifyTypes(MethodReflection $methodReflection, MethodCall $nod
}

$specType = $scope->getType($node->var);
if (!$specType instanceof TypeWithClassName) {
return new SpecifiedTypes();
}

$specTypeReflection = $specType->getClassReflection();
if ($specTypeReflection === null) {
return new SpecifiedTypes();
}

$typeInterfaceAncestor = $specTypeReflection->getAncestorWithClassName(TypeInterface::class);
if ($typeInterfaceAncestor === null) {
return new SpecifiedTypes();
}

$typeMap = $typeInterfaceAncestor->getActiveTemplateTypeMap();
$t = $typeMap->getType('T');
if ($t === null) {
return new SpecifiedTypes();
}

return $this->typeSpecifier->create(
$args[0]->value,
$t,
$specType->getTemplateType(TypeInterface::class, 'T'),
$context
);
}
Expand Down
53 changes: 7 additions & 46 deletions src/Type/TypeShapeReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use Psl\Type\Internal\OptionalType;
use Psl\Type\TypeInterface;
use function count;
Expand Down Expand Up @@ -40,12 +40,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

$results = [];
foreach ($arrays as $array) {
$result = $this->createResult($array);
if ($result === null) {
return null;
}

$results[] = $result;
$results[] = $this->createResult($array);
}

return new GenericObjectType(
Expand All @@ -56,32 +51,12 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
);
}

private function createResult(ConstantArrayType $arrayType): ?Type
private function createResult(ConstantArrayType $arrayType): Type
{
$builder = ConstantArrayTypeBuilder::createEmpty();
foreach ($arrayType->getKeyTypes() as $key) {
$valueType = $arrayType->getOffsetValueType($key);
if (!$valueType instanceof TypeWithClassName) {
return null;
}

$valueClassReflection = $valueType->getClassReflection();
if ($valueClassReflection === null) {
return null;
}

$typeInterfaceAncestor = $valueClassReflection->getAncestorWithClassName(TypeInterface::class);
if ($typeInterfaceAncestor === null) {
return null;
}

$typeMap = $typeInterfaceAncestor->getActiveTemplateTypeMap();
$t = $typeMap->getType('T');
if ($t === null) {
return null;
}

[$type, $optional] = $this->extractOptional($t);
[$type, $optional] = $this->extractOptional($valueType->getTemplateType(TypeInterface::class, 'T'));

$builder->setOffsetValueType($key, $type, $optional);
}
Expand All @@ -94,26 +69,12 @@ private function createResult(ConstantArrayType $arrayType): ?Type
*/
private function extractOptional(Type $type): array
{
if (!$type instanceof TypeWithClassName) {
return [$type, false];
}

$classReflection = $type->getClassReflection();
if ($classReflection === null) {
return [$type, false];
}
$optionalTypeAncestor = $classReflection->getAncestorWithClassName(OptionalType::class);
if ($optionalTypeAncestor === null) {
return [$type, false];
}

$typeMap = $optionalTypeAncestor->getActiveTemplateTypeMap();
$t = $typeMap->getType('T');
if ($t === null) {
$optionalType = $type->getTemplateType(OptionalType::class, 'T');
if ($optionalType instanceof ErrorType) {
return [$type, false];
}

return [$t, true];
return [$optionalType, true];
}

}

0 comments on commit 9d6b4b6

Please sign in to comment.