From 9dabb43e2f770aec9e15276cca2273ddeb48fe5c Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 17 Nov 2026 23:29:11 +0400 Subject: [PATCH 1/6] feat(core): fetch attributes of every enum case through Reflection Every case is present in the result, in declaration order, so a caller can walk an enum once and fall back to a default where a case carries nothing. Assisted-By: Claude Opus 5.5 --- core/Common/Reflection.php | 31 +++++++++++++++++++++++ tests/Common/Stub/CaseMarkerAttribute.php | 13 ++++++++++ tests/Common/Stub/MarkedEnum.php | 16 ++++++++++++ tests/Common/Unit/ReflectionTest.php | 30 ++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 tests/Common/Stub/CaseMarkerAttribute.php create mode 100644 tests/Common/Stub/MarkedEnum.php diff --git a/core/Common/Reflection.php b/core/Common/Reflection.php index b399a759..f291dd72 100644 --- a/core/Common/Reflection.php +++ b/core/Common/Reflection.php @@ -175,6 +175,37 @@ public static function fetchTraits( return \array_unique($traits); } + /** + * Fetch the attributes of every case of an enum. + * + * Every case is present in the result, in declaration order, so a caller can walk the enum once and + * fall back to a default where a case carries nothing. + * + * @template T of object + * + * @param \ReflectionEnum|class-string<\UnitEnum> $enum + * @param class-string|null $attributeClass If provided, only attributes of this class will be returned. + * @param int $flags Flags to pass to {@see \ReflectionEnumUnitCase::getAttributes()}. + * + * @return ($attributeClass is null + * ? array> + * : array>>) Attributes keyed by case name. + */ + public static function fetchEnumCaseAttributes( + \ReflectionEnum|string $enum, + ?string $attributeClass = null, + int $flags = 0, + ): array { + \is_string($enum) and $enum = new \ReflectionEnum($enum); + + $attributes = []; + foreach ($enum->getCases() as $case) { + $attributes[$case->name] = $case->getAttributes($attributeClass, $flags); + } + + return $attributes; + } + /** * Find all methods in a class that have a specific attribute. * diff --git a/tests/Common/Stub/CaseMarkerAttribute.php b/tests/Common/Stub/CaseMarkerAttribute.php new file mode 100644 index 00000000..9b8285a6 --- /dev/null +++ b/tests/Common/Stub/CaseMarkerAttribute.php @@ -0,0 +1,13 @@ += 1); } + + // --- fetchEnumCaseAttributes --- + + public function fetchEnumCaseAttributes_listsEveryCaseInDeclarationOrder(): void + { + $attributes = Reflection::fetchEnumCaseAttributes(MarkedEnum::class, CaseMarkerAttribute::class); + + Assert::same(\array_keys($attributes), ['Marked', 'Plain', 'Repeated']); + Assert::same($attributes['Plain'], []); + Assert::same( + \array_map(static fn(\ReflectionAttribute $a) => $a->newInstance()->label, $attributes['Repeated']), + ['second', 'third'], + ); + } + + public function fetchEnumCaseAttributes_acceptsReflectionEnumInstance(): void + { + $attributes = Reflection::fetchEnumCaseAttributes(new \ReflectionEnum(MarkedEnum::class), CaseMarkerAttribute::class); + + Assert::same($attributes['Marked'][0]->newInstance()->label, 'first'); + } + + public function fetchEnumCaseAttributes_withNullAttributeClass_returnsAllAttributes(): void + { + $attributes = Reflection::fetchEnumCaseAttributes(MarkedEnum::class); + + Assert::same(\array_map(\count(...), $attributes), ['Marked' => 1, 'Plain' => 0, 'Repeated' => 2]); + } } From ac15795ab9aeda3ff95ffd09ab423e346ead94c4 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 17 Nov 2026 23:29:14 +0400 Subject: [PATCH 2/6] feat(metric): add a unit-aware metric value with attribute-declared unit families A `Metric` carries its number in the unit it was measured in, so each reporter renders the unit its own way instead of parsing it back out of a key. A family is a backed enum: `#[Dimension]` names what it measures, `#[Factor]` on a case gives its multiplier to the base unit, and `Units` reads both once per enum. Conversion stays integral while every operand is an integer, so `2 KiB` is `2048` bytes rather than `2048.0`; `compact()` picks the most readable unit from the factors alone. `Testo\Core\Metric` ships the built-in families: `Time`, `Memory` (binary `KiB`/`MiB`/`GiB`), `Percent`, `Scalar`. Assisted-By: Claude Opus 5.5 --- core/Core/Metric/Memory.php | 34 ++++ core/Core/Metric/Percent.php | 21 +++ core/Core/Metric/Scalar.php | 21 +++ core/Core/Metric/Time.php | 33 ++++ core/Metric/Dimension.php | 25 +++ core/Metric/Exception/IncompatibleUnits.php | 26 +++ .../Exception/InvalidUnitDeclaration.php | 30 ++++ core/Metric/Factor.php | 26 +++ core/Metric/Metric.php | 52 ++++++ core/Metric/Unit.php | 33 ++++ core/Metric/UnitConversion.php | 25 +++ core/Metric/UnitFamily.php | 58 +++++++ core/Metric/Units.php | 122 ++++++++++++++ testo.php | 1 + tests/Core/Metric/BuiltinUnitsTest.php | 53 ++++++ tests/Core/suites.php | 6 + tests/Metric/Fixture/Distance.php | 27 ++++ tests/Metric/Fixture/NegativeFactor.php | 18 +++ tests/Metric/Fixture/NotAUnit.php | 13 ++ tests/Metric/Fixture/Plain.php | 18 +++ tests/Metric/MetricTest.php | 42 +++++ tests/Metric/UnitsTest.php | 152 ++++++++++++++++++ tests/Metric/suites.php | 19 +++ 23 files changed, 855 insertions(+) create mode 100644 core/Core/Metric/Memory.php create mode 100644 core/Core/Metric/Percent.php create mode 100644 core/Core/Metric/Scalar.php create mode 100644 core/Core/Metric/Time.php create mode 100644 core/Metric/Dimension.php create mode 100644 core/Metric/Exception/IncompatibleUnits.php create mode 100644 core/Metric/Exception/InvalidUnitDeclaration.php create mode 100644 core/Metric/Factor.php create mode 100644 core/Metric/Metric.php create mode 100644 core/Metric/Unit.php create mode 100644 core/Metric/UnitConversion.php create mode 100644 core/Metric/UnitFamily.php create mode 100644 core/Metric/Units.php create mode 100644 tests/Core/Metric/BuiltinUnitsTest.php create mode 100644 tests/Metric/Fixture/Distance.php create mode 100644 tests/Metric/Fixture/NegativeFactor.php create mode 100644 tests/Metric/Fixture/NotAUnit.php create mode 100644 tests/Metric/Fixture/Plain.php create mode 100644 tests/Metric/MetricTest.php create mode 100644 tests/Metric/UnitsTest.php create mode 100644 tests/Metric/suites.php diff --git a/core/Core/Metric/Memory.php b/core/Core/Metric/Memory.php new file mode 100644 index 00000000..1f58cb00 --- /dev/null +++ b/core/Core/Metric/Memory.php @@ -0,0 +1,34 @@ +name, + $to::class, + $to->name, + )); + } +} diff --git a/core/Metric/Exception/InvalidUnitDeclaration.php b/core/Metric/Exception/InvalidUnitDeclaration.php new file mode 100644 index 00000000..8d9ffa10 --- /dev/null +++ b/core/Metric/Exception/InvalidUnitDeclaration.php @@ -0,0 +1,30 @@ +` is distinct from a `Metric