function SchemaCompatibilityChecker::checkSharedProperties

Same name in this branch
  1. 11.x core/modules/sdc/src/Component/SchemaCompatibilityChecker.php \Drupal\sdc\Component\SchemaCompatibilityChecker::checkSharedProperties()
Same name in other branches
  1. 10 core/modules/sdc/src/Component/SchemaCompatibilityChecker.php \Drupal\sdc\Component\SchemaCompatibilityChecker::checkSharedProperties()
  2. 10 core/lib/Drupal/Core/Theme/Component/SchemaCompatibilityChecker.php \Drupal\Core\Theme\Component\SchemaCompatibilityChecker::checkSharedProperties()

Checks that the shared properties are compatible.

Parameters

array $original_schema: The original schema.

array $new_schema: The schema of the replacement component.

Return value

array The detected error messages, if any.

1 call to SchemaCompatibilityChecker::checkSharedProperties()
SchemaCompatibilityChecker::isCompatible in core/lib/Drupal/Core/Theme/Component/SchemaCompatibilityChecker.php
Checks if the replacement schema is compatible with the old one.

File

core/lib/Drupal/Core/Theme/Component/SchemaCompatibilityChecker.php, line 97

Class

SchemaCompatibilityChecker
Checks whether two schemas are compatible.

Namespace

Drupal\Core\Theme\Component

Code

private function checkSharedProperties(array $original_schema, array $new_schema) : array {
    $error_messages = [];
    $original_properties = $original_schema['properties'] ?? [];
    $new_properties = $new_schema['properties'] ?? [];
    $shared_properties = array_intersect(array_keys($original_properties), array_keys($new_properties));
    return array_reduce($shared_properties, function (array $errors, string $property_name) use ($original_properties, $new_properties) {
        $original_types = $original_properties[$property_name]['type'] ?? [];
        $new_types = $new_properties[$property_name]['type'] ?? [];
        // The type for the new property should, at least, accept all types for
        // the original property. Type in JSON Schema can be either a string or
        // an array of strings.
        $original_types = is_string($original_types) ? [
            $original_types,
        ] : $original_types;
        $new_types = is_string($new_types) ? [
            $new_types,
        ] : $new_types;
        $unsupported_types = array_diff($original_types, $new_types);
        if (!empty($unsupported_types)) {
            $errors[] = sprintf('Property "%s" does not support the types [%s]. These types are supported in the original schema and should be supported in the new schema for compatibility.', $property_name, implode(', ', $unsupported_types));
        }
        // If there are enums, those also need to be compatible.
        $original_enums = $original_properties[$property_name]['enum'] ?? [];
        $new_enums = $new_properties[$property_name]['enum'] ?? [];
        $unsupported_enums = array_diff($original_enums, $new_enums);
        if (!empty($unsupported_enums)) {
            $errors[] = sprintf('Property "%s" does not allow some necessary enum values [%s]. These are supported in the original schema and should be supported in the new schema for compatibility.', $property_name, implode(', ', $unsupported_enums));
        }
        // If the property is an object, then ensure sub-schema compatibility.
        $original_subproperties = $original_properties[$property_name]['properties'] ?? [];
        $new_subproperties = $new_properties[$property_name]['properties'] ?? [];
        if (!empty($original_subproperties) && !empty($new_subproperties)) {
            try {
                $this->isCompatible($original_properties[$property_name], $new_properties[$property_name]);
            } catch (IncompatibleComponentSchema $exception) {
                $errors[] = $exception->getMessage();
            }
        }
        return $errors;
    }, $error_messages);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.