Completed
Push — dev ( 12acf4...3bce75 )
by Auke
13:27
created

ar_pinp   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 32.84%
Metric Value
wmc 26
lcom 1
cbo 2
dl 0
loc 101
ccs 22
cts 67
cp 0.3284
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A allow() 0 11 3
A allowMatch() 0 3 1
C isAllowed() 0 23 9
A getCallback() 0 19 4
A load() 0 4 1
B loaded() 0 18 5
A exists() 0 12 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 2 and the first side effect is on line 104.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
	class ar_pinp extends arBase {
3
4
		private static $allowed;
5
6 8
		public static function allow($class, $methods = null) {
7 8
			if (isset($methods)) {
8 8
				self::$allowed[$class]['methods'] = array_combine( $methods, array_fill( 0, count($methods), true ) );
9 8
				if (func_num_args()>2) {
10
					$args = array_slice(func_get_args(), 2);
11
					self::$allowed[$class]['implements'] = $args;
12
				}
13 8
			} else {
14 4
				self::$allowed[$class]['methods'] = true;
15
			}
16 8
		}
17
18
		public static function allowMatch($class, $methods) {
19
			self::$allowed[$class]['matches'] = array_combine( $methods, array_fill( 0, count($methods), true ) );
20
		}
21
22 12
		public static function isAllowed($class, $method) {
23
			// FIXME: support interfaces?
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "support interfaces?"
Loading history...
24 12
			if (!is_string($class)) {
25 12
				$class = get_class($class);
26 12
			}
27 12
			$current = $class;
28
			do {
29 12
				if (isset(self::$allowed[$current]) ) {
30 12
					if (self::$allowed[$current]['methods']===true) {
31 12
						return true;
32 4
					} else if (isset(self::$allowed[$current]['methods'][$method])
33 4
						&& self::$allowed[$current]['methods'][$method]) {
34 4
						return true;
35
					} else if (isset(self::$allowed[$current]['matches'])) {
36
						$result = preg_match( '/(' . implode ( '|', self::$allowed[$current]['matches']) . ')/is',  $method );
37
						if ($result) {
38
							return true;
39
						}
40
					}
41
				}
42
			} while ($current = get_parent_class($current));
43
			return false;
44
		}
45
46 4
		public static function getCallback( $method, $params = array() ) {
47 4
			if (is_string($method)) {
48 4
				return function() use ( $params, $method ) {
49
					$me = ar::context()->getObject();
0 ignored issues
show
Unused Code introduced by
$me is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
					$args_in = func_get_args();
51
					if (count($params)) {
52
						$args = array();
53
						foreach($params as $key => $arg) {
54
							$args[$arg] = $args_in[$key];
55
						}
56
					} else {
57
						$args = $args_in;
58
					}
59
					return ar::call($method, $args);
60 4
				};
61
			} else {
62
				return false;
63
			}
64
		}
65
66
		public static function load( $name, $library ) {
67
			$context = ar::context()->getObject();
68
			return $context->loadLibrary( $name, $library );
69
		}
70
71
		public static function loaded( $name = null, $library = null ) {
72
			global $ARConfig;
73
			// return if a library with that name is loaded
74
			// if library is set will also check if it is the same path
75
			$context = ar::context()->getObject();
76
			$path = $context->path;
77
			$config = $ARConfig->cache[$path] ?: $context->loadConfig($path);
78
			if ( isset($name) ) {
79
				$libraryPath = $config->libraries[$name] ?: null;
80
				if ( isset($library) ) {
81
					return ($libraryPath == $library );
82
				} else {
83
					return $libraryPath;
84
				}
85
			} else {
86
				return $config->libraries;
87
			}
88
		}
89
90
		public static function exists( $template ) {
91
			// will check if a template with the given name is available to call
92
			// template has format "library:type::function"
93
			// FIXME: allow search for specific nls as well
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "allow search for specific nls as well"
Loading history...
94
			$context = ar::context()->getObject();
95
			$templateData = $context->getPinpTemplate($template);
96
			if ( !$templateData || !$templateData['arTemplateId'] ) {
97
				return false;
98
			} else {
99
				return $templateData;
100
			}
101
		}
102
	}
103
104
	ar_pinp::allow('ar_pinp', array('isAllowed', 'getCallback', 'load', 'loaded', 'exists'));
105