Skip to content

Commit 67144f0

Browse files
committed
Remove strict types option (enabled by default)
1 parent ae26afd commit 67144f0

17 files changed

+79
-369
lines changed

src/Mapper.php

-13
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,6 @@ public function __construct(
2424
$this->types = new Repository($this->platform);
2525
}
2626

27-
/**
28-
* @api
29-
*
30-
* @see Context::withStrictTypes()
31-
*/
32-
public function withStrictTypes(?bool $enabled = null): self
33-
{
34-
$self = clone $this;
35-
$self->context = $this->context->withStrictTypes($enabled);
36-
37-
return $self;
38-
}
39-
4027
/**
4128
* @api
4229
*

src/Path/Entry/UnionLeafEntry.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Path\Entry;
6+
7+
final class UnionLeafEntry extends Entry
8+
{
9+
/**
10+
* @param int<0, max> $index
11+
*/
12+
public function __construct(
13+
public readonly int $index,
14+
) {
15+
parent::__construct((string) $this->index);
16+
}
17+
}

src/Type/ArrayType.php

+5-39
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use TypeLang\Parser\Node\Stmt\Template\TemplateArgumentNode;
1313
use TypeLang\Parser\Node\Stmt\Template\TemplateArgumentsListNode;
1414
use TypeLang\Parser\Node\Stmt\TypeStatement;
15-
use TypeLang\Parser\Node\Stmt\UnionTypeNode;
1615

