Skip to content

RequiredConstraint::incrementPath is duplicated across drafts instead of living on JsonPointer #918

@DannyvdSluijs

Description

@DannyvdSluijs

Description

incrementPath is a path manipulation helper that is copy-pasted into the RequiredConstraint of every draft (Draft06, Draft07, Draft2019) because those constraints implement ConstraintInterface directly rather than extending Constraint, and therefore don't inherit the incrementPath method available on the base Constraint class.

Affected Files

  • src/JsonSchema/Constraints/Drafts/Draft06/RequiredConstraint.php
  • src/JsonSchema/Constraints/Drafts/Draft07/RequiredConstraint.php
  • src/JsonSchema/Constraints/Drafts/Draft2019/RequiredConstraint.php

The Draft2019 version has a @todo comment acknowledging this:

/**
 * @todo refactor as this was only copied from UndefinedConstraint
 */
protected function incrementPath(?JsonPointer $path, $i): JsonPointer
{
    $path = $path ?? new JsonPointer('');

    if ($i === null || $i === '') {
        return $path;
    }

    return $path->withPropertyPaths(array_merge($path->getPropertyPaths(), [$i]));
}

Suggested Fix

Move incrementPath onto JsonPointer itself as an instance method, since it exclusively operates on a JsonPointer. All three RequiredConstraint files (and any other constraint that needs it) could then call $path->withAppendedPath($i) without needing to duplicate the helper.

Alternatively, a static utility method on JsonPointer or a dedicated path utility class would also centralise the logic without touching the constraint hierarchy.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions