mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-19 09:26:45 +00:00
Reduce code duplication
This commit is contained in:
parent
847f931660
commit
d01203d7c4
@ -295,25 +295,9 @@ function processStateGroup(string $oldName, array $upgradeTable, BlockStateUpgra
|
|||||||
if($oldValue === null){
|
if($oldValue === null){
|
||||||
throw new AssumptionFailedError("We already checked that all states had consistent old properties");
|
throw new AssumptionFailedError("We already checked that all states had consistent old properties");
|
||||||
}
|
}
|
||||||
//TODO: lots of similar logic to the remappedStates builder below
|
if(!checkFlattenPropertySuitability($oldValue, $flattenedPropertyType, $pair->new->getName(), $valueMap)){
|
||||||
if(!$oldValue instanceof ByteTag && !$oldValue instanceof IntTag && !$oldValue instanceof StringTag){
|
|
||||||
//unknown property type - bad candidate for flattening
|
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
if($flattenedPropertyType === null){
|
|
||||||
$flattenedPropertyType = get_class($oldValue);
|
|
||||||
}elseif(!$oldValue instanceof $flattenedPropertyType){
|
|
||||||
//property type mismatch - bad candidate for flattening
|
|
||||||
continue 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
$rawValue = (string) $oldValue->getValue();
|
|
||||||
$existingNewId = $valueMap[$rawValue] ?? null;
|
|
||||||
if($existingNewId !== null && $existingNewId !== $pair->new->getName()){
|
|
||||||
//this property value is associated with multiple new IDs - bad candidate for flattening
|
|
||||||
continue 2;
|
|
||||||
}
|
|
||||||
$valueMap[$rawValue] = $pair->new->getName();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($flattenedProperty !== null){
|
if($flattenedProperty !== null){
|
||||||
@ -392,6 +376,37 @@ function findCommonSuffix(array $strings) : string{
|
|||||||
return strrev(findCommonPrefix($reversed));
|
return strrev(findCommonPrefix($reversed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string[] $valueToIdMap
|
||||||
|
* @phpstan-param ?class-string<ByteTag|IntTag|StringTag> $expectedType
|
||||||
|
* @phpstan-param-out class-string<ByteTag|IntTag|StringTag> $expectedType
|
||||||
|
* @phpstan-param array<string, string> $valueToIdMap
|
||||||
|
* @phpstan-param-out array<string, string> $valueToIdMap
|
||||||
|
*/
|
||||||
|
function checkFlattenPropertySuitability(Tag $oldValue, ?string &$expectedType, string $actualNewId, array &$valueToIdMap) : bool{
|
||||||
|
//TODO: lots of similar logic to the remappedStates builder below
|
||||||
|
if(!$oldValue instanceof ByteTag && !$oldValue instanceof IntTag && !$oldValue instanceof StringTag){
|
||||||
|
//unknown property type - bad candidate for flattening
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if($expectedType === null){
|
||||||
|
$expectedType = get_class($oldValue);
|
||||||
|
}elseif(!$oldValue instanceof $expectedType){
|
||||||
|
//property type mismatch - bad candidate for flattening
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rawValue = (string) $oldValue->getValue();
|
||||||
|
$existingNewId = $valueToIdMap[$rawValue] ?? null;
|
||||||
|
if($existingNewId !== null && $existingNewId !== $actualNewId){
|
||||||
|
//this property value is associated with multiple new IDs - bad candidate for flattening
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$valueToIdMap[$rawValue] = $actualNewId;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string[] $valueToId
|
* @param string[] $valueToId
|
||||||
* @phpstan-param array<string, string> $valueToId
|
* @phpstan-param array<string, string> $valueToId
|
||||||
@ -522,23 +537,6 @@ function processRemappedStates(array $upgradeTable) : array{
|
|||||||
if(isset($notFlattenedProperties[$propertyName])){
|
if(isset($notFlattenedProperties[$propertyName])){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(!$propertyValue instanceof StringTag && !$propertyValue instanceof IntTag && !$propertyValue instanceof ByteTag){
|
|
||||||
$notFlattenedProperties[$propertyName] = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$previousType = $candidateFlattenedPropertyTypes[$propertyName] ?? null;
|
|
||||||
if($previousType !== null && $previousType !== get_class($propertyValue)){
|
|
||||||
//mismatched types for the same property name - this has never happened so far, but it's not impossible
|
|
||||||
$notFlattenedProperties[$propertyName] = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$candidateFlattenedPropertyTypes[$propertyName] = get_class($propertyValue);
|
|
||||||
|
|
||||||
$rawValue = (string) $propertyValue->getValue();
|
|
||||||
if($rawValue === ""){
|
|
||||||
$notFlattenedProperties[$propertyName] = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$filter = $pair->old->getStates();
|
$filter = $pair->old->getStates();
|
||||||
foreach($unchangedStatesByNewName[$pair->new->getName()] as $unchangedPropertyName){
|
foreach($unchangedStatesByNewName[$pair->new->getName()] as $unchangedPropertyName){
|
||||||
@ -551,16 +549,13 @@ function processRemappedStates(array $upgradeTable) : array{
|
|||||||
unset($filter[$propertyName]);
|
unset($filter[$propertyName]);
|
||||||
|
|
||||||
$rawFilter = encodeOrderedProperties($filter);
|
$rawFilter = encodeOrderedProperties($filter);
|
||||||
if(isset($candidateFlattenedValues[$propertyName][$rawFilter])){
|
$candidateFlattenedValues[$propertyName][$rawFilter] ??= [];
|
||||||
$valuesToIds = $candidateFlattenedValues[$propertyName][$rawFilter];
|
$expectedType = $candidateFlattenedPropertyTypes[$propertyName] ?? null;
|
||||||
$existingNewId = $valuesToIds[$rawValue] ?? null;
|
if(!checkFlattenPropertySuitability($propertyValue, $expectedType, $pair->new->getName(), $candidateFlattenedValues[$propertyName][$rawFilter])){
|
||||||
if($existingNewId !== null && $existingNewId !== $pair->new->getName()){
|
|
||||||
//this old value is associated with multiple new IDs - bad candidate for flattening
|
|
||||||
$notFlattenedProperties[$propertyName] = true;
|
$notFlattenedProperties[$propertyName] = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
$candidateFlattenedPropertyTypes[$propertyName] = $expectedType;
|
||||||
$candidateFlattenedValues[$propertyName][$rawFilter][$rawValue] = $pair->new->getName();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach(Utils::stringifyKeys($candidateFlattenedValues) as $propertyName => $filters){
|
foreach(Utils::stringifyKeys($candidateFlattenedValues) as $propertyName => $filters){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user