Skip to content

Commit 138e5b5

Browse files
committed
TASK: Rename Inferrer to Narrower and apply further suggestions from code review
1 parent d835fa5 commit 138e5b5

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

src/TypeSystem/Inferrer/TypeInferrer.php renamed to src/TypeSystem/Narrower/ExpressionTypeNarrower.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
declare(strict_types=1);
2222

23-
namespace PackageFactory\ComponentEngine\TypeSystem\Inferrer;
23+
namespace PackageFactory\ComponentEngine\TypeSystem\Narrower;
2424

2525
use PackageFactory\ComponentEngine\Definition\BinaryOperator;
2626
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperationNode;
@@ -40,30 +40,30 @@
4040
* The structure is partially inspired by phpstan
4141
* https://github.com/phpstan/phpstan-src/blob/07bb4aa2d5e39dafa78f56c5df132c763c2d1b67/src/Analyser/TypeSpecifier.php#L111
4242
*/
43-
class TypeInferrer
43+
class ExpressionTypeNarrower
4444
{
4545
public function __construct(
4646
private readonly ScopeInterface $scope
4747
) {
4848
}
4949

50-
public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferrerContext $context): InferredTypes
50+
public function narrowTypesOfSymbolsIn(ExpressionNode $conditionNode, TypeNarrowerContext $context): NarrowedTypes
5151
{
5252
if ($conditionNode->root instanceof IdentifierNode) {
5353
$type = $this->scope->lookupTypeFor($conditionNode->root->value);
5454
if (!$type) {
55-
return InferredTypes::empty();
55+
return NarrowedTypes::empty();
5656
}
5757
// case `nullableString ? "nullableString is not null" : "nullableString is null"`
58-
return InferredTypes::fromType($conditionNode->root->value, $context->narrowDownType($type));
58+
return NarrowedTypes::fromEntry($conditionNode->root->value, $context->narrowType($type));
5959
}
6060

6161
if (($binaryOperationNode = $conditionNode->root) instanceof BinaryOperationNode) {
6262
// cases
6363
// `nullableString === null ? "nullableString is null" : "nullableString is not null"`
6464
// `nullableString !== null ? "nullableString is not null" : "nullableString is null"`
6565
if (count($binaryOperationNode->operands->rest) !== 1) {
66-
return InferredTypes::empty();
66+
return NarrowedTypes::empty();
6767
}
6868
$first = $binaryOperationNode->operands->first;
6969
$second = $binaryOperationNode->operands->rest[0];
@@ -77,21 +77,21 @@ public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferre
7777
};
7878

7979
if ($comparedIdentifierValueToNull === null) {
80-
return InferredTypes::empty();
80+
return NarrowedTypes::empty();
8181
}
8282
$type = $this->scope->lookupTypeFor($comparedIdentifierValueToNull);
8383
if (!$type) {
84-
return InferredTypes::empty();
84+
return NarrowedTypes::empty();
8585
}
8686

8787
if ($binaryOperationNode->operator === BinaryOperator::EQUAL) {
88-
return InferredTypes::fromType($comparedIdentifierValueToNull, $context->negate()->narrowDownType($type));
88+
return NarrowedTypes::fromEntry($comparedIdentifierValueToNull, $context->negate()->narrowType($type));
8989
}
9090
if ($binaryOperationNode->operator === BinaryOperator::NOT_EQUAL) {
91-
return InferredTypes::fromType($comparedIdentifierValueToNull, $context->narrowDownType($type));
91+
return NarrowedTypes::fromEntry($comparedIdentifierValueToNull, $context->narrowType($type));
9292
}
9393
}
9494

95-
return InferredTypes::empty();
95+
return NarrowedTypes::empty();
9696
}
9797
}

src/TypeSystem/Inferrer/InferredTypes.php renamed to src/TypeSystem/Narrower/NarrowedTypes.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
declare(strict_types=1);
2222

23-
namespace PackageFactory\ComponentEngine\TypeSystem\Inferrer;
23+
namespace PackageFactory\ComponentEngine\TypeSystem\Narrower;
2424

2525
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface;
2626

27-
class InferredTypes
27+
class NarrowedTypes
2828
{
2929
/**
3030
* Map of identifierName to the corresponding inferred type
@@ -35,7 +35,7 @@ class InferredTypes
3535
private function __construct(
3636
TypeInterface ...$types
3737
) {
38-
assert(self::isAssociativeArray($types), '$types must be an associative array');
38+
/** @var array<string,TypeInterface> $types */
3939
$this->types = $types;
4040
}
4141

