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

SheetLoader::setCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
//Separates out TSS file loading/caching from parsing
9
class SheetLoader {
10
    private $tss;
11
    private $filePath;
12
    private $time;
13
    private $import = [];
14
15
    public function __construct(\Transphporm\Cache $cache, \Transphporm\FilePath $filePath, TSSRules $tss, $time) {
16
    	$this->cache = $cache;
0 ignored issues
show
Bug introduced by
The property cache 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...
17
        $this->filePath = $filePath;
18
        $this->tss = $tss;
19
        $this->time = $time ?? time();
20
    }
21
22
	//Allows controlling whether any updates are required to the template
23
	//e.g. return false
24
	//	 1. If all update-frequencies  haven't expired
25
	//   2. If the data hasn't changed since the last run
26
	//If this function returns false, the rendered template is sent straight from the cache skipping 99% of transphporm's code
27
	public function updateRequired($data) {
28
		return $this->tss->updateRequired($data);
29
	}
30
31
	public function addImport($import) {
32
		$this->filePath->addPath(dirname(realpath($this->filePath->getFilePath($import))));
33
		$this->import[] = $import;
34
	}
35
36
	public function setCacheKey($tokens) {
37
		$this->tss->setCacheKey($tokens);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Transphporm\SheetLoader\TSSRules as the method setCacheKey() does only exist in the following implementations of said interface: Transphporm\SheetLoader\TSSFile.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
38
	}
39
40
	public function getCacheKey($data) {
41
		return $this->tss->getCacheKey($data);
42
	}
43
44
45
	public function processRules($template, \Transphporm\Config $config) {
46
		$rules = $this->getRules($config->getCssToXpath(), $config->getValueParser());
47
48
49
		usort($rules, [$this, 'sortRules']);
50
51
		foreach ($rules as $rule) {
52
			if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config);
53
		}
54
55
		//if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import);
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
		$this->tss->write($rules, $this->import);
57
	}
58
59
	//Load the TSS
60
	public function getRules($cssToXpath, $valueParser, $indexStart = 0) {
61
		return $this->tss->getRules($cssToXpath, $valueParser, $this, $indexStart);
62
	}
63
64
	//Process a TSS rule e.g. `ul li {content: "foo"; format: bar}
65
	private function executeTssRule($rule, $template, $config) {
66
		$rule->touch();
67
68
		$pseudoMatcher = $config->createPseudoMatcher($rule->pseudo);
69
		$hook = new \Transphporm\Hook\PropertyHook($rule->properties, $config->getLine(), $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet(), $config->getFilePath());
0 ignored issues
show
Bug introduced by
$config->getLine() cannot be passed to __construct() as the parameter $configLine expects a reference.
Loading history...
70
		$config->loadProperties($hook);
71
		$template->addHook($rule->query, $hook);
72
	}
73
74
75
	private function sortRules($a, $b) {
76
		//If they have the same depth, compare on index
77
		if ($a->query === $b->query) return $this->sortPseudo($a, $b);
78
79
		if ($a->depth === $b->depth) $property = 'index';
80
		else $property = 'depth';
81
82
		return ($a->$property < $b->$property) ? -1 : 1;
83
	}
84
85
86
	private function sortPseudo($a, $b) {
87
		return count($a->pseudo) > count($b->pseudo)  ? 1 : -1;
88
	}
89
}
90