Completed
Push — master ( 9af7b9...041144 )
by Robbert
15s
created

ar_template_filesystem::ls()   D

Complexity

Conditions 16
Paths 10

Size

Total Lines 82
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 16
eloc 58
c 4
b 0
f 2
nc 10
nop 1
dl 0
loc 82
ccs 0
cts 73
cp 0
crap 272
rs 4.9422

How to fix   Long Method    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
	use arc\path as path;
4
5
	class ar_template_filesystem extends arBase {
6
		private $path;
7
		private $config;
8
9
		public function __construct($path, $config ) {
10
			$this->path   = path::collapse($path);
11
			$this->config = $config;
12
			$this->config['path'] = path::collapse($config['path']);
13
		}
14
15
		public function get($path, $name){
16
			$arpath = path::collapse($path);
17
18
			if ( strpos($arpath, $this->path, 0) !== 0) {
19
				return ar('error')->raiseError('invalide path for loading template',500);
20
			}
21
22
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
23
			$realpath = realpath($realpath) .'/';
24
25
			$cachepath = sha1($path . $name);
26
			$tempOb    = ar::context()->getObject();
27
			$cacheroot = $tempOb->store->get_config('files').'temp/';
28
29
			if ( 
30
				! file_exists( $cacheroot . $cachepath )  ||
31
				( filemtime($cacheroot . $cachepath ) < filemtime ( $realpath  . $name ) )
32
			) {
33
				$compiled = $this->compile($path, $name);
34
				file_put_contents($cacheroot . $cachepath, $compiled);
35
			}
36
			include (  $cacheroot . $cachepath );
37
			return $arTemplateFunction;
0 ignored issues
show
Bug introduced by
The variable $arTemplateFunction does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
38
		}
39
40
		public function save($path, $name, $template, $local=null, $private=null) {
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $template is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $local is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $private is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
			return false;
42
		}
43
44 View Code Duplication
		public function load($path, $name) {
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...
45
			$arpath = path::collapse($path);
46
47
			if ( strpos($arpath, $this->path, 0) !== 0) {
48
				return ar('error')->raiseError('invalide path for loading template',500);
49
			}
50
51
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
52
			$realpath = realpath($realpath) .'/';
53
54
			return file_get_contents($realpath . $name);
55
		}
56
57
		public function ls($path) {
58
			$arpath = path::collapse($path);
59
60
			if ( strpos($arpath, $this->path, 0) !== 0) {
61
				return [];
62
			}
63
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
64
			$realpath = realpath($realpath) .'/';
65
			$traverse = 'src/';
66
			if (!file_exists($realpath . 'library.json')) {
67
				$realparent = path::parent($realpath);
68
				$node = basename($realpath);
69
				if ($node === 'tests' && file_exists($realparent . 'library.json') ) {
70
					// special case
71
					// system/lib/libname/tests/ is a sibling of src instead of a child library
72
					$traverse = 'tests/';
73
					$arpath   = path::parent($path);
74
					$realpath = $realparent;
75
				} else {
76
					return [];
77
				}
78
			}
79
80
			$config = json_decode(file_get_contents($realpath . 'library.json'),true);
81
82
			if(!isset($config['exports']) ) {
83
				$config['exports'] = [];
84
			}
85
			if(!isset($config['local']) ) {
86
				$config['local'] = [];
87
			}
88
89
			$result = [];
90
91
			$traverseDir = function ($path, $type = 'pobject', $nls = 'any') use ($arpath, &$result, $config, &$traverseDir, $realpath) {
92
				$path = path::collapse($path);
93
				if (is_dir($path) ) {
94
					$index = scandir($path, SCANDIR_SORT_NONE);
95
					if($index !== false) {
96
						list($maintype, $subtype) = explode('.', $type,2);
97
						foreach($index as $filename) {
98
							if($filename[0] === "." ) {
99
								continue;
100
							}
101
							$filepath = $path . $filename;
102
							if ( is_dir($filepath) ) {
103
								if (strlen($filename) == 2) {
104
									$traverseDir(path::collapse($filename, $path), $type, $filename);
105
								} else {
106
									$traverseDir(path::collapse($filename, $path), $filename);
107
								}
108
							} else if ( is_file($filepath) ) {
109
								$tempname = sprintf("%s.%s.%s",$type,$filename,$nls);
110
								$confname = sprintf("%s::%s",$type,$filename);
111
								$private = true;
112
								if (isset( $config['exports'] ) ) {
113
									$private = ! in_array($confname, $config['exports']);
114
								}
115
								$local = false;
116
								if (isset( $config['local'] ) ) {
117
									$local = in_array($confname, $config['local']);
118
								}
119
								$result[$filename][] = [
120
									'id'       => PHP_INT_MAX,
121
									'path'     => $arpath,
122
									'type'     => $maintype,
123
									'subtype'  => $subtype,
124
									'name'     => $tempname,
125
									'filename' => substr($filepath,strlen($realpath)),
126
									'language' => $nls,
127
									'private'  => $private,
128
									'local'    => $local,
129
								];
130
							}
131
						}
132
					}
133
				}
134
135
			};
136
			$traverseDir($realpath . $traverse );
137
			return $result;
138
		}
139
140
		public function rm($path, $name){
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
		}
142
143 View Code Duplication
		public function exists($path, $name) {
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...
144
			$arpath = path::collapse($path);
145
146
			if ( strpos($arpath, $this->path, 0) !== 0) {
147
				return ar('error')->raiseError('invalide path for loading template',500);
148
			}
149
150
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
151
			$realpath = realpath($realpath) .'/';
152
			return file_exists($realpath . $name);
153
		}
154
155
		public function compile($path, $name) {
156
			global $AR;
157
			$arpath = path::collapse($path);
158
159
			if ( strpos($arpath, $this->path, 0) !== 0) {
160
				return ar('error')->raiseError('invalide path for loading template',500);
161
			}
162
163
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
164
			$realpath = realpath($realpath) .'/';
165
166
			$template = file_get_contents($realpath . $name);
167
168
			require_once(AriadneBasePath."/modules/mod_pinp.phtml");
169
170
			$pinp = new pinp($AR->PINP_Functions, "local->", "\$AR_this->_");
171
172
			// FIXME error checking
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "error checking"
Loading history...
173
			$compiled = $pinp->compile(strtr($template,"\r",""));
174
			$compiled = sprintf($AR->PINPtemplate, $compiled);
175
			return $compiled;
176
177
		}
178
	}
179