From 8d901d28208502e20eef138e345047152506e97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Goran=20Alkovi=C4=87?= <77000136+goranalkovic-infinum@users.noreply.github.com> Date: Tue, 1 Oct 2024 09:16:44 +0200 Subject: [PATCH] fix lint errors --- src/Cli/CliHelpers.php | 2 +- src/Helpers/TailwindTrait.php | 85 +++++++++++++++++++++++++---------- 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/Cli/CliHelpers.php b/src/Cli/CliHelpers.php index 13f350b87..a929f478e 100644 --- a/src/Cli/CliHelpers.php +++ b/src/Cli/CliHelpers.php @@ -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; } diff --git a/src/Helpers/TailwindTrait.php b/src/Helpers/TailwindTrait.php index 2fd7458d6..ba05fa759 100644 --- a/src/Helpers/TailwindTrait.php +++ b/src/Helpers/TailwindTrait.php @@ -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. @@ -53,6 +51,8 @@ public static function getTwBreakpoints($desktopFirst = false) * @param array $manifest Component/block manifest data. * @param array ...$custom Additional custom classes. * + * @deprecated 9.2.0 Use `tailwindClasses` instead. + * * @return string */ public static function getTwPart($part, $manifest, ...$custom) @@ -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. @@ -82,6 +80,8 @@ public static function getTwPart($part, $manifest, ...$custom) * @param array $manifest Component/block manifest data. * @param array ...$custom Additional custom classes. * + * @deprecated 9.2.0 Use `tailwindClasses` instead. + * * @return string */ public static function getTwDynamicPart($part, $attributes, $manifest, ...$custom) @@ -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 $attributes Component/block attributes. * @param array $manifest Component/block manifest data. * @param array ...$custom Additional custom classes. * + * @deprecated 9.2.0 Use `tailwindClasses` instead. + * * @return string */ public static function getTwClasses($attributes, $manifest, ...$custom) @@ -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 $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 = []; @@ -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 ''; } @@ -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) { @@ -355,8 +377,8 @@ 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]; } @@ -364,7 +386,22 @@ private static function processOption($partName, $optionValue, $defs) 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 $attributes The attributes that dictate how the combination should be processed. + * @param array $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; @@ -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; } @@ -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.'); } @@ -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 $attributes Component/block attributes. * @param array $manifest Component/block manifest data. * @param array ...$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.");