mod_grant::compile()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 0
loc 11
rs 9.6111
c 0
b 0
f 0
ccs 0
cts 11
cp 0
crap 30
1
<?php
2
	define("T_G_IDENT",		253);
3
4
	class mod_grant {
5
		function get_token() {
6
			$this->token = $this->token_ahead;
0 ignored issues
show
Bug introduced by
The property token 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 property token_ahead 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...
7
			$this->token_value = $this->token_ahead_value;
0 ignored issues
show
Bug introduced by
The property token_value 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 property token_ahead_value does not seem to exist. Did you mean token_ahead?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
8
			$this->input = preg_replace('/^([[:space:]]|,)*/', '', $this->input);
0 ignored issues
show
Bug introduced by
The property input 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...
9
			//echo "scanner: input(".$this->input.")<br>\n";
10
			switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^([a-zA-Z0-...', $this->input, $regs) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^([]+->=(){...', $this->input, $regs) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
11 View Code Duplication
				case preg_match('/^([a-zA-Z0-9_][a-zA-Z0-9._:]*)/', $this->input, $regs):
12
					$this->token_ahead = T_G_IDENT;
13
					$this->token_ahead_value = $regs[1];
0 ignored issues
show
Bug introduced by
The property token_ahead_value does not seem to exist. Did you mean token_ahead?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The variable $regs seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
14
					$this->input = substr($this->input, strlen($regs[1]));
0 ignored issues
show
Bug introduced by
The variable $regs seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
15
				break;
16 View Code Duplication
				case preg_match('/^([]+->=(){}[])/', $this->input, $regs):
17
					$this->token_ahead = $regs[1];
0 ignored issues
show
Bug introduced by
The variable $regs seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
18
					$this->token_ahead_value = $regs[1];
0 ignored issues
show
Bug introduced by
The property token_ahead_value does not seem to exist. Did you mean token_ahead?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The variable $regs seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
19
					$this->input = substr($this->input, 1);
20
				break;
21
				default:
22
					$this->token_ahead = 0;
23
					$this->token_ahead_value = '';
0 ignored issues
show
Bug introduced by
The property token_ahead_value does not seem to exist. Did you mean token_ahead?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
24
			}
25
			//echo "scanner:: (".$this->token_ahead.") (".$this->token_ahead_value.")<br>\n";
26
			return $this->token_ahead;
27
		}
28
29
		function parse_stmt(&$grants) {
30
			$g_array = Array();
31
			switch ($this->token_ahead) {
32
				case '-':
33
					$g_op = '-';
34
					$this->get_token();
35
				break;
36
				case '+':
37
					$this->get_token();
38
				default:
39
					$g_op = '+';
40
			}
41
42 View Code Duplication
			switch ($this->token_ahead) {
43
				case '>':
44
					$g_val = ARGRANTCHILDREN;
45
					$this->get_token();
46
				break;
47
				case '=':
48
					$g_val = ARGRANTLOCAL;
49
					$this->get_token();
50
				break;
51
				default:
52
					$g_val = ARGRANTGLOBAL;
53
			}
54
55
			if ($this->token_ahead === '[') {
56
				$this->get_token();
57
					while ($this->token_ahead === T_G_IDENT) {
58
						$this->get_token();
59
						$g_array[$this->token_value] = $g_val;
60
					}
61
				if (!$this->token_ahead === ']') {
62
					$this->error = 'expected ]';
0 ignored issues
show
Bug introduced by
The property error 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...
63
					return 0;
64
				}
65
				$this->get_token();
66
			} else
67
			if ($this->token_ahead === T_G_IDENT) {
68
				$this->get_token();
69
				$g_array[$this->token_value] = $g_val;
70
			} else {
71
				return 0;
72
			}
73
74
			$m_array = Array();
75
76
			/* parse modifiers */
77
			if ($this->token_ahead === '(') {
78
				$this->get_token();
79
				do {
80 View Code Duplication
					switch ($this->token_ahead) {
81
						case '>':
82
							$m_val = ARGRANTCHILDREN;
83
							$this->get_token();
84
						break;
85
						case '=':
86
							$m_val = ARGRANTLOCAL;
87
							$this->get_token();
88
						break;
89
						default:
90
							$m_val = $g_val;
91
					}
92
					if ($this->token_ahead != T_G_IDENT) {
93
						$this->error = 'expected modifier near: '.$this->input;
94
						return 0;
95
					}
96
					$this->get_token();
97
					$m_array[$this->token_value] = $m_val;
98
				} while ($this->token_ahead && $this->token_ahead != ')');
99
100
				if ($this->token_ahead != ')') {
101
					$this->error = 'modifier list is not closed';
102
					return 0;
103
				}
104
				$this->get_token();
105
106
				foreach($g_array as $grant => $g_val) {
107
					$g_array[$grant] = $m_array;
108
				}
109
			}
110
111
			foreach($g_array as $grant => $g_val) {
112
				if ($g_op === '-') {
113
					unset($grants[$grant]);
114
				} else {
115
					$grants[$grant] = $g_val;
116
				}
117
			}
118
			return 1;
119
		}
120
121
		function compile($input, &$grants) {
122
			$this->input = $input;
123
			$this->get_token();
124
			while (!$this->error && $this->parse_stmt($grants));
125
			ksort($grants);
126
			foreach( $grants as $key => $value ) {
127
				if( is_array($value) ) {
128
					ksort($grants[$key]);
129
				}
130
			}
131
		}
132
	}
133