Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
goranalkovic-infinum committed Oct 1, 2024
1 parent 1c760ee commit 8d901d2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Cli/CliHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function getShortenCliPathOutput(string $path, string $ref = 'projectR
protected function getFolderItems(string $path): array
{
$output = \array_diff(\scandir($path), ['..', '.']);
$output = \array_values($output);
$output = \array_values($output); // @phpstan-ignore-line

return $output;
}
Expand Down
85 changes: 62 additions & 23 deletions src/Helpers/TailwindTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public static function getTwBreakpoints($desktopFirst = false)
}

/**
* @deprecated 9.2.0 Use `tailwindClasses` instead.
*
* Gets Tailwind classes for the provided part.
*
* The part needs to be defined within the manifest, in the `tailwind` object.
Expand All @@ -53,6 +51,8 @@ public static function getTwBreakpoints($desktopFirst = false)
* @param array<mixed> $manifest Component/block manifest data.
* @param array<string> ...$custom Additional custom classes.
*
* @deprecated 9.2.0 Use `tailwindClasses` instead.
*
* @return string
*/
public static function getTwPart($part, $manifest, ...$custom)
Expand All @@ -71,8 +71,6 @@ public static function getTwPart($part, $manifest, ...$custom)
}

/**
* @deprecated 9.2.0 Use `tailwindClasses` instead.
*
* Gets Tailwind classes for the provided dynamic part.
*
* The part needs to be defined within the manifest, in the `tailwind` object.
Expand All @@ -82,6 +80,8 @@ public static function getTwPart($part, $manifest, ...$custom)
* @param array<mixed> $manifest Component/block manifest data.
* @param array<string> ...$custom Additional custom classes.
*
* @deprecated 9.2.0 Use `tailwindClasses` instead.
*
* @return string
*/
public static function getTwDynamicPart($part, $attributes, $manifest, ...$custom)
Expand Down Expand Up @@ -171,14 +171,14 @@ public static function getTwDynamicPart($part, $attributes, $manifest, ...$custo
}

/**
* @deprecated 9.2.0 Use `tailwindClasses` instead.
*
* Get Tailwind classes for the given component/block.
*
* @param array<mixed> $attributes Component/block attributes.
* @param array<mixed> $manifest Component/block manifest data.
* @param array<string> ...$custom Additional custom classes.
*
* @deprecated 9.2.0 Use `tailwindClasses` instead.
*
* @return string
*/
public static function getTwClasses($attributes, $manifest, ...$custom)
Expand Down Expand Up @@ -299,16 +299,38 @@ public static function getTwClasses($attributes, $manifest, ...$custom)
return Helpers::classnames([$baseClasses, ...$mainClasses, ...$combinationClasses, ...$custom]);
}

private static function unifyClasses($input)
/**
* Unifies the given input classes into a single string.
*
* Takes an array or string of CSS classes and unifies them into a single string,
* ensuring that there are no duplicate classes and that the classes are properly formatted.
*
* @param mixed $input The input classes to be unified. This can be a string or an array of strings.
*
* @return string The unified string of CSS classes.
*/
private static function unifyClasses($input): string
{
if (\is_array($input)) {
return Helpers::classnames($input);
}

return trim($input);
return \trim($input);
}

private static function processOption($partName, $optionValue, $defs)
/**
* Processes the given option for a specific part name.
*
* This method processes the option value for a given part name based on the provided definitions.
* It ensures that the option value is correctly handled according to the definitions.
*
* @param string $partName The name of the part for which the option is being processed.
* @param mixed $optionValue The value of the option to be processed.
* @param array<mixed> $defs The definitions that dictate how the option should be processed.
*
* @return string The processed option value.
*/
private static function processOption($partName, $optionValue, $defs): string
{
$optionClasses = [];

Expand All @@ -321,7 +343,7 @@ private static function processOption($partName, $optionValue, $defs)
return '';
}

if ($isSingleValue && !str_contains($itemPartName, $partName)) {
if ($isSingleValue && !\str_contains($itemPartName, $partName)) {
return '';
}

Expand All @@ -333,10 +355,10 @@ private static function processOption($partName, $optionValue, $defs)
}

// Responsive options.
$breakpoints = array_keys($optionValue);
$breakpoints = \array_keys($optionValue);

