How can PHP developers effectively debug and troubleshoot issues related to directory size calculation in recursive functions?

Issue: When calculating directory size in recursive functions, PHP developers may encounter issues such as incorrect size calculations or infinite loops. To effectively debug and troubleshoot these issues, developers can implement proper error handling, use recursive functions with base cases, and utilize PHP functions like `is_dir()` and `filesize()` to accurately calculate directory sizes.

function calculateDirectorySize($dir) {
    $totalSize = 0;

    if (!is_dir($dir)) {
        return filesize($dir);
    }

    $files = scandir($dir);

    foreach ($files as $file) {
        if ($file != '.' && $file != '..') {
            $totalSize += calculateDirectorySize($dir . '/' . $file);
        }
    }

    return $totalSize;
}

$directory = '/path/to/directory';
$size = calculateDirectorySize($directory);
echo "Total size of directory: " . $size . " bytes";