Skip to content

Wrong logic flow #21

@hnccox-yard

Description

@hnccox-yard

public function meta(string $key, $default = null)
{
$separator = '.';
$key = sprintf('%s_%s', 'woo', $key);
$data = $this->data['meta'];
// @Assert $key is a non-empty string
// @Assert $data is a loopable array
// @otherwise return $default value
if (! is_string($key) || empty($key) || ! count($data)) {
return $default;
}

    // @assert $key contains a dot notated string
    if (false !== strpos($key, $separator)) {
        $keys = array_map(function ($key) {
            if (! preg_match('/^woo_/', $key)) {
                return sprintf('%s_%s', 'woo', $key);
            }

            return $key;
        }, explode($separator, $key));

        foreach ($keys as $innerKey) {
            // @assert $data[$innerKey] is available to continue
            // @otherwise return $default value
            if (! array_key_exists($innerKey, $data)) {
                return $default;
            }

            $data = $data[$innerKey];
        }

        return $data;
    }

    // @fallback returning value of $key in $data or $default value
    $data = $data[$key] ?? $default;

    // If there is a default but the types do not match, return default
    if (! is_null($default) && gettype($data) !== gettype($default)) {
        return $default;
    }

    return $data;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions