Skip to content

Commit

Permalink
Add support for introspecting enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Mar 28, 2024
1 parent 59e3fb9 commit 86d4727
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/View/Helper/CrudViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

namespace CrudView\View\Helper;

use BackedEnum;
use Cake\Database\Type\EnumLabelInterface;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\SchemaInterface;
use Cake\Utility\Inflector;
use Cake\Utility\Text;
use Cake\View\Helper;
use Cake\View\Helper\FormHelper;
use UnitEnum;
use function Cake\Core\h;
use function Cake\I18n\__d;

Expand Down Expand Up @@ -156,6 +159,10 @@ public function introspect(string $field, mixed $value, array $options = []): ar
return $this->formatTime($field, $value, $options);
}

if (str_starts_with($type, 'enum-')) {

Check failure on line 162 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PossiblyNullArgument

src/View/Helper/CrudViewHelper.php:162:29: PossiblyNullArgument: Argument 1 of str_starts_with cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 162 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PossiblyNullArgument

src/View/Helper/CrudViewHelper.php:162:29: PossiblyNullArgument: Argument 1 of str_starts_with cannot be null, possibly null value provided (see https://psalm.dev/078)
return $this->formatEnum($field, $value, $options);
}

$value = $this->formatString($field, $value);

if ($field === $this->getViewVar('displayField')) {
Expand Down Expand Up @@ -230,6 +237,23 @@ public function formatTime(string $field, mixed $value, array $options): string
return $value;
}

/**
* Format an enum for display
*
* @param string $field Name of field.
* @param int|string|\UnitEnum|\BackedEnum $value Value of field.

Check warning on line 244 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

@param type hint is not formatted properly, expected "\UnitEnum|\BackedEnum|string|int"

Check warning on line 244 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

@param type hint is not formatted properly, expected "\UnitEnum|\BackedEnum|string|int"
* @return string

Check failure on line 245 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

InvalidReturnType

src/View/Helper/CrudViewHelper.php:245:16: InvalidReturnType: The declared return type 'string' for CrudView\View\Helper\CrudViewHelper::formatEnum is incorrect, got 'int|string' (see https://psalm.dev/011)

Check failure on line 245 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

InvalidReturnType

src/View/Helper/CrudViewHelper.php:245:16: InvalidReturnType: The declared return type 'string' for CrudView\View\Helper\CrudViewHelper::formatEnum is incorrect, got 'int|string' (see https://psalm.dev/011)
*/
public function formatEnum(string $field, int|string|UnitEnum|BackedEnum $value, array $options): string
{
if (is_scalar($value)) {
return $value;

Check failure on line 250 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method CrudView\View\Helper\CrudViewHelper::formatEnum() should return string but returns int|string.

Check failure on line 250 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

InvalidReturnStatement

src/View/Helper/CrudViewHelper.php:250:20: InvalidReturnStatement: The inferred type 'int|string' does not match the declared return type 'string' for CrudView\View\Helper\CrudViewHelper::formatEnum (see https://psalm.dev/128)

Check failure on line 250 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method CrudView\View\Helper\CrudViewHelper::formatEnum() should return string but returns int|string.

Check failure on line 250 in src/View/Helper/CrudViewHelper.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

InvalidReturnStatement

src/View/Helper/CrudViewHelper.php:250:20: InvalidReturnStatement: The inferred type 'int|string' does not match the declared return type 'string' for CrudView\View\Helper\CrudViewHelper::formatEnum (see https://psalm.dev/128)
}

return $value instanceof EnumLabelInterface ?
$value->label() : Inflector::humanize(Inflector::underscore($value->name));
}

/**
* Format a string for display
*
Expand Down

0 comments on commit 86d4727

Please sign in to comment.