Completed
Push — simpler-test-run ( d8dc4b )
by Sam
10:41
created

ManifestFileFinder::acceptDir()   D

Complexity

Conditions 17
Paths 19

Size

Total Lines 44
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 21
nc 19
nop 3
dl 0
loc 44
rs 4.9807
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\Core\Manifest;
4
5
use SilverStripe\Assets\FileFinder;
6
7
/**
8
 * An extension to the default file finder with some extra filters to faciliate
9
 * autoload and template manifest generation:
10
 *   - Only modules with _config.php files are scanned.
11
 *   - If a _manifest_exclude file is present inside a directory it is ignored.
12
 *   - Assets and module language directories are ignored.
13
 *   - Module tests directories are skipped if the ignore_tests option is not
14
 *     set to false.
15
 */
16
class ManifestFileFinder extends FileFinder {
17
18
	const CONFIG_FILE  = '_config.php';
19
	const CONFIG_DIR  = '_config';
20
	const EXCLUDE_FILE = '_manifest_exclude';
21
	const LANG_DIR     = 'lang';
22
	const TESTS_DIR    = 'tests';
23
24
	protected static $default_options = array(
25
		'include_themes' => false,
26
		'ignore_tests'   => true,
27
		'min_depth'      => 1,
28
		'ignore_dirs'    => array('node_modules')
29
	);
30
31
	public function acceptDir($basename, $pathname, $depth) {
32
		// Skip over the assets directory in the site root.
33
		if ($depth == 1 && $basename == ASSETS_DIR) {
34
			return false;
35
		}
36
37
		// Skip over any lang directories in the top level of the module.
38
		if ($depth == 2 && $basename == self::LANG_DIR) {
39
			return false;
40
		}
41
42
		// Skip over the vendor directory
43
		if ($depth == 1 && $basename == 'vendor') {
44
			return false;
45
		}
46
47
		// If we're not in testing mode, then skip over any tests directories.
48
		if ($this->getOption('ignore_tests') && $basename == self::TESTS_DIR) {
49
			return false;
50
		}
51
52
		// Ignore any directories which contain a _manifest_exclude file.
53
		if (file_exists($pathname . '/' . self::EXCLUDE_FILE)) {
54
			return false;
55
		}
56
57
		// Only include top level module directories which have a configuration
58
		// _config.php file. However, if we're in themes mode then include
59
		// the themes dir without a config file.
60
		$lackingConfig = (
61
			$depth == 1
62
			&& !($this->getOption('include_themes') && $basename == THEMES_DIR)
63
			&& !file_exists($pathname . '/' . self::CONFIG_FILE)
64
			&& !file_exists($pathname . '/' . self::CONFIG_DIR)
65
			&& $basename !== self::CONFIG_DIR // include a root config dir
66
			&& !file_exists("$pathname/../" . self::CONFIG_DIR) // include all paths if a root config dir exists
67
		);
68
69
		if ($lackingConfig) {
70
			return false;
71
		}
72
73
		return parent::acceptDir($basename, $pathname, $depth);
74
	}
75
76
}
77