Completed
Branch BUG-8511-spco-revisit-oversell... (0aad32)
by
unknown
34:42 queued 17:09
created

FileLocator::getFilePaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\services\locators;
3
4
use  EventEspresso\core\exceptions\InvalidDataTypeException;
5
use FilesystemIterator;
6
use GlobIterator;
7
8
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
9
	exit( 'No direct script access allowed' );
10
}
11
12
13
14
/**
15
 * Class FileLocator
16
 *
17
 * finds filepaths from folder paths or FQCNs (Fully Qualified Class Name) from partial FQCNs
18
 *
19
 * @package       Event Espresso
20
 * @author        Brent Christensen
21
 * @since         4.9.0
22
 */
23
class FileLocator extends Locator {
24
25
	/**
26
	 * @var string $file_mask
27
	 */
28
	protected $file_mask = '*.php';
29
30
	/**
31
	 * @var array $filepaths
32
	 */
33
	protected $filepaths = array();
34
35
36
37
	/**
38
	 * @param string $file_mask
39
	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
40
	 */
41
	public function setFileMask( $file_mask ) {
42
		if ( ! is_string( $file_mask ) ) {
43
			throw new InvalidDataTypeException( '$file_mask', $file_mask, 'string' );
44
		}
45
		$this->file_mask = $file_mask;
46
	}
47
48
49
50
	/**
51
	 * @access public
52
	 * @return array
53
	 */
54
	public function getFilePaths() {
55
		return $this->filepaths;
56
	}
57
58
59
60
61
	/**
62
	 * @access public
63
	 * @return int
64
	 */
65
	public function count() {
66
		return count( $this->filepaths );
67
	}
68
69
70
71
	/**
72
	 * given a path to a valid directory, or an array of valid paths,
73
	 * will find all files that match the provided mask
74
	 *
75
	 * @access public
76
	 * @param array|string $directory_paths
77
	 * @return \FilesystemIterator
78
	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
79
	 */
80 View Code Duplication
	public function locate( $directory_paths ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
		if ( ! ( is_string( $directory_paths ) || is_array( $directory_paths ) ) ) {
82
			throw new InvalidDataTypeException( '$directory_paths', $directory_paths, 'string or array' );
83
		}
84
		foreach ( (array) $directory_paths as $directory_path ) {
85
			foreach ( $this->findFilesByPath( $directory_path ) as $key => $file ) {
86
				$this->filepaths[ $key ] = \EEH_File::standardise_directory_separators( $file );
87
			}
88
		}
89
		return $this->filepaths;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->filepaths; (array) is incompatible with the return type declared by the interface EventEspresso\core\servi...ocatorInterface::locate of type FilesystemIterator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
90
	}
91
92
93
94
	/**
95
	 * given a path to a valid directory, will find all files that match the provided mask
96
	 *
97
	 * @access protected
98
	 * @param string $directory_path
99
	 * @return \FilesystemIterator
100
	 */
101
	protected function findFilesByPath( $directory_path = '' ) {
102
		$iterator = new GlobIterator (
103
			\EEH_File::end_with_directory_separator( $directory_path ) . $this->file_mask
104
		);
105
		foreach ( $this->flags as $flag ) {
106
			$iterator->setFlags( $flag );
107
		}
108
		return $iterator;
109
	}
110
111
112
113
}
114
// End of file FileLocator.php
115
// Location: /FileLocator.php