Files
peptide-reconstitution-calc…/includes/class-prc-math.php
peptiderepo 31ad0463f7 Adopt reusable CI standard + PHPUnit suite (#8)
* feat(ci): adopt estate CI + add PHPUnit suite from scratch (v1.2.0)

- Add PHPUnit 9.6 test suite (WP stubs, tests: stubs) — 40 tests across 4 files:
  MathTest: 25 tests covering all PRC_Math public methods and calculate()
  orchestrator with real peptide scenarios (BPC-157/TB-500/Semaglutide/MK-677/
  Ipamorelin) plus edge cases (zero water, negative inputs, floor rounding,
  rounding precision, mcg↔mg conversion).
  DefaultPresetsTest: 11 tests — all 8 expected slugs, required keys per preset
  (dataProvider), dose_unit validity, min<max range, source field, spot-checks.
  RestControllerTest: 7 tests — calculate() 200 + correct values (mcg+mg),
  get_presets() 200, get_preset() 200 for known slug / WP_Error for unknown.
  PresetProviderTest: 4 tests — fallback to defaults sans PR Core, slug key
  present, cache invalidation, idempotent invalidation.
- Add phpunit.xml.dist + tests/bootstrap.php (WP function/class stubs).
- Add composer test script: vendor/bin/phpunit --configuration phpunit.xml.dist.
- Add phpunit/phpunit ^9.6 + yoast/phpunit-polyfills ^2.0 to require-dev.
- Replace standalone ci.yml with thin caller to estate reusable workflow:
  uses: peptiderepo/peptide-e2e/.github/workflows/ci.yml@main
  with: tests: stubs, has_js: true
  permissions: contents: write (required for phpcbf commit-back)
  workflow_call (required for deploy.yml gate).
- deploy.yml unchanged — already gates on ./ci.yml + uses shared deploy-app.yml.
- Version bump 1.1.0 → 1.2.0.

Agent-Session: claude/adopt-ci-20260616

* style: auto-fix PHPCS violations via phpcbf

* fix(ci): resolve latent PHPCS + JS lint failures

Latent debt surfaced by the reusable PHPCS job (previously continue-on-error).

PHPCS fixes (all pre-existing, not introduced by this PR):
- Add @package PeptideReconstitutionCalculator to file docblocks in all 12 PHP
  files (previously missing; phpcbf cannot insert missing docblock tags).
- Replace two short-ternary ?: operators in class-prc-preset-provider.php with
  full ternary (Universal.Operators.DisallowShortTernary.Found; lines 136+152).

JS lint fix:
- Add package.json + package-lock.json (no deps; lint script = node --check)
  so the reusable lint-js job's npm ci / cache: npm steps succeed. The
  calculator ships vanilla JS with no build toolchain — package.json is the
  minimal stub the reusable workflow requires for has_js: true.

Agent-Session: claude/adopt-ci-20260616

* style: auto-fix PHPCS violations via phpcbf

* fix(phpcs): suppress intentional warnings with phpcs:ignore annotations

uninstall.php: direct wpdb->query() intentional in uninstall (no caching
available); add phpcs:ignore DirectDatabaseQuery.DirectQuery and NoCaching.
class-prc-rest-controller.php get_presets(): request param required by
WP REST API callback contract; add phpcs:ignore UnusedFunctionParameter.

Agent-Session: claude/adopt-ci-20260616

---------

Co-authored-by: peptiderepo <bot@peptiderepo.com>
Co-authored-by: peptiderepo <peptiderepo@users.noreply.github.com>
2026-06-17 01:25:11 +07:00

152 lines
4.7 KiB
PHP
Executable File

<?php
/**
* Reconstitution math engine.
*
* What: Pure calculation functions — no side effects, no database access.
* Computes concentration, injection volume, syringe units, and
* doses per vial from user inputs.
* Who calls it: PRC_Rest_Controller (server-side), also mirrored in JS.
* Dependencies: None.
*
* @see assets/js/calculator.js — Client-side mirror of this math.
* @see api/class-prc-rest-controller.php — Server-side consumer.
* @package PeptideReconstitutionCalculator
*/
declare(strict_types=1);
/**
* Static math functions for reconstitution calculations.
*
* All methods are pure — same inputs always produce same outputs.
* The JS calculator mirrors this logic client-side for instant feedback;
* the REST endpoint provides server-side validation.
*/
class PRC_Math {
/**
* Standard insulin syringe size in units (100 IU = 1 mL).
*
* Mirrored in: assets/js/calculator.js (SYRINGE_UNITS_PER_ML)
* and prcConfig.syringeUnits (class-prc-shortcode.php).
* All three MUST stay in sync.
*/
private const SYRINGE_UNITS_PER_ML = 100;
/**
* Run the full reconstitution calculation.
*
* @param float $vial_mg Amount of peptide in the vial (mg).
* @param float $water_ml Bacteriostatic water added (mL).
* @param float $desired_dose Desired dose per injection.
* @param string $dose_unit Unit of desired_dose: 'mcg' or 'mg'.
* @return array<string, mixed> Calculation results.
*/
public static function calculate(
float $vial_mg,
float $water_ml,
float $desired_dose,
string $dose_unit
): array {
// Normalize desired dose to mg for consistent math.
$dose_mg = ( 'mcg' === $dose_unit )
? $desired_dose / 1000
: $desired_dose;
$concentration_mg_per_ml = self::concentration_mg_per_ml( $vial_mg, $water_ml );
$concentration_mcg_per_unit = self::concentration_mcg_per_unit( $vial_mg, $water_ml );
$injection_ml = self::injection_volume_ml( $dose_mg, $concentration_mg_per_ml );
$syringe_units = self::syringe_units( $injection_ml );
$doses_per_vial = self::doses_per_vial( $vial_mg, $dose_mg );
return array(
'concentration_mg_per_ml' => round( $concentration_mg_per_ml, 4 ),
'concentration_mcg_per_unit' => round( $concentration_mcg_per_unit, 2 ),
'injection_volume_ml' => round( $injection_ml, 4 ),
'syringe_units' => round( $syringe_units, 1 ),
'doses_per_vial' => floor( $doses_per_vial ),
'total_peptide_mg' => $vial_mg,
'water_ml' => $water_ml,
'desired_dose_mg' => round( $dose_mg, 4 ),
'desired_dose_display' => $desired_dose,
'dose_unit' => $dose_unit,
);
}
/**
* Concentration after reconstitution in mg/mL.
*
* @param float $vial_mg Peptide amount (mg).
* @param float $water_ml Water volume (mL).
* @return float mg per mL.
*/
public static function concentration_mg_per_ml( float $vial_mg, float $water_ml ): float {
if ( $water_ml <= 0 ) {
return 0;
}
return $vial_mg / $water_ml;
}
/**
* Concentration in mcg per insulin syringe unit.
*
* Why: Most users measure with insulin syringes (100 units = 1 mL).
* Knowing mcg-per-unit makes it easy to count tick marks.
*
* @param float $vial_mg Peptide amount (mg).
* @param float $water_ml Water volume (mL).
* @return float mcg per syringe unit.
*/
public static function concentration_mcg_per_unit( float $vial_mg, float $water_ml ): float {
if ( $water_ml <= 0 ) {
return 0;
}
$total_mcg = $vial_mg * 1000;
$total_units = $water_ml * self::SYRINGE_UNITS_PER_ML;
return $total_mcg / $total_units;
}
/**
* Volume to inject for a desired dose (mL).
*
* @param float $dose_mg Desired dose in mg.
* @param float $concentration_mg_ml Solution concentration in mg/mL.
* @return float Injection volume in mL.
*/
public static function injection_volume_ml( float $dose_mg, float $concentration_mg_ml ): float {
if ( $concentration_mg_ml <= 0 ) {
return 0;
}
return $dose_mg / $concentration_mg_ml;
}
/**
* Convert mL to insulin syringe units.
*
* @param float $ml Volume in mL.
* @return float Syringe units (100 units = 1 mL).
*/
public static function syringe_units( float $ml ): float {
return $ml * self::SYRINGE_UNITS_PER_ML;
}
/**
* How many full doses fit in one vial.
*
* @param float $vial_mg Total peptide in vial (mg).
* @param float $dose_mg Dose per injection (mg).
* @return float Number of doses (floor to get whole doses).
*/
public static function doses_per_vial( float $vial_mg, float $dose_mg ): float {
if ( $dose_mg <= 0 ) {
return 0;
}
return $vial_mg / $dose_mg;
}
}