Well, the function kind-of sort-of violates SRP...but according to Uncle Bob, I think your class violates the SRP moreso than the function.
Your class above, has three functions within it: getArrayOfThemeNames(), loadUnfilteredDirectoryListing(), and loadThemeFiles()
. One of them scans the directory, one of theme creates an array of theme names, and another loads the theme files. One of those methods should not be in there: loadUnfilteredDirectoryListing
should be in its own class called DirectoryLister{}
or something. Which means, by extension, that getArrayOfThemeNames()
is in violation of SRP. To quote Atwood - SRP is about "choosing a single, clearly defined goal for any particular bit of code: Do One Thing ... But in choosing one thing, you are ruling out an infinite universe of other possible things you could have done. It also means consciously choosing what your code won't do.". So in the end, its up to you whether this violates SRP and whether you would choose to follow SRP so deeply that you violate OOP or DRY.
Perhaps someone with more experience can chime in or prove me wrong (heck, always good to learn something from someone else)
EDIT: See (http://www.oodesign.com/single-responsibility-principle.html) article as well for clarification.
REVISIT 01/26/2015: Revisiting this answer, I would rewrite the code as follows:
class Theme {
public $theme_directory;
public $theme_files;
public function __construct($theme_directory){
$this->theme_directory = $theme_directory;
}
public function get_theme_names() /* Array */ {
$this->load_theme_files();
return $this->theme_files;
}
private function load_theme_files(Directory $directory) /* Null */ {
$directory->directory = $this->theme_directory;
foreach($directory->load_unfiltered() as $file) {
if($file != '..' && $file != '.') {
$this->theme_files[] = $file;
}
}
}
}
class Directory {
public $directory;
public function __construct() {
$this->directory = $directory;
}
private function load_unfiltered() /* array */ {
return scandir($this->directory);
}
}