@@ -44,7 +44,7 @@ public static function empty(): self
4444
return new self();
4545
}
4646

47-
public static function fromType(string $identifierName, TypeInterface $type): self
47+
public static function fromEntry(string $identifierName, TypeInterface $type): self
4848
{
4949
return new self(...[$identifierName => $type]);
5050
}
@@ -53,20 +53,4 @@ public function getType(string $identifierName): ?TypeInterface
5353
{
5454
return $this->types[$identifierName] ?? null;
5555
}
56-
57-
/**
58-
* @template T
59-
* @param array<string|int,T> $array
60-
* @phpstan-assert-if-true array<string,T> $array
61-
*/
62-
private static function isAssociativeArray(array $array): bool
63-
{
64-
foreach ($array as $key => $value) {
65-
if (is_string($key)) {
66-
continue;
67-
}
68-
return false;
69-
}
70-
return true;
71-
}
7256
}

src/TypeSystem/Inferrer/TypeInferrerContext.php renamed to src/TypeSystem/Narrower/TypeNarrowerContext.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
declare(strict_types=1);
2222

23-
namespace PackageFactory\ComponentEngine\TypeSystem\Inferrer;
23+
namespace PackageFactory\ComponentEngine\TypeSystem\Narrower;
2424

2525
use PackageFactory\ComponentEngine\TypeSystem\Type\NullType\NullType;
2626
use PackageFactory\ComponentEngine\TypeSystem\Type\UnionType\UnionType;
2727
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface;
2828

29-
enum TypeInferrerContext
29+
enum TypeNarrowerContext
3030
{
3131
case TRUTHY;
3232

@@ -40,7 +40,7 @@ public function negate(): self
4040
};
4141
}
4242

43-
public function narrowDownType(TypeInterface $type): TypeInterface
43+
public function narrowType(TypeInterface $type): TypeInterface
4444
{
4545
if (!$type instanceof UnionType || !$type->containsNull()) {
4646
return $type;

src/TypeSystem/Scope/TernaryBranchScope/TernaryBranchScope.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@
2424

2525
use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
2626
use PackageFactory\ComponentEngine\Parser\Ast\TypeReferenceNode;
27-
use PackageFactory\ComponentEngine\TypeSystem\Inferrer\InferredTypes;
28-
use PackageFactory\ComponentEngine\TypeSystem\Inferrer\TypeInferrer;
29-
use PackageFactory\ComponentEngine\TypeSystem\Inferrer\TypeInferrerContext;
27+
use PackageFactory\ComponentEngine\TypeSystem\Narrower\NarrowedTypes;
28+
use PackageFactory\ComponentEngine\TypeSystem\Narrower\ExpressionTypeNarrower;
29+
use PackageFactory\ComponentEngine\TypeSystem\Narrower\TypeNarrowerContext;
3030
use PackageFactory\ComponentEngine\TypeSystem\ScopeInterface;
3131
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface;
3232

3333
final class TernaryBranchScope implements ScopeInterface
3434
{
3535
private function __construct(
36-
private readonly InferredTypes $inferredTypes,
36+
private readonly NarrowedTypes $inferredTypes,
3737
private readonly ScopeInterface $parentScope
3838
) {
3939
}
4040

4141
public static function forTruthyBranch(ExpressionNode $conditionNode, ScopeInterface $parentScope): self
4242
{
4343
return new self(
44-
(new TypeInferrer($parentScope))->inferTypesInCondition($conditionNode, TypeInferrerContext::TRUTHY),
44+
(new ExpressionTypeNarrower($parentScope))->narrowTypesOfSymbolsIn($conditionNode, TypeNarrowerContext::TRUTHY),
4545
$parentScope
4646
);
4747
}
4848

4949
public static function forFalsyBranch(ExpressionNode $conditionNode, ScopeInterface $parentScope): self
5050
{
5151
return new self(
52-
(new TypeInferrer($parentScope))->inferTypesInCondition($conditionNode, TypeInferrerContext::FALSY),
52+
(new ExpressionTypeNarrower($parentScope))->narrowTypesOfSymbolsIn($conditionNode, TypeNarrowerContext::FALSY),
5353
$parentScope
5454
);
5555
}

0 commit comments

Comments
 (0)