Skip to content

Commit 5b27a56

Browse files
committed
feat(dossiq): build method-decomposition [skip ci]
Slice 8w: RecordCaseMapping's value shaping moves to Import/RecordFieldShape; its ExcessiveClassComplexity suppression goes. Assisted-by: ClaudeCode:claude-opus-5-5
1 parent 8336984 commit 5b27a56

12 files changed

Lines changed: 310 additions & 124 deletions

‎lib/Service/Import/RecordCaseMapping.php‎

Lines changed: 9 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@
3030

3131
namespace OCA\Dossiq\Service\Import;
3232

33-
use DateTimeInterface;
3433
use InvalidArgumentException;
3534
use OCA\Dossiq\Service\CaseDateNormaliser;
3635
use OCA\Dossiq\Service\Term\TermCarryOver;
3736

3837
/**
3938
* Maps one source record onto a case, by a case type's import declaration.
4039
*
41-
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity) The total is the sum of small private steps; each method stays under the method thresholds.
42-
*
4340
* @spec openspec/changes/woo-request-takes-over-from-opencatalogi/specs/case-record-import/spec.md#requirement-a-case-type-declares-how-another-apps-records-become-its-cases-req-cri-001
4441
*/
4542
class RecordCaseMapping {
@@ -54,11 +51,6 @@ class RecordCaseMapping {
5451
*/
5552
public const ANY_STATUS = '*';
5653

57-
/**
58-
* The words `as: boolean` reads as true.
59-
*/
60-
private const TRUE_WORDS = ['1', 'true', 'yes'];
61-
6254
/**
6355
* The term states a status may declare.
6456
*/
@@ -68,6 +60,13 @@ class RecordCaseMapping {
6860
TermCarryOver::STATE_CLOSED,
6961
];
7062

63+
/**
64+
* Puts a raw value in the shape its field declaration asks for.
65+
*
66+
* @var RecordFieldShape
67+
*/
68+
private readonly RecordFieldShape $shapes;
69+
7170
/**
7271
* Constructor.
7372
*
@@ -78,6 +77,7 @@ public function __construct(
7877
private readonly CaseDateNormaliser $dates,
7978
private readonly RecordValues $values = new RecordValues(),
8079
) {
80+
$this->shapes = new RecordFieldShape(dates: $this->dates, values: $this->values);
8181
}//end __construct()
8282

8383
/**
@@ -315,129 +315,14 @@ private function valueOf(array $field, RecordMappingRun $run): mixed {
315315
$raw = $run->recordId;
316316
}
317317

318-
$value = $this->shaped(field: $field, raw: $raw);
318+
$value = $this->shapes->shaped(field: $field, raw: $raw);
319319
if ($value === null && ($field['required'] ?? false) === true) {
320320
throw new InvalidArgumentException('The record has no readable ' . $from . '.');
321321
}
322322

323323
return $value;
324324
}//end valueOf()
325325

326-
/**
327-
* A raw value in the shape its declaration asks for.
328-
*
329-
* @param array<string, mixed> $field The field declaration.
330-
* @param mixed $raw The raw value.
331-
*
332-
* @return mixed The value, or null when empty or unreadable.
333-
*
334-
* @throws InvalidArgumentException When a value is outside its declared list.
335-
*/
336-
private function shaped(array $field, mixed $raw): mixed {
337-
$shape = (string)($field['as'] ?? 'text');
338-
if ($shape === 'json') {
339-
return $this->values->json(raw: $raw);
340-
}
341-
342-
if ($shape === 'boolean' && is_bool($raw) === true) {
343-
return $raw;
344-
}
345-
346-
$text = $this->textOf(raw: $raw);
347-
348-
if (isset($field['values']) === true) {
349-
return $this->listed(field: $field, text: $text);
350-
}
351-
352-
return $this->typed(field: $field, text: $text);
353-
}//end shaped()
354-
355-
/**
356-
* A scalar raw value as text with its whitespace collapsed; '' for anything else.
357-
*
358-
* @param mixed $raw The raw value.
359-
*
360-
* @return string The text.
361-
*/
362-
private function textOf(mixed $raw): string {
363-
$text = '';
364-
if (is_scalar($raw) === true) {
365-
$text = trim((string)preg_replace('/\s+/u', ' ', (string)$raw));
366-
}
367-
368-
return $text;
369-
}//end textOf()
370-
371-
/**
372-
* A text value in the shape its declaration asks for.
373-
*
374-
* @param array<string, mixed> $field The field declaration, with its shape (`as`, text when absent).
375-
* @param string $text The raw value as text.
376-
*
377-
* @return mixed The value, or null when empty or unreadable.
378-
*/
379-
private function typed(array $field, string $text): mixed {
380-
if ($text === '') {
381-
return null;
382-
}
383-
384-
return match ((string)($field['as'] ?? 'text')) {
385-
'date' => $this->dates->toCalendarDateOrNull(value: $text),
386-
'dateTime' => $this->dates->tryParse(value: $text)?->format(DateTimeInterface::ATOM),
387-
'integer' => (int)$text,
388-
'number' => $this->values->number(text: $text),
389-
'boolean' => in_array(strtolower($text), self::TRUE_WORDS, true),
390-
default => $this->cut(text: $text, length: (int)($field['maxLength'] ?? 0)),
391-
};
392-
}//end typed()
393-
394-
/**
395-
* A value read through its declared list.
396-
*
397-
* @param array<string, mixed> $field The field declaration, with `values` and an optional `default`.
398-
* @param string $text The raw value as text.
399-
*
400-
* @return mixed The listed value, or the default for an empty one.
401-
*
402-
* @throws InvalidArgumentException When the value is not in the list.
403-
*/
404-
private function listed(array $field, string $text): mixed {
405-
$values = (array)$field['values'];
406-
if ($text === '') {
407-
return ($field['default'] ?? null);
408-
}
409-
410-
if (array_key_exists($text, $values) === false) {
411-
throw new InvalidArgumentException(
412-
'The ' . (string)($field['from'] ?? '') . ' must be one of: ' . implode(', ', array_keys($values)) . '.'
413-
);
414-
}
415-
416-
return $values[$text];
417-
}//end listed()
418-
419-
/**
420-
* Text cut to a length at the last word that fits.
421-
*
422-
* @param string $text The text.
423-
* @param int $length The length, or 0 for no limit.
424-
*
425-
* @return string The text.
426-
*/
427-
private function cut(string $text, int $length): string {
428-
if ($length <= 0 || mb_strlen($text) <= $length) {
429-
return $text;
430-
}
431-
432-
$head = mb_substr($text, 0, $length + 1);
433-
$space = mb_strrpos($head, ' ');
434-
if ($space === false || $space === 0) {
435-
return mb_substr($text, 0, $length);
436-
}
437-
438-
return rtrim(mb_substr($head, 0, $space));
439-
}//end cut()
440-
441326
/**
442327
* Set a value at a dot path.
443328
*
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
/**
4+
* Dossiq record field shape.
5+
*
6+
* A raw value of a source record in the shape its field declaration asks
7+
* for: JSON, a boolean, a date, a moment, an integer, a number, a value from
8+
* a declared list, or text with its whitespace collapsed and cut at a word.
9+
*
10+
* @category Service
11+
* @package OCA\Dossiq\Service\Import
12+
*
13+
* @author Conduction Development Team <info@conduction.nl>
14+
* @copyright 2026 Conduction B.V.
15+
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
16+
*
17+
* @version GIT: <git-id>
18+
*
19+
* @link https://conduction.nl
20+
*
21+
* SPDX-License-Identifier: EUPL-1.2
22+
* SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl>
23+
*
24+
* @spec openspec/changes/method-decomposition/tasks.md
25+
*/
26+
27+
declare(strict_types=1);
28+
29+
namespace OCA\Dossiq\Service\Import;
30+
31+
use DateTimeInterface;
32+
use InvalidArgumentException;
33+
use OCA\Dossiq\Service\CaseDateNormaliser;
34+
35+
/**
36+
* One declared field's value, shaped; reads no store.
37+
*
38+
* @spec openspec/specs/case-record-import/spec.md#requirement-a-declaration-reads-nested-typed-source-values-and-answers-the-case-types-fields-req-cri-005
39+
* @spec openspec/changes/method-decomposition/tasks.md
40+
*/
41+
class RecordFieldShape {
42+
43+
/**
44+
* The words `as: boolean` reads as true.
45+
*/
46+
private const TRUE_WORDS = ['1', 'true', 'yes'];
47+
48+
/**
49+
* Constructor.
50+
*
51+
* @param CaseDateNormaliser $dates The one reader of a date and the tenant's time zone.
52+
* @param RecordValues $values Lists, numbers and answers as the case stores them.
53+
*/
54+
public function __construct(
55+
private readonly CaseDateNormaliser $dates,
56+
private readonly RecordValues $values = new RecordValues(),
57+
) {
58+
}//end __construct()
59+
60+
/**
61+
* A raw value in the shape its declaration asks for.
62+
*
63+
* @param array<string, mixed> $field The field declaration.
64+
* @param mixed $raw The raw value.
65+
*
66+
* @return mixed The value, or null when empty or unreadable.
67+
*
68+
* @throws InvalidArgumentException When a value is outside its declared list.
69+
*
70+
* @spec openspec/specs/case-record-import/spec.md#requirement-a-declaration-reads-nested-typed-source-values-and-answers-the-case-types-fields-req-cri-005
71+
* @spec openspec/changes/method-decomposition/tasks.md
72+
*/
73+
public function shaped(array $field, mixed $raw): mixed {
74+
$shape = (string)($field['as'] ?? 'text');
75+
if ($shape === 'json') {
76+
return $this->values->json(raw: $raw);
77+
}
78+
79+
if ($shape === 'boolean' && is_bool($raw) === true) {
80+
return $raw;
81+
}
82+
83+
$text = $this->textOf(raw: $raw);
84+
85+
if (isset($field['values']) === true) {
86+
return $this->listed(field: $field, text: $text);
87+
}
88+
89+
return $this->typed(field: $field, text: $text);
90+
}//end shaped()
91+
92+
/**
93+
* A scalar raw value as text with its whitespace collapsed; '' for anything else.
94+
*
95+
* @param mixed $raw The raw value.
96+
*
97+
* @return string The text.
98+
*/
99+
private function textOf(mixed $raw): string {
100+
$text = '';
101+
if (is_scalar($raw) === true) {
102+
$text = trim((string)preg_replace('/\s+/u', ' ', (string)$raw));
103+
}
104+
105+
return $text;
106+
}//end textOf()
107+
108+
/**
109+
* A text value in the shape its declaration asks for.
110+
*
111+
* @param array<string, mixed> $field The field declaration, with its shape (`as`, text when absent).
112+
* @param string $text The raw value as text.
113+
*
114+
* @return mixed The value, or null when empty or unreadable.
115+
*/
116+
private function typed(array $field, string $text): mixed {
117+
if ($text === '') {
118+
return null;
119+
}
120+
121+
return match ((string)($field['as'] ?? 'text')) {
122+
'date' => $this->dates->toCalendarDateOrNull(value: $text),
123+
'dateTime' => $this->dates->tryParse(value: $text)?->format(DateTimeInterface::ATOM),
124+
'integer' => (int)$text,
125+
'number' => $this->values->number(text: $text),
126+
'boolean' => in_array(strtolower($text), self::TRUE_WORDS, true),
127+
default => $this->cut(text: $text, length: (int)($field['maxLength'] ?? 0)),
128+
};
129+
}//end typed()
130+
131+
/**
132+
* A value read through its declared list.
133+
*
134+
* @param array<string, mixed> $field The field declaration, with `values` and an optional `default`.
135+
* @param string $text The raw value as text.
136+
*
137+
* @return mixed The listed value, or the default for an empty one.
138+
*
139+
* @throws InvalidArgumentException When the value is not in the list.
140+
*/
141+
private function listed(array $field, string $text): mixed {
142+
$values = (array)$field['values'];
143+
if ($text === '') {
144+
return ($field['default'] ?? null);
145+
}
146+
147+
if (array_key_exists($text, $values) === false) {
148+
throw new InvalidArgumentException(
149+
'The ' . (string)($field['from'] ?? '') . ' must be one of: ' . implode(', ', array_keys($values)) . '.'
150+
);
151+
}
152+
153+
return $values[$text];
154+
}//end listed()
155+
156+
/**
157+
* Text cut to a length at the last word that fits.
158+
*
159+
* @param string $text The text.
160+
* @param int $length The length, or 0 for no limit.
161+
*
162+
* @return string The text.
163+
*/
164+
private function cut(string $text, int $length): string {
165+
if ($length <= 0 || mb_strlen($text) <= $length) {
166+
return $text;
167+
}
168+
169+
$head = mb_substr($text, 0, $length + 1);
170+
$space = mb_strrpos($head, ' ');
171+
if ($space === false || $space === 0) {
172+
return mb_substr($text, 0, $length);
173+
}
174+
175+
return rtrim(mb_substr($head, 0, $space));
176+
}//end cut()
177+
}//end class

‎openspec/changes/method-decomposition/tasks.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,15 @@ characterisation test with a mapped ObjectService is a sufficient safety net; th
685685
refusal of the grammar), green before and unchanged after but for one `@uses` line. Own
686686
test: `tests/Unit/Service/Scoring/FormulaTokensTest` (3); giving a name the `num` kind turns
687687
12 tests red (checked).
688+
- [x] 8w `Import/RecordCaseMapping` (1: ExcessiveClassComplexity 51 against 50). Putting a raw source
689+
value in the shape its field declares (JSON, boolean, date, moment, integer, number, a listed
690+
value with its default and refusal, collapsed text cut at a word) moved to the new
691+
`Import/RecordFieldShape` (`shaped()`, with `textOf()`, `typed()`, `listed()`, `cut()` and
692+
`TRUE_WORDS`; bodies moved unchanged). The mapping keeps reading, the required check, the
693+
statuses, terms and the payload paths, and builds the shaper over its own date path and
694+
values in its constructor, so its constructor and every caller are unchanged.
695+
Characterisation, green on the single class first (own commit): 3 tests added to
696+
`RecordCaseMappingShapesTest` (6 in all) for the cut at a word and the hard cut, the listed
697+
value, its default and its refusal message, and dates, moments (written in the tenant's
698+
zone) and integers. Own test: `tests/Unit/Service/Import/RecordFieldShapeTest` (3);
699+
cutting one character short of the space turns 3 tests red (checked).

‎tests/Unit/Command/ImportCaseRecordsCommandTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @covers \OCA\Dossiq\Service\Import\RecordImportSteps
4545
* @uses \OCA\Dossiq\Service\Import\CaseRecordImport
4646
* @uses \OCA\Dossiq\Service\Import\RecordCaseMapping
47+
* @uses \OCA\Dossiq\Service\Import\RecordFieldShape
4748
* @uses \OCA\Dossiq\Service\Import\RecordImportStore
4849
* @uses \OCA\Dossiq\Service\Term\TermCarryOver
4950
* @uses \OCA\Dossiq\Service\CaseDateNormaliser

‎tests/Unit/Service/Import/CaseRecordImportTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
*
5252
* @covers \OCA\Dossiq\Service\Import\CaseRecordImport
5353
* @covers \OCA\Dossiq\Service\Import\RecordCaseMapping
54+
* @uses \OCA\Dossiq\Service\Import\RecordFieldShape
5455
* @covers \OCA\Dossiq\Service\Import\RecordImportStore
5556
* @covers \OCA\Dossiq\Service\Import\RecordValues
5657
* @covers \OCA\Dossiq\Service\Import\RecordImportReferences

‎tests/Unit/Service/Import/RecordCaseMappingShapesTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @covers \OCA\Dossiq\Service\Import\RecordCaseMapping
30+
* @uses \OCA\Dossiq\Service\Import\RecordFieldShape
3031
* @covers \OCA\Dossiq\Service\Import\RecordValues
3132
* @uses \OCA\Dossiq\Service\CaseDateNormaliser
3233
* @uses \OCA\Dossiq\Service\Import\RecordMappingRun

0 commit comments

Comments
 (0)