Skip to content

Commit 12e1d17

Browse files
committed
refactor(class-variance): fixing mago errors
1 parent aa0f655 commit 12e1d17

6 files changed

Lines changed: 52 additions & 23 deletions

File tree

packages/class-variance/src/ClassNames.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private function __construct(
2929
* Passing $slot = '' is the passthrough context (e.g. extra $props['class'])
3030
* and always emits the classes regardless of slot.
3131
*
32-
* @param string|array<array-key, mixed>|bool $input
32+
* @param string|list<string|array<string, mixed>|bool>|array<string, string|array<string, mixed>|bool>|bool $input
3333
*/
3434
public static function of(string|array|bool $input, string $slot = ''): self
3535
{

packages/class-variance/src/Classmaps/Classmap.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ public function findGroup(string $class): ?string
6060
continue;
6161
}
6262

63-
if (! is_array($matcher)) {
64-
continue;
65-
}
66-
6763
if (array_is_list($matcher)) {
6864
// ['prefix'] — wildcard: matches prefix itself or prefix-{anything}
6965
$prefix = $matcher[0];
@@ -73,7 +69,8 @@ public function findGroup(string $class): ?string
7369
}
7470
} else {
7571
// ['prefix' => ['suffix1', 'suffix2']] — constrained suffix list
76-
$prefix = array_key_first($matcher);
72+
$prefix = (string) array_key_first($matcher);
73+
/** @var list<string> $suffixes */
7774
$suffixes = $matcher[$prefix];
7875

7976
foreach ($suffixes as $suffix) {
@@ -102,7 +99,9 @@ public function extend(self $additions): self
10299
$groups = $this->classGroups;
103100

104101
foreach ($additions->classGroups as $groupId => $matchers) {
105-
$groups[$groupId] = array_merge($groups[$groupId] ?? [], $matchers);
102+
/** @var list<string|array<string, list<string>>|array{0: string}> $merged */
103+
$merged = array_merge($groups[$groupId] ?? [], $matchers);
104+
$groups[$groupId] = $merged;
106105
}
107106

108107
$conflicts = $this->conflictingClassGroups;
@@ -123,9 +122,11 @@ public function extend(self $additions): self
123122
*/
124123
public function override(self $replacements): self
125124
{
126-
return new self(
127-
array_replace($this->classGroups, $replacements->classGroups),
128-
array_replace($this->conflictingClassGroups, $replacements->conflictingClassGroups),
129-
);
125+
/** @var array<string, list<string|array<string, list<string>>|array{0: string}>> $newGroups */
126+
$newGroups = array_replace($this->classGroups, $replacements->classGroups);
127+
/** @var array<string, list<string>> $newConflicts */
128+
$newConflicts = array_replace($this->conflictingClassGroups, $replacements->conflictingClassGroups);
129+
130+
return new self($newGroups, $newConflicts);
130131
}
131132
}

packages/class-variance/src/Config/TailwindClassVarianceConfig.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ public function __construct(
3232
get {
3333
$groups = TailwindClassmap::default();
3434

35-
if ($this->extend !== null) {
36-
$groups = $groups->extend($this->extend);
35+
$extend = $this->extend;
36+
if ($extend instanceof Classmap) {
37+
$groups = $groups->extend($extend);
3738
}
3839

39-
if ($this->override !== null) {
40-
$groups = $groups->override($this->override);
40+
$override = $this->override;
41+
if ($override instanceof Classmap) {
42+
$groups = $groups->override($override);
4143
}
4244

4345
return new GroupClassMerger($groups, $this->prefix, $this->separator);

packages/class-variance/src/GroupClassMerger.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public function merge(string ...$classes): string
7474
*
7575
* Example: 'dark:hover:bg-red-500' → ['dark:hover', 'bg-red-500']
7676
* Example: 'p-[color:red]' → ['', 'p-[color:red]']
77+
*
78+
* @return array{0: string, 1: string}
7779
*/
7880
private function extractModifiers(string $class): array
7981
{

packages/class-variance/src/ResolvesVariants.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@
99
/**
1010
* Core variant resolution engine, shared by all ClassVariance implementations.
1111
*
12-
* The class using this trait must declare the following public readonly properties:
12+
* The class using this trait must declare the following public readable properties:
1313
* - array|string $base
1414
* - ClassMerger $merger
1515
* - array $variants
1616
* - array $compoundVariants
1717
* - array $defaultVariants
1818
*
19+
* @property-read array|string $base
20+
* @property-read ClassMerger $merger
21+
* @property-read array $variants
22+
* @property-read array $compoundVariants
23+
* @property-read array $defaultVariants
24+
*
1925
* @internal
2026
*/
2127
trait ResolvesVariants
2228
{
29+
/** @param array<string, string|bool> $props */
2330
public function __invoke(array $props = [], string $slot = ''): string
2431
{
2532
$props = $this->applyDefaultVariants($props);
@@ -33,10 +40,16 @@ public function __invoke(array $props = [], string $slot = ''): string
3340
return $this->merger->merge(...$classes->toArray());
3441
}
3542

36-
/** @param array<string, string|bool> $props */
43+
/**
44+
* @param array<string, string|bool> $props
45+
* @return array<string, string|bool>
46+
*/
3747
private function applyDefaultVariants(array $props): array
3848
{
39-
foreach ($this->defaultVariants as $key => $value) {
49+
/** @var array<string, string|bool> $defaults */
50+
$defaults = $this->defaultVariants;
51+
52+
foreach ($defaults as $key => $value) {
4053
$props[$key] ??= $value;
4154
}
4255

@@ -76,6 +89,7 @@ private function resolveSlot(string $slot): string
7689
*/
7790
private function resolvePassthrough(array $props, string $slot): ClassNames
7891
{
92+
/** @var string|array<array-key, mixed>|bool $value */
7993
$value = $props['class'] ?? $props['className'] ?? '';
8094

8195
if (is_bool($value) || $value === '') {
@@ -98,8 +112,12 @@ private function resolveVariants(array $props, string $slot): ClassNames
98112
{
99113
$classes = ClassNames::empty();
100114

115+
/** @var array<string, array<string, string|array<string, mixed>>> $variants */
116+
$variants = $this->variants;
117+
101118
foreach ($props as $key => $value) {
102-
$entry = $this->variants[$key][$value] ?? null;
119+
$lookupKey = is_bool($value) ? ($value ? 'true' : 'false') : $value;
120+
$entry = $variants[$key][$lookupKey] ?? null;
103121

104122
if ($entry === null) {
105123
continue;
@@ -118,11 +136,15 @@ private function resolveCompoundVariants(array $props, string $slot): ClassNames
118136
{
119137
$classes = ClassNames::empty();
120138

121-
foreach ($this->compoundVariants as $compound) {
139+
/** @var array<int, array<string, mixed>> $compoundVariants */
140+
$compoundVariants = $this->compoundVariants;
141+
142+
foreach ($compoundVariants as $compound) {
122143
if (! $this->compoundMatches($props, $compound)) {
123144
continue;
124145
}
125146

147+
/** @var string|array<string, mixed>|bool $classValue */
126148
$classValue = $compound['class'] ?? $compound['className'] ?? '';
127149
$classes = $classes->concat(ClassNames::of($classValue, $slot));
128150
}
@@ -137,10 +159,12 @@ private function resolveCompoundVariants(array $props, string $slot): ClassNames
137159
private function compoundMatches(array $props, array $compound): bool
138160
{
139161
foreach ($compound as $key => $value) {
140-
if ($key === 'class' || $key === 'className') {
162+
if ($key === 'class') {
163+
continue;
164+
}
165+
if ($key === 'className') {
141166
continue;
142167
}
143-
144168
if (is_array($value)) {
145169
if (! isset($props[$key]) || ! in_array($props[$key], $value, strict: true)) {
146170
return false;

packages/class-variance/src/SeparatorClassMerger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function merge(string ...$classes): string
4949
private function resolveGroup(string $class): string
5050
{
5151
// Explicit group map takes priority over separator heuristic.
52-
if ($this->classGroups !== null) {
52+
if ($this->classGroups instanceof Classmap) {
5353
$group = $this->classGroups->findGroup($class);
5454

5555
if ($group !== null) {

0 commit comments

Comments
 (0)