What are the potential pitfalls of implementing the directory tree traversal method within the directory object itself?

Implementing the directory tree traversal method within the directory object itself can lead to tight coupling and violate the single responsibility principle. To solve this, it's better to separate the traversal logic into a separate class or function to keep the directory object focused on its core responsibilities.

class Directory {
    private $path;

    public function __construct($path) {
        $this->path = $path;
    }

    public function getFiles() {
        $traverser = new DirectoryTraverser();
        return $traverser->traverse($this->path);
    }
}

class DirectoryTraverser {
    public function traverse($path) {
        $files = [];

        // Traversal logic goes here

        return $files;
    }
}