Skip to content

Documentation and possibly implementation for Differ needs updated #70

@denzel-morris

Description

@denzel-morris

The documentation for Differ::diff states that it accepts both arrays and strings as parameters:

/**
     * Returns the diff between two arrays or strings as string.
     *
     * @param array|string                            $from
     * @param array|string                            $to
     * @param LongestCommonSubsequenceCalculator|null $lcs
     *
     * @return string
     */
    public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string

This is clearly not the case since Differ::validateDiffInput explicitly tries to cast everything to a string except arrays:

/**
     * Casts variable to string if it is not a string or array.
     *
     * @param mixed $input
     *
     * @return string
     */
    private function validateDiffInput($input): string
    {
        if (!\is_array($input) && !\is_string($input)) {
            return (string) $input;
        }
        return $input;
    }

The first fix is obvious, the documentation for Differ::diff needs to be updated. The second issue, is maybe we want to throw an InvalidArgumentException on an array instead of the much more opaque:

TypeError: Return value of SebastianBergmann\Diff\Differ::validateDiffInput() must be of the type string, array returned

Throwing InvalidArgumentException with an appropriate description would represent a better user experience.

If we agree, let me know, and I'll submit a PR with both fixes referencing this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions