Completed
Push — master ( e893f6...204e13 )
by Peter
20:17
created

NsCache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL, Commercial license.
5
 *
6
 * @package maslosoft/addendum
7
 * @licence AGPL, Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes)
9
 * @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
10
 * @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
11
 * @link http://maslosoft.com/addendum/ - maslosoft addendum
12
 * @link https://code.google.com/p/addendum/ - original addendum project
13
 */
14
15
namespace Maslosoft\Addendum\Cache;
16
17
use Maslosoft\Addendum\Addendum;
18
use Maslosoft\Addendum\Helpers\SoftIncluder;
19
use Maslosoft\Addendum\Options\MetaOptions;
20
use Maslosoft\Cli\Shared\Helpers\PhpExporter;
21
22
/**
23
 * Meta Namespaces cache
24
 *
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class NsCache
28
{
29
30
	const FileName = '_ns.php';
31
32
	private $file = '';
33
34
	/**
35
	 * Addendum instance
36
	 * @var Addendum
37
	 */
38
	private $ad = null;
39
40
	/**
41
	 *
42
	 * @var
43
	 */
44
	private static $nsCache = [];
45
46 3
	public function __construct($path, Addendum $addendum, MetaOptions $options = null)
47
	{
48 3
		$this->file = sprintf('%s/%s', $path, self::FileName);
49 3
		$this->ad = $addendum;
50 3
		$this->setOptions($options);
51 3
	}
52
53 34
	public function setOptions(MetaOptions $options = null)
54
	{
55 34
		if (!empty($options))
56 34
		{
57 3
			foreach ($options->namespaces as $ns)
58
			{
59 3
				if (!$this->isValid($ns))
60 3
				{
61 3
					$this->ad->addNamespace($ns);
62 3
				}
63 3
			}
64 3
		}
65 34
	}
66
67 32
	public function valid()
68
	{
69 32
		$ns = $this->get();
70 32
		$valid = $this->isValid($ns);
71
72
		// Ovverride existing cache if not valid
73 32
		if (!$valid)
74 32
		{
75 2
			$this->set();
76 2
		}
77 32
		return $valid;
78
	}
79
80 32
	public function get()
81
	{
82 32
		if (!empty(self::$nsCache[$this->file]))
83 32
		{
84 30
			return self::$nsCache[$this->file];
85
		}
86 2
		self::$nsCache[$this->file] = SoftIncluder::includeFile($this->file);
87 2
		return self::$nsCache[$this->file];
88
	}
89
90 21
	public function set()
91
	{
92 21
		foreach ($this->ad->namespaces as $name)
93
		{
94 21
			$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...
95 21
		}
96 21
		$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...
97 21
		$mask = umask(0);
98 21
		file_put_contents($this->file, $data);
99 21
		umask($mask);
100 21
		self::$nsCache[$this->file] = $ns;
101 21
	}
102
103
	public function remove()
104
	{
105
		unset(self::$nsCache[$this->file]);
106
		if (file_exists($this->file))
107
		{
108
			unlink($this->file);
109
		}
110
	}
111
112 32
	private function isValid($ns)
113
	{
114
		// Fresh data
115 32
		if (empty($ns))
116 32
		{
117 2
			return true;
118
		}
119
120 30
		foreach ($this->ad->namespaces as $name)
121
		{
122 30
			if (empty($ns[$name]))
123 30
			{
124 3
				return false;
125
			}
126 30
		}
127 29
		return true;
128
	}
129
130
}
131