Skip to content

Fix #53: funnel response-shape checks through one expectKey() helper - #107

Open
corgab wants to merge 2 commits into
mainfrom
fix/53-response-shape-helper
Open

Fix #53: funnel response-shape checks through one expectKey() helper#107
corgab wants to merge 2 commits into
mainfrom
fix/53-response-shape-helper

Conversation

@corgab

@corgab corgab commented Sep 8, 2026

Copy link
Copy Markdown
Owner

Summary

Five call sites in AbstractQuantumDriver repeated the same check on decoded Python output: key present, value of the right type, else throw QuantumExecutionException::malformedResponse() with a hand-written message. Only the script, the key and the type test differed. This PR extracts the helper the issue proposes.

Plan

  1. Add a private expectKey(array $response, string $script, string $key, Closure $isValid, string $expected, string $subject = 'the response'): mixed that performs the presence-and-shape check, throws the shared exception with one wording, and returns the validated value.
  2. Collapse the results, per-item counts, circuit counts, task_arn, status and bits checks onto it, each passing its own predicate (is_array(...), is_string(...), non-empty string, valid TaskStatus).
  3. Leave the checks that are not shape checks inline: the batch result count, a batch item that is not an object (now reported with its index), and the entropy bit length.

Changes

  • src/Drivers/AbstractQuantumDriver.php: expectKey() added next to payload(); six checks rewritten as one call each. The message is uniform: expected <subject> to have a "<key>" key holding <expected>., with <subject> defaulting to the response and set to result #<n> for batch items. pollTask() hands the validated status to the snapshot instead of letting it re-parse the key. The @var annotations on counts say array<mixed>, which is what is_array() proves; CircuitResult owns the value shape as before.
  • src/Tasks/TaskSnapshot.php: fromResponse() accepts an optional pre-validated TaskStatus so the guard and the snapshot agree on the value.
  • tests/Unit/Drivers/AbstractQuantumDriverTest.php: assertions updated to the unified wording; new tests pin the full message for a wrong-type key, prove an absent, null and wrong-type key are all reported the same way, and name the offending batch item. tests/Unit/Tasks/TaskSnapshotTest.php: the pre-validated status is carried as given.

Tests

  • vendor/bin/pint --test passes.
  • vendor/bin/pest --compact: 797 tests, 1 skipped (791 before: +6).

Closes #53

…() helper

Five call sites in AbstractQuantumDriver repeated the same check on decoded
Python output: key present, value of the right type, else throw
QuantumExecutionException::malformedResponse() with a hand-written message.
expectKey() now performs the check and words the message once
("expected <subject> to have a \"<key>\" key holding <expected>"), so each
call site states only the script, the key, the predicate and the expected
shape. The count and length checks that are not shape checks stay inline.

Closes #53
…h item, and stop over-claiming counts types

pollTask() now hands the status expectKey() validated to
TaskSnapshot::fromResponse() instead of letting the snapshot re-parse the
key. A batch item that is not an object gets its own message with its
index, and the per-item counts check names the item too. The @var
annotations on counts say array<mixed>, which is what is_array() proves;
CircuitResult owns the value shape.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[maintainability] Response-shape validation ("key present + right type") is copy-pasted five times in AbstractQuantumDriver

1 participant