1716
class ArrayType implements TypeInterface
1817
{
@@ -33,33 +32,20 @@ public function __construct(
3332
#[\Override]
3433
public function getTypeStatement(LocalContext $context): TypeStatement
3534
{
36-
if (!$context->isDetailedTypes()) {
37-
return new NamedTypeNode($this->name);
38-
}
35+
$child = $context->withDetailedTypes(false);
3936

4037
return new NamedTypeNode(
4138
name: $this->name,
4239
arguments: new TemplateArgumentsListNode([
43-
new TemplateArgumentNode($this->key->getTypeStatement($context)),
44-
new TemplateArgumentNode($this->value->getTypeStatement($context)),
40+
new TemplateArgumentNode($this->key->getTypeStatement($child)),
41+
new TemplateArgumentNode($this->value->getTypeStatement($child)),
4542
]),
4643
);
4744
}
4845

49-
/**
50-
* @return UnionTypeNode<TypeStatement>
51-
*/
52-
protected function getSupportedKeyType(): UnionTypeNode
53-
{
54-
return new UnionTypeNode(
55-
new NamedTypeNode('string'),
56-
new NamedTypeNode('int'),
57-
);
58-
}
59-
6046
public function match(mixed $value, LocalContext $context): bool
6147
{
62-
if (!$this->matchRootType($value, $context)) {
48+
if (!\is_array($value)) {
6349
return false;
6450
}
6551

@@ -79,26 +65,14 @@ public function match(mixed $value, LocalContext $context): bool
7965
return true;
8066
}
8167

82-
/**
83-
* @return ($value is iterable ? true : false)
84-
*/
85-
private function matchRootType(mixed $value, LocalContext $context): bool
86-
{
87-
if (!$context->isStrictTypesEnabled()) {
88-
return \is_iterable($value);
89-
}
90-
91-
return \is_array($value);
92-
}
93-
9468
/**
9569
* @return array<array-key, mixed>
9670
* @throws InvalidValueException
9771
* @throws TypeNotFoundException
9872
*/
9973
public function cast(mixed $value, LocalContext $context): array
10074
{
101-
if (!$this->matchRootType($value, $context)) {
75+
if (!\is_array($value)) {
10276
throw InvalidValueException::becauseInvalidValueGiven(
10377
value: $value,
10478
expected: $this->getTypeStatement($context),
@@ -109,14 +83,6 @@ public function cast(mixed $value, LocalContext $context): array
10983
$result = [];
11084

11185
foreach ($value as $index => $item) {
112-
if (!\is_string($index) && !\is_int($index)) {
113-
throw InvalidValueException::becauseInvalidValueGiven(
114-
value: $index,
115-
expected: $this->getSupportedKeyType(),
116-
context: $context,
117-
);
118-
}
119-
12086
$context->enter(new ArrayIndexEntry($index));
12187

12288
$result[$this->key->cast($index, $context)]

src/Type/BoolLiteralType.php

+3-15
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,14 @@ public function getTypeStatement(LocalContext $context): TypeStatement
2929
{
3030
$result = new BoolLiteralNode($this->value, $this->name);
3131

32-
if ($context->isDetailedTypes()) {
33-
return new NamedTypeNode(self::DEFAULT_TYPE_NAME, new TemplateArgumentsListNode([
34-
new TemplateArgumentNode($result),
35-
]));
36-
}
37-
38-
return $result;
32+
return new NamedTypeNode(self::DEFAULT_TYPE_NAME, new TemplateArgumentsListNode([
33+
new TemplateArgumentNode($result),
34+
]));
3935
}
4036

4137
#[\Override]
4238
public function match(mixed $value, LocalContext $context): bool
4339
{
44-
if (!$context->isStrictTypesEnabled()) {
45-
$value = $this->tryCastToBool($value);
46-
}
47-
4840
return $value === $this->value;
4941
}
5042

@@ -56,10 +48,6 @@ public function match(mixed $value, LocalContext $context): bool
5648
#[\Override]
5749
public function cast(mixed $value, LocalContext $context): bool
5850
{
59-
if (!$context->isStrictTypesEnabled()) {
60-
$value = $this->tryCastToBool($value);
61-
}
62-
6351
if ($value === $this->value) {
6452
return $value;
6553
}

src/Type/BoolType.php

-23
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ public function __construct(
2525

2626
public function match(mixed $value, LocalContext $context): bool
2727
{
28-
if (!$context->isStrictTypesEnabled()) {
29-
$value = $this->tryCastToBool($value);
30-
}
31-
3228
return \is_bool($value);
3329
}
3430

@@ -39,10 +35,6 @@ public function match(mixed $value, LocalContext $context): bool
3935
*/
4036
public function cast(mixed $value, LocalContext $context): bool
4137
{
42-
if (!$context->isStrictTypesEnabled()) {
43-
$value = $this->tryCastToBool($value);
44-
}
45-
4638
if (\is_bool($value)) {
4739
return $value;
4840
}
@@ -53,19 +45,4 @@ public function cast(mixed $value, LocalContext $context): bool
5345
context: $context,
5446
);
5547
}
56-
57-
/**
58-
* A method to convert input data to a bool representation, if possible.
59-
*
60-
* If conversion is not possible, it returns the value "as is".
61-
*/
62-
protected function tryCastToBool(mixed $value): bool
63-
{
64-
return match (true) {
65-
\is_array($value) => $value !== [],
66-
\is_object($value) => true,
67-
\is_string($value) => $value !== '',
68-
default => (bool) $value,
69-
};
70-
}
7148
}

src/Type/Context/Context.php

-37
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
class Context
88
{
9-
/**
10-
* Default value for {@see $strictTypes} option.
11-
*/
12-
public const STRICT_TYPES_DEFAULT_VALUE = true;
13-
149
/**
1510
* Default value for {@see $objectsAsArrays} option.
1611
*/
@@ -22,11 +17,6 @@ class Context
2217
public const DETAILED_TYPES_DEFAULT_VALUE = true;
2318

2419
public function __construct(
25-
/**
26-
* If this option contains {@see false}, then type conversion is
27-
* allowed during transformation.
28-
*/
29-
protected ?bool $strictTypes = null,
3020
/**
3121
* If this option contains {@see true}, then objects are converted to
3222
* associative arrays, otherwise anonymous {@see object} will be
@@ -40,32 +30,6 @@ public function __construct(
4030
protected ?bool $detailedTypes = null,
4131
) {}
4232

43-
/**
44-
* Enables or disables strict types checking.
45-
*
46-
* In case of $enabled is {@see null} a default value will be defined.
47-
*
48-
* @api
49-
*/
50-
public function withStrictTypes(?bool $enabled = null): static
51-
{
52-
$self = clone $this;
53-
$self->strictTypes = $enabled;
54-
55-
return $self;
56-
}
57-
58-
/**
59-
* Returns current {@see $strictTypes} option or default value
60-
* in case of option is not set.
61-
*
62-
* @api
63-
*/
64-
public function isStrictTypesEnabled(): bool
65-
{
66-
return $this->strictTypes ?? self::STRICT_TYPES_DEFAULT_VALUE;
67-
}
68-
6933
/**
7034
* Enables or disables object to arrays conversion.
7135
*
@@ -125,7 +89,6 @@ public function with(?Context $context): self
12589
}
12690

12791
return new self(
128-
strictTypes: $context->strictTypes ?? $this->strictTypes,
12992
objectsAsArrays: $context->objectsAsArrays ?? $this->objectsAsArrays,
13093
detailedTypes: $context->detailedTypes ?? $this->detailedTypes,
13194
);

src/Type/Context/LocalContext.php

-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ final class LocalContext extends Context implements ExecutionStackInterface
2222
final public function __construct(
2323
private readonly Direction $direction,
2424
private readonly RepositoryInterface $types,
25-
?bool $strictTypes = null,
2625
?bool $objectsAsArrays = null,
2726
?bool $detailedTypes = null,
2827
) {
2928
$this->path = new MutablePath();
3029
$this->trace = new MutablePath();
3130

3231
parent::__construct(
33-
strictTypes: $strictTypes,
3432
objectsAsArrays: $objectsAsArrays,
3533
detailedTypes: $detailedTypes,
3634
);
@@ -58,7 +56,6 @@ public function with(?Context $context): self
5856
return new self(
5957
direction: $local->direction,
6058
types: $local->types,
61-
strictTypes: $context->strictTypes ?? $this->strictTypes,
6259
objectsAsArrays: $context->objectsAsArrays ?? $this->objectsAsArrays,
6360
detailedTypes: $context->detailedTypes ?? $this->detailedTypes,
6461
);

src/Type/DateTimeType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function denormalize(mixed $value, LocalContext $context): \DateTimeInter
103103

104104
private function tryParseDateTime(string $value, Context $context): ?\DateTimeInterface
105105
{
106-
if ($context->isStrictTypesEnabled() && $this->format !== null) {
106+
if ($this->format !== null) {
107107
try {
108108
$result = ($this->class)::createFromFormat($this->format, $value);
109109
} catch (\Throwable) {

src/Type/FloatLiteralType.php

+3-15
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,20 @@ public function getTypeStatement(LocalContext $context): TypeStatement
2929
{
3030
$result = new FloatLiteralNode($this->value, $this->name);
3131

32-
if ($context->isDetailedTypes()) {
33-
return new NamedTypeNode(self::DEFAULT_TYPE_NAME, new TemplateArgumentsListNode([
34-
new TemplateArgumentNode($result),
35-
]));
36-
}
37-
38-
return $result;
32+
return new NamedTypeNode(self::DEFAULT_TYPE_NAME, new TemplateArgumentsListNode([
33+
new TemplateArgumentNode($result),
34+
]));
3935
}
4036

4137
#[\Override]
4238
public function match(mixed $value, LocalContext $context): bool
4339
{
44-
if (!$context->isStrictTypesEnabled()) {
45-
$value = $this->tryCastToFloat($value);
46-
}
47-
4840
return $value === (float) $this->value;
4941
}
5042

5143
#[\Override]
5244
public function cast(mixed $value, LocalContext $context): float
5345
{
54-
if (!$context->isStrictTypesEnabled()) {
55-
$value = $this->tryCastToFloat($value);
56-
}
57-
5846
if ($value === (float) $this->value) {
5947
return $value;
6048
}

0 commit comments

Comments
 (0)