Completed
Push — master ( 02c8a1...58faf5 )
by Tom
02:14
created

SheetCache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 52
Duplicated Lines 48.08 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 2
dl 25
loc 52
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getRulesFromCache() 14 14 4
A getCacheKey() 11 11 3
A write() 0 8 3
A setKey() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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;
8
//Separates out TSS file loading/caching from parsing
9
class SheetCache {
10
	private $cacheKey;
11
	private $cacheName;
12
	private $cache;
13
14
	public function __construct(Cache $cache) {
15
		$this->cache = $cache;
16
	}
17
18 View Code Duplication
	private function getRulesFromCache($file) {
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...
19
		//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet
20
		$rules = $this->cache->load($this->cacheName, filemtime($file));
21
22
		$this->cacheKey = $this->cacheKey ?? $rules['cacheKey'] ?? null;
23
24
		if ($rules) {
25
			foreach ($rules['import'] as $file) {
26
				//Check that the import file hasn't been changed since the cache was written
27
				if (filemtime($file) > $rules['ctime']) return false;
28
			}
29
		}
30
		return $rules;
31
	}
32
33 View Code Duplication
	public function getCacheKey($tss, $data) {
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...
34
		//Read the rules so that $this->cacheKey is set
35
		if (is_file($tss)) $this->getRulesFromCache($tss);
36
		if ($this->cacheKey) {
37
			$parser = new Parser\Value($data);
38
			$parsedKey = $parser->parseTokens($this->cacheKey)[0];
39
			$this->cacheName = $parsedKey . $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...
40
			return $parsedKey;
41
		}
42
		else return '';
43
	}
44
45
46
	//write the sheet to cache
47
    public function write($file, $rules, $imports = []) {
48
		if (is_file($file)) {
49
			$existing = $this->cache->load($file, filemtime($file));
50
			if (isset($existing['import'])) $imports = $existing['import'];
51
			$this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]);
0 ignored issues
show
Bug introduced by
The property time 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...
Bug introduced by
The method getMinUpdateFreq() does not seem to exist on object<Transphporm\SheetCache>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
		}
53
		return $rules;
54
    }
55
56
    public function setKey($key) {
57
    	$this->cacheKey = $key;
58
    }
59
60
}