Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flattenDictionary causes a type error in PHP 8 by calling method_exists without checking type of first parameter #124

Open
trent-crawford opened this issue Mar 3, 2023 · 0 comments

Comments

@trent-crawford
Copy link

trent-crawford commented Mar 3, 2023

SaintSystems\OData\ODataRequest::flattenDictionary() checks if the method exits on $arrayValue. $arrayValue could be an array itself, which would cause a type error on method_exits in PHP 8.

Existing method

/**
     * Flattens the property dictionaries into
     * JSON-friendly arrays
     *
     * @param mixed $obj the object to flatten
     *
     * @return array flattened object
     */
    protected function flattenDictionary($obj) {
        foreach ($obj as $arrayKey => $arrayValue) {
            if (method_exists($arrayValue, 'getProperties')) {
                $data = $arrayValue->getProperties();
                $obj[$arrayKey] = $data;
            } else {
                $data = $arrayValue;
            }
            if (is_array($data)) {
                $newItem = $this->flattenDictionary($data);
                $obj[$arrayKey] = $newItem;
            }
        }
        return $obj;
    }

I will open a pull request for the following change, where we check if $arrayValue is either a string or an object:

/**
     * Flattens the property dictionaries into
     * JSON-friendly arrays
     *
     * @param mixed $obj the object to flatten
     *
     * @return array flattened object
     */
    protected function flattenDictionary($obj) {
        foreach ($obj as $arrayKey => $arrayValue) {
            if ((is_string($arrayValue) || is_object($arrayValue)) && method_exists($arrayValue, 'getProperties')) {
                $data = $arrayValue->getProperties();
                $obj[$arrayKey] = $data;
            } else {
                $data = $arrayValue;
            }
            if (is_array($data)) {
                $newItem = $this->flattenDictionary($data);
                $obj[$arrayKey] = $newItem;
            }
        }
        return $obj;
    }
@trent-crawford trent-crawford changed the title flattenDictionary calls method exist without first ensuring the first parameter is an object or class flattenDictionary causes a type error in PHP 8 by calling method_exists without checking type of first parameter Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant