Fix #1
- Strict check for enum duplicates - Added check for empty enum definition - Added check for duplicated enum definition
This commit is contained in:
parent
1c342113b6
commit
34965dee37
3 changed files with 50 additions and 3 deletions
|
|
@ -4,8 +4,10 @@ namespace Kwaadpepper\Enum;
|
|||
|
||||
use BadMethodCallException;
|
||||
use JsonSerializable;
|
||||
use Kwaadpepper\Enum\Exceptions\DuplicateDefinitionException;
|
||||
use Kwaadpepper\Enum\Exceptions\DuplicateLabelsException;
|
||||
use Kwaadpepper\Enum\Exceptions\DuplicateValuesException;
|
||||
use Kwaadpepper\Enum\Exceptions\EmptyDefinitionException;
|
||||
use Kwaadpepper\Enum\Exceptions\EnumNotRoutableException;
|
||||
use Kwaadpepper\Enum\Exceptions\UnknownEnumProperty;
|
||||
use ReflectionClass;
|
||||
|
|
@ -270,6 +272,13 @@ abstract class BaseEnum implements JsonSerializable
|
|||
$valueMap = static::values();
|
||||
$labelMap = static::labels();
|
||||
|
||||
if (!count($matches[1])) {
|
||||
throw new EmptyDefinitionException('You are missing defining enum definition in comments.');
|
||||
}
|
||||
if (self::arrayHasDuplicates($matches[1])) {
|
||||
throw new DuplicateDefinitionException('You have duplicate values in comments enum definition.');
|
||||
}
|
||||
|
||||
foreach ($matches[1] as $methodName) {
|
||||
$valueMap[$methodName] = $valueMap[$methodName] ?? $methodName;
|
||||
$labelMap[$methodName] = $labelMap[$methodName] ?? $methodName;
|
||||
|
|
@ -281,11 +290,11 @@ abstract class BaseEnum implements JsonSerializable
|
|||
}
|
||||
|
||||
if (self::arrayHasDuplicates($valueMap)) {
|
||||
throw new DuplicateValuesException();
|
||||
throw new DuplicateValuesException('You have duplicates values in enum values method.');
|
||||
}
|
||||
|
||||
if (self::arrayHasDuplicates($labelMap)) {
|
||||
throw new DuplicateLabelsException();
|
||||
throw new DuplicateLabelsException('You have duplicates values in enum labels method.');
|
||||
}
|
||||
|
||||
return self::$definitionCache[static::class] = $definition;
|
||||
|
|
@ -299,7 +308,13 @@ abstract class BaseEnum implements JsonSerializable
|
|||
*/
|
||||
private static function arrayHasDuplicates(array $array): bool
|
||||
{
|
||||
return count($array) > count(array_unique($array));
|
||||
$uniqueVals = [];
|
||||
foreach ($array as $value) {
|
||||
if (!in_array($value, $uniqueVals, true)) {
|
||||
$uniqueVals[] = $value;
|
||||
}
|
||||
}
|
||||
return count($array) > count($uniqueVals);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
16
src/Exceptions/DuplicateDefinitionException.php
Normal file
16
src/Exceptions/DuplicateDefinitionException.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Kwaadpepper\Enum\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Is thrown when your enum definition
|
||||
* contain multiple times the same label value
|
||||
*
|
||||
* This should happen if you have a duplication declaration
|
||||
*/
|
||||
class DuplicateDefinitionException extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
16
src/Exceptions/EmptyDefinitionException.php
Normal file
16
src/Exceptions/EmptyDefinitionException.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Kwaadpepper\Enum\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Is thrown when your enum definition
|
||||
* contain multiple times the same label value
|
||||
*
|
||||
* This should happen if you have a duplication declaration
|
||||
*/
|
||||
class EmptyDefinitionException extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue