/**
 * Checks if all functions (outside class) and classes have package
 * 
 * package tag may be inherited from file-level phpdocs
 *
 * @param local_moodlecheck_file $file
 * @return array of found errors
 */
function local_moodlecheck_packagespecified(local_moodlecheck_file $file)
{
    $errors = array();
    $phpdocs = $file->find_file_phpdocs();
    if ($phpdocs && count($phpdocs->get_tags('package', true))) {
        // package is specified on file level, it is automatically inherited
        return array();
    }
    foreach ($file->get_classes() as $object) {
        if (!$object->phpdocs || !count($object->phpdocs->get_tags('package', true))) {
            $errors[] = array('line' => $file->get_line_number($object->boundaries[0]), 'object' => 'class ' . $object->name);
        }
    }
    foreach ($file->get_functions() as $object) {
        if ($object->class === false) {
            if (!$object->phpdocs || !count($object->phpdocs->get_tags('package', true))) {
                $errors[] = array('line' => $file->get_line_number($object->boundaries[0]), 'object' => 'function ' . $object->fullname);
            }
        }
    }
    return $errors;
}
/**
 * Checks that all functions have proper arguments in phpdocs
 *
 * @param local_moodlecheck_file $file 
 * @return array of found errors
 */
function local_moodlecheck_functionarguments(local_moodlecheck_file $file)
{
    $errors = array();
    foreach ($file->get_functions() as $function) {
        if ($function->phpdocs !== false) {
            $documentedarguments = $function->phpdocs->get_params();
            $match = count($documentedarguments) == count($function->arguments);
            for ($i = 0; $match && $i < count($documentedarguments); $i++) {
                if (count($documentedarguments[$i]) < 2) {
                    // must be at least type and parameter name
                    $match = false;
                } else {
                    if (strlen($function->arguments[$i][0]) && $function->arguments[$i][0] != $documentedarguments[$i][0]) {
                        $match = false;
                    } else {
                        if ($documentedarguments[$i][0] == 'type') {
                            $match = false;
                        } else {
                            if ($function->arguments[$i][1] != $documentedarguments[$i][1]) {
                                $match = false;
                            }
                        }
                    }
                }
            }
            $documentedreturns = $function->phpdocs->get_params('return');
            for ($i = 0; $match && $i < count($documentedreturns); $i++) {
                if (empty($documentedreturns[$i][0]) || $documentedreturns[$i][0] == 'type') {
                    $match = false;
                }
            }
            if (!$match) {
                $errors[] = array('line' => $function->phpdocs->get_line_number($file, '@param'), 'function' => $function->fullname);
            }
        }
    }
    return $errors;
}