Skip to content

Commit d91be2d

Browse files
committed
Simplify Field Interface
1 parent 7f26820 commit d91be2d

9 files changed

Lines changed: 10 additions & 130 deletions

src/Schema/CustomFieldTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,6 @@ public function testEvaluateUsesParse(): void
6767
// score()
6868
// --------------------------------------------------------
6969

70-
public function testScoreUsesCustomParsingLogic(): void
71-
{
72-
$field = new CustomField(
73-
fn ($value) => is_int($value) ? $value : null,
74-
'custom',
75-
);
76-
77-
$score = $field->score([
78-
1,
79-
2,
80-
'3',
81-
null,
82-
'',
83-
]);
84-
85-
// valid = 2 (1,2)
86-
// counted = 3 (1,2,'3')
87-
self::assertSame(2 / 3, $score);
88-
}
89-
9070
// --------------------------------------------------------
9171
// type()
9272
// --------------------------------------------------------

src/Schema/Field.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,6 @@ public function name(): string;
2929
*/
3030
public function confidenceThreshold(): float;
3131

32-
/**
33-
* Scores a sample of values to estimate type confidence.
34-
*
35-
* IMPORTANT:
36-
* The iterable is expected to be a *representative sample* of the dataset,
37-
* not the full dataset. Implementations may assume limited size for performance.
38-
*
39-
* @param iterable $values Sample of dataset values
40-
*/
41-
public function score(iterable $values): float;
42-
4332
/**
4433
* Score a single value to estimate its type.
4534
*

src/Schema/FieldEvaluator.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,6 @@ final protected static function filterConfidenceThreshold(float $confidenceThres
3939
return $confidenceThreshold;
4040
}
4141

42-
public function score(iterable $values): float
43-
{
44-
$valid = 0;
45-
$counted = 0;
46-
47-
foreach ($values as $value) {
48-
$eval = $this->evaluate($value);
49-
if (0 === $eval) {
50-
continue;
51-
}
52-
53-
$counted++;
54-
if (1 === $eval) {
55-
$valid++;
56-
}
57-
}
58-
59-
return 0 < $counted ? $valid / $counted : 0.0;
60-
}
61-
6242
/**
6343
* @return int<-1, 1>
6444
*/

src/Schema/FieldEvaluatorTest.php

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -70,56 +70,6 @@ public function testEvaluateReturnsMinusOneForInvalidValue(): void
7070

7171
self::assertSame(-1, $field->evaluate('invAlid'));
7272
}
73-
74-
// --------------------------------------------------------
75-
// score()
76-
// --------------------------------------------------------
77-
78-
public function testScoreIgnoresNullAndEmptyValues(): void
79-
{
80-
$field = new DummyField();
81-
82-
$score = $field->score([
83-
null,
84-
'',
85-
' ',
86-
'valid-1',
87-
'invalid',
88-
]);
89-
90-
self::assertSame(1.0, $score);
91-
}
92-
93-
public function testScoreReturnsZeroWhenNoCountedValues(): void
94-
{
95-
$field = new DummyField();
96-
97-
self::assertSame(0.0, $field->score([
98-
null,
99-
'',
100-
' ',
101-
]));
102-
}
103-
104-
public function testScoreIsPerfectWhenAllValid(): void
105-
{
106-
$field = new DummyField();
107-
108-
self::assertSame(1.0, $field->score([
109-
'valid-1',
110-
'valid-2',
111-
]));
112-
}
113-
114-
public function testScoreIsZeroWhenAllInvalid(): void
115-
{
116-
$field = new DummyField();
117-
118-
self::assertSame(0.0, $field->score([
119-
'invAlid-1',
120-
'invAlid-2',
121-
]));
122-
}
12373
}
12474

12575
final class DummyField extends FieldEvaluator

src/Schema/FieldListTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ public function confidenceThreshold(): float
5858
return 0.5;
5959
}
6060

61-
public function score(iterable $values): float
62-
{
63-
return 1.0;
64-
}
65-
6661
public function parse(mixed $value): mixed
6762
{
6863
return $value;

src/Schema/NumericField.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace League\Csv\Schema;
1515

16+
use ValueError;
17+
1618
use function filter_var;
1719
use function is_float;
1820
use function is_int;
@@ -30,7 +32,7 @@ public function __construct(
3032
float $confidenceThreshold = 0.8
3133
) {
3234
if (null !== $min && null !== $max && $min > $max) {
33-
throw new \ValueError('Minimum length can not be greater than maximum length.');
35+
throw new ValueError('Minimum length can not be greater than maximum length.');
3436
}
3537

3638
parent::__construct($confidenceThreshold);

src/Schema/StringField.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,15 @@ public function metadata(): FieldMetadata
126126
*/
127127
public function evaluate(mixed $value): int
128128
{
129-
return match (true) {
130-
null !== $this->constraint => parent::evaluate($value),
131-
is_string($value) => 1,
132-
default => 0,
133-
};
134-
}
135-
136-
public function score(iterable $values): float
137-
{
138-
return null === $this->constraint ? 1.0 : parent::score($values);
129+
return null === $this->constraint
130+
? (is_string($value) ? 1 : 0)
131+
: parent::evaluate($value);
139132
}
140133

141134
public function confidenceThreshold(): float
142135
{
143-
return null === $this->constraint ? 0.0 : parent::confidenceThreshold();
136+
return null === $this->constraint
137+
? 0.0
138+
: parent::confidenceThreshold();
144139
}
145140
}

src/Schema/StringFieldTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,6 @@ public function testEvaluate(mixed $input, int $expected): void
9393
self::assertSame($expected, $this->field->evaluate($input));
9494
}
9595

96-
// --------------------------------------------------------
97-
// score()
98-
// --------------------------------------------------------
99-
100-
public function testScoreAlwaysReturnsOne(): void
101-
{
102-
self::assertSame(1.0, $this->field->score(['a', 'b', 'c']));
103-
self::assertSame(1.0, $this->field->score([1, 2, 3]));
104-
self::assertSame(1.0, $this->field->score([]));
105-
}
106-
10796
// --------------------------------------------------------
10897
// type()
10998
// --------------------------------------------------------

src/Schema/StringLengthConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function fieldTypeName(): string
8989
$range = (null === $this->min && null === $this->max)
9090
? '' :
9191
(
92-
$this->min === $this->max
92+
$this->min === $this->max
9393
? '['.$this->min.']'
9494
: '['.$this->min.','.$this->max.']'
9595
);

0 commit comments

Comments
 (0)