Completed
Push — master ( a05d8f...d51291 )
by Peter
18:15
created

NsCache   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.72%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 105
ccs 50
cts 57
cp 0.8772
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 13 4
A __construct() 0 6 1
A valid() 0 12 2
A get() 0 9 2
A set() 0 12 2
A remove() 0 8 2
A _valid() 0 17 4
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Addendum\Cache;
10
11
use Maslosoft\Addendum\Addendum;
12
use Maslosoft\Addendum\Helpers\SoftIncluder;
13
use Maslosoft\Addendum\Options\MetaOptions;
14
use Maslosoft\Cli\Shared\Helpers\PhpExporter;
15
16
/**
17
 * Meta Namespaces cache
18
 *
19
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
20
 */
21
class NsCache
22
{
23
24
	const FileName = '_ns.php';
1 ignored issue
show
Coding Style introduced by
Constant FileName should be defined in uppercase
Loading history...
25
26
	private $_file = '';
27
28
	/**
29
	 * Addendum instance
30
	 * @var Addendum
31
	 */
32
	private $ad = null;
33
	private $_ns = [];
0 ignored issues
show
Unused Code introduced by
The property $_ns is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
35
	/**
36
	 *
37
	 * @var
38
	 */
39
	private static $_nsCache = [];
40
41 3
	public function __construct($path, Addendum $addendum, MetaOptions $options = null)
42
	{
43 3
		$this->_file = sprintf('%s/%s', $path, self::FileName);
44 3
		$this->ad = $addendum;
45 3
		$this->setOptions($options);
46 3
	}
47
48 30
	public function setOptions(MetaOptions $options = null)
49
	{
50 30
		if (!empty($options))
51 30
		{
52 3
			foreach ($options->namespaces as $ns)
53
			{
54 3
				if (!$this->_valid($ns))
55 3
				{
56 3
					$this->ad->addNamespace($ns);
57 3
				}
58 3
			}
59 3
		}
60 30
	}
61
62 28
	public function valid()
63
	{
64 28
		$ns = $this->get();
65 28
		$valid = $this->_valid($ns);
66
67
		// Ovverride existing cache if not valid
68 28
		if (!$valid)
69 28
		{
70 2
			$this->set();
71 2
		}
72 28
		return $valid;
73
	}
74
75 28
	public function get()
76
	{
77 28
		if (!empty(self::$_nsCache[$this->_file]))
78 28
		{
79 26
			return self::$_nsCache[$this->_file];
80
		}
81 2
		self::$_nsCache[$this->_file] = SoftIncluder::includeFile($this->_file);
82 2
		return self::$_nsCache[$this->_file];
83
	}
84
85 19
	public function set()
86
	{
87 19
		foreach ($this->ad->namespaces as $name)
88
		{
89 19
			$ns[$name] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ns was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ns = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
90 19
		}
91 19
		$data = PhpExporter::export($ns);
0 ignored issues
show
Bug introduced by
The variable $ns does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
92 19
		$mask = umask(0);
93 19
		file_put_contents($this->_file, $data);
94 19
		umask($mask);
95 19
		self::$_nsCache[$this->_file] = $ns;
96 19
	}
97
98
	public function remove()
99
	{
100
		unset(self::$_nsCache[$this->_file]);
101
		if (file_exists($this->_file))
102
		{
103
			unlink($this->_file);
104
		}
105
	}
106
107 28
	private function _valid($ns)
108
	{
109
		// Fresh data
110 28
		if (empty($ns))
111 28
		{
112 2
			return true;
113
		}
114
115 26
		foreach ($this->ad->namespaces as $name)
116
		{
117 26
			if (empty($ns[$name]))
118 26
			{
119 3
				return false;
120
			}
121 26
		}
122 25
		return true;
123
	}
124
125
}
126