Skip to content

Commit

Permalink
[BCB] Type generalization precision is now a required argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 23, 2021
1 parent 4e7d60d commit 6568103
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 19 deletions.
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ private function resolveType(Expr $node): Type
foreach ($rightTypes as $rightType) {
$resultType = $this->calculateFromScalars($node, $leftType, $rightType);
if ($generalize) {
$resultType = TypeUtils::generalizeType($resultType);
$resultType = TypeUtils::generalizeType($resultType, GeneralizePrecision::lessSpecific());
}
$resultTypes[] = $resultType;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ public function toFloat(): Type
return $this->toBoolean()->toFloat();
}

public function generalize(?GeneralizePrecision $precision = null): Type
public function generalize(GeneralizePrecision $precision): Type
{
if (count($this->keyTypes) === 0) {
return $this;
Expand Down
8 changes: 4 additions & 4 deletions src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,25 +301,25 @@ public function append(self $otherString): self
return new self($this->getValue() . $otherString->getValue());
}

public function generalize(?GeneralizePrecision $precision = null): Type
public function generalize(GeneralizePrecision $precision): Type
{
if ($this->isClassString) {
if ($precision !== null && $precision->isMoreSpecific()) {
if ($precision->isMoreSpecific()) {
return new ClassStringType();
}

return new StringType();
}

if ($this->getValue() !== '' && $precision !== null && $precision->isMoreSpecific()) {
if ($this->getValue() !== '' && $precision->isMoreSpecific()) {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryLiteralStringType(),
]);
}

if ($precision !== null && $precision->isMoreSpecific()) {
if ($precision->isMoreSpecific()) {
return new IntersectionType([
new StringType(),
new AccessoryLiteralStringType(),
Expand Down
2 changes: 1 addition & 1 deletion src/Type/ConstantType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
interface ConstantType extends Type
{

public function generalize(?GeneralizePrecision $precision = null): Type;
public function generalize(GeneralizePrecision $precision): Type;

}
2 changes: 1 addition & 1 deletion src/Type/IntegerRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function equals(Type $type): bool
}


public function generalize(?GeneralizePrecision $precision = null): Type
public function generalize(GeneralizePrecision $precision): Type
{
return new parent();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getValue()
return null;
}

public function generalize(?GeneralizePrecision $precision = null): Type
public function generalize(GeneralizePrecision $precision): Type
{
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Traits/ConstantScalarTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function generalize(?GeneralizePrecision $precision = null): Type
public function generalize(GeneralizePrecision $precision): Type
{
return new parent();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/TypeUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function getAnyArrays(Type $type): array
return self::map(ArrayType::class, $type, true, false);
}

public static function generalizeType(Type $type, ?GeneralizePrecision $precision = null): Type
public static function generalizeType(Type $type, GeneralizePrecision $precision): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($precision): Type {
if ($type instanceof ConstantType) {
Expand Down

0 comments on commit 6568103

Please sign in to comment.