feature-ninja/cva
PHP implementation of Class Variance Authority (cva) for building composable CSS class strings. Define base classes, variants, compound variants, and defaults, then generate Tailwind-friendly class names via ClassVarianceAuthority or the cva() helper.
Repository<T extends Model>) or polymorphic relationships.Cva\Generic<User>).Service<T>).cva() helper (added in PR #12) simplifies runtime type enforcement, bridging the gap between compile-time generics and dynamic PHP. This is valuable for:
cva($user, User::class)).assertInstanceOf(User::class, cva($user))).cva() helper adds a new abstraction layer. Developers must understand:
cva() helper add value beyond existing generics? (e.g., dynamic polymorphism, runtime validation).##[Assert\Type]) or Laravel’s ValidatesWhen replace the helper?cva() to validate DTOs in ->resolve() (e.g., cva($request->input(), UserRequest::class)).cva($data['user'], User::class)).instanceof checks with cva() in 1–2 high-risk areas (e.g., API input validation).cva() for legacy or dynamic cases.// Old: Manual check
if (!$user instanceof User) throw new InvalidArgumentException();
// New: Runtime helper
$user = cva($user, User::class); // Throws if invalid
assertThrows for invalid types).| Phase | Task | Dependencies |
|---|---|---|
| Assessment | Audit codebase for instanceof/is_a() checks. |
None |
| Setup | Install package, add cva() to composer.json aliases. |
PHP 8.1+, PHPStan/Psalm |
| Pilot | Replace 1–2 instanceof checks with cva() in Form Requests/APIs. |
Team buy-in |
| Rollout | Expand to: |
instanceof with generics where possible. | Full adoption |cva() calls explicitly declare expected types.cva() vs. generics tradeoffs.cva() for dynamic data, generics for static code).cva() failures (e.g., InvalidArgumentException with type hints).| Risk | Mitigation Strategy |
|---|---|
| Runtime Type Errors | Add cva() to CI tests; use @throws annotations. |
| Overuse of Helper | Enforce a code review rule for cva() usage. |
| Legacy Code Incompatibility | Use @phpstan-ignore for non-generic code; phase out over time. |
| IDE Autocomplete Issues | Configure PHPStorm for cva() (treat as type assertion). |
| Performance Bottlenecks | Benchmark critical paths; avoid cva() in hot loops. |
cva() to a single Form Request/API.instanceof checks in 2–3 modules; gather feedback.instanceof in favor of cva() or generics.How can I help you explore Laravel packages today?