if (in_array('_desktopFirst', $breakpoints, true)) {
$breakpoints = array_filter($breakpoints, fn($breakpoint) => $breakpoint !== '_desktopFirst');
if (\in_array('_desktopFirst', $breakpoints, true)) {
$breakpoints = \array_filter($breakpoints, fn($breakpoint) => $breakpoint !== '_desktopFirst');
}

foreach ($breakpoints as $breakpoint) {
Expand All @@ -355,16 +377,31 @@ private static function processOption($partName, $optionValue, $defs)
continue;
}

$splitClasses = explode(' ', $rawClasses);
$splitClasses = array_map(fn($cn) => empty($cn) ? null : "{$breakpoint}:{$cn}", $splitClasses);
$splitClasses = \explode(' ', $rawClasses);
$splitClasses = \array_map(fn($cn) => empty($cn) ? null : "{$breakpoint}:{$cn}", $splitClasses);

$optionClasses = [...$optionClasses, ...$splitClasses];
}

return self::unifyClasses($optionClasses);
}

private static function processCombination($partName, $combo, $attributes, $manifest)
/**
* Processes the given combination for a specific part name.
*
* This method processes the combination value for a given part name based on the provided attributes and manifest.
* It ensures that the combination is correctly handled according to the attributes and manifest.
*
* @param string $partName The name of the part for which the combination is being processed.
* @param mixed $combo The combination value to be processed.
* @param array<mixed> $attributes The attributes that dictate how the combination should be processed.
* @param array<mixed> $manifest The manifest that provides additional context for processing the combination.
*
* @throws JsonException If the combination was not defined correctly.
*
* @return string The processed combination value.
*/
private static function processCombination($partName, $combo, $attributes, $manifest): string
{
$matches = true;

Expand All @@ -375,7 +412,7 @@ private static function processCombination($partName, $combo, $attributes, $mani
$optionValue = $optionValue ? 'true' : 'false';
}

if (is_array($allowedValue) && !in_array($optionValue, $allowedValue, true)) {
if (\is_array($allowedValue) && !\in_array($optionValue, $allowedValue, true)) {
$matches = false;
break;
}
Expand All @@ -393,13 +430,13 @@ private static function processCombination($partName, $combo, $attributes, $mani
$itemPartName = isset($combo['part']) ? $combo['part'] : 'base';
$isSingleValue = isset($combo['twClasses']) || isset($combo['twClassesEditor']);

if ($isSingleValue && !str_contains($itemPartName, $partName)) {
if ($isSingleValue && !\str_contains($itemPartName, $partName)) {
return '';
}

$rawValue = $combo['output'][$partName]['twClasses'] ?? $combo['twClasses'] ?? '';

if (is_array($rawValue) && !\array_is_list($rawValue)) {
if (\is_array($rawValue) && !\array_is_list($rawValue)) {
throw new JsonException('Combination was not defined correctly. Please check the combination definition in the manifest.');
}

Expand All @@ -409,25 +446,27 @@ private static function processCombination($partName, $combo, $attributes, $mani
/**
* Get Tailwind classes for the given component/block.
*
* @param string $string Part to get classes for.
* @param string $part Part to get classes for.
* @param array<mixed> $attributes Component/block attributes.
* @param array<mixed> $manifest Component/block manifest data.
* @param array<string> ...$custom Additional custom classes.
*
* @throws Exception If the part is not defined in the manifest.
*
* @return string
*/
public static function tailwindClasses($part, $attributes, $manifest, ...$custom)
public static function tailwindClasses($part, $attributes, $manifest, ...$custom): string
{
// If nothing is set, return custom classes as a fallback.
if (!$attributes || !$manifest || !isset($manifest['tailwind']) || \array_keys($manifest['tailwind']) === []) {
return $custom ? Helpers::classnames($custom) : ''; // @phpstan-ignore-line
}

$allParts = isset($manifest['tailwind']['parts']) ? ['base', ...array_keys($manifest['tailwind']['parts'])] : ['base'];
$allParts = isset($manifest['tailwind']['parts']) ? ['base', ...\array_keys($manifest['tailwind']['parts'])] : ['base'];

$partName = 'base';

if (!empty($part) && isset($manifest['tailwind']['parts'][$part]) && in_array($part, $allParts, true)) {
if (!empty($part) && isset($manifest['tailwind']['parts'][$part]) && \in_array($part, $allParts, true)) {
$partName = $part;
} elseif ($part !== 'base') {
throw new Exception("Part '{$part}' is not defined in the manifest.");
Expand Down

0 comments on commit 8d901d2

Please sign in to comment.