ViewComponent::getFile()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 20
ccs 0
cts 16
cp 0
crap 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\Debug\Inspector;
4
5
/**
6
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
7
 *
8
 * @access private
9
 *
10
 * @package Elgg.Core
11
 * @since   1.11
12
 */
13
class ViewComponent {
14
15
	/**
16
	 * @var string View location. E.g. "/path/to/views/default/"
17
	 */
18
	public $location;
19
20
	/**
21
	 * @var string View name. E.g. "css/elgg"
22
	 */
23
	public $view;
24
25
	/**
26
	 * @var string View file extension, if known. E.g. "php"
27
	 */
28
	public $extension = null;
29
30
	/**
31
	 * @var bool Is this component represent an overridden view?
32
	 */
33
	public $overridden = false;
34
35
	/**
36
	 * @return string Return the component as a file path
37
	 */
38
	public function getFile() {
39
		$ext = pathinfo($this->view, PATHINFO_EXTENSION);
40
		if ($ext) {
41
			// view is filename
42
			return "{$this->location}{$this->view}";
43
		}
44
45
		$str = "{$this->location}{$this->view}.{$this->extension}";
46
		if ($this->extension === null) {
47
			// try to guess from filesystem
48
			$files = glob("{$this->location}{$this->view}.*");
49
			if (count($files) === 1) {
50
				$str = $files[0];
51
			} else {
52
				$str = "{$this->location}{$this->view}.?";
53
			}
54
		}
55
56
		return $str;
57
	}
58
59
	/**
60
	 * Get a component from the path and location
61
	 *
62
	 * @param string $path     Full file path
63
	 * @param string $location Base location of view
64
	 *
65
	 * @return ViewComponent
66
	 */
67
	public static function fromPaths($path, $location) {
68
		$component = new self();
69
		$component->location = $location;
70
71
		// cut location off
72
		$file = substr($path, strlen($location));
73
		$component->file = $file;
0 ignored issues
show
Bug introduced by
The property file does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
75
		$basename = basename($file);
76
		$period = strpos($basename, '.');
77
		if ($period === false) {
78
			// file with no extension? shouldn't happen
79
			$component->view = $file;
80
			$component->extension = '';
81
		} else {
82
			$cut_off_end = strlen($basename) - $period;
83
			$component->view = substr($file, 0, -$cut_off_end);
84
			$component->extension = substr($basename, $period + 1);
85
		}
86
87
		return $component;
88
	}
89
}
90