Completed
Push — master ( f4d4c4...598d1e )
by Tom
02:08
created

TSSFile   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 90
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getRulesFromCache() 0 15 4
A setCacheKey() 0 3 1
A updateRequired() 0 12 3
A getCacheKey() 0 10 2
A getRules() 0 8 3
A write() 0 8 3
A getMinUpdateFreq() 0 10 3
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
namespace Transphporm\SheetLoader;
8
class TSSFile implements TSSRules {
9
	private $fileName;
10
	private $cacheName;
11
	private $cacheKey;
12
	private $cache;
13
	private $time;
14
15
	public function __construct($fileName, \Transphporm\FilePath $filePath, $cache, $time) {
16
		$this->fileName = $fileName;
17
		$this->filePath = $filePath;
0 ignored issues
show
Bug introduced by
The property filePath 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...
18
		$this->cache = $cache;
19
	    $this->time = $time ?? time();
20
	    $this->cacheName = $this->fileName;
21
	}
22
23
	private function getRulesFromCache($file) {
24
		//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet
25
		$rules = $this->cache->load($this->cacheName, filemtime($file));
26
27
		$this->cacheKey = $this->cacheKey ?? $rules['cacheKey'] ?? null;
28
29
		if ($rules) {
30
			foreach ($rules['import'] as $file) {
31
				//Check that the import file hasn't been changed since the cache was written
32
				if (filemtime($file) > $rules['ctime']) return false;
33
			}
34
		}
35
36
		return $rules;
37
	}
38
39
	public function setCacheKey($tokens) {
40
		$this->cacheKey = $tokens;
41
	}
42
43
	public function updateRequired($data) {
44
		$this->cacheName = $this->getCacheKey($data) . $this->fileName;
45
46
		$rules = $this->getRulesFromCache($this->fileName, $data);
0 ignored issues
show
Unused Code introduced by
The call to TSSFile::getRulesFromCache() has too many arguments starting with $data.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
		//Nothing was cached or the TSS file has changed, update is required
48
		if (empty($rules)) return true;
49
50
		//Find the sheet's minimum update-frequency, if it hasn't passed then no updates are required
51
		if ($rules['ctime']+$rules['minFreq'] <= $this->time) return true;
52
53
		return false;
54
	}
55
56
	public function getCacheKey($data) {
57
		$this->getRulesFromCache($this->fileName);
58
		if ($this->cacheKey) {
59
			$parser = new \Transphporm\Parser\Value($data);
60
			$x= $parser->parseTokens($this->cacheKey)[0];
61
			$this->cacheName = $x . $this->tss;
0 ignored issues
show
Bug introduced by
The property tss 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...
62
			return $x;
63
		}
64
		else return '';
65
	}
66
67
	public function getRules($cssToXpath, $valueParser, $sheetLoader, $indexStart) {
68
		$rules = $this->getRulesFromCache($this->fileName)['rules'];
69
		$this->filePath->addPath(dirname(realpath($this->fileName)));
70
		if (empty($rules)) $tss = file_get_contents($this->fileName);
71
		else return $rules;
72
73
		return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
74
	}
75
76
	//write the sheet to cache
77
    public function write($rules, $imports = []) {
78
79
		$existing = $this->cache->load($this->fileName, filemtime($this->fileName));
80
		if (isset($existing['import']) && empty($imports)) $imports = $existing['import'];
81
		$this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]);
82
83
		return $rules;
84
    }
85
86
    //Gets the minimum update-frequency for a sheet's rules
87
	private function getMinUpdateFreq($rules) {
88
		$min = \PHP_INT_MAX;
89
90
		foreach ($rules as $rule) {
91
			$ruleFreq = $rule->getUpdateFrequency();
92
			if ($ruleFreq < $min) $min = $ruleFreq;
93
		}
94
95
		return $min;
96
	}
97
}