Completed
Branch master (48c558)
by
unknown
05:41
created

Module::process()   B

Complexity

Conditions 9
Paths 6

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 54
Code Lines 25

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 7.255
c 0
b 0
f 0
cc 9
eloc 25
nc 6
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Redaxscript\Parser\Tag;
3
4
use Redaxscript\Config;
5
use Redaxscript\Hook;
6
use Redaxscript\Request;
7
8
/**
9
 * children class to parse content for module tags
10
 *
11
 * @since 3.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Parser
15
 * @author Henry Ruhs
16
 */
17
18
class Module extends TagAbstract
19
{
20
	/**
21
	 * options of the module tag
22
	 *
23
	 * @var array
24
	 */
25
26
	protected $_optionArray =
27
	[
28
		'search' =>
29
		[
30
			'<module>',
31
			'</module>'
32
		],
33
		'namespace' => 'Redaxscript\Modules',
34
		'delimiter' => '@@@'
35
	];
36
37
	/**
38
	 * process the class
39
	 *
40
	 * @since 3.0.0
41
	 *
42
	 * @param string $content content to be parsed
43
	 *
44
	 * @return string
45
	 */
46
47
	public function process($content = null)
48
	{
49
		$output = str_replace($this->_optionArray['search'], $this->_optionArray['delimiter'], $content);
50
		$partArray = array_filter(explode($this->_optionArray['delimiter'], $output));
51
		$modulesLoaded = Hook::getModuleArray();
52
53
		/* parse as needed */
54
55
		foreach ($partArray as $key => $value)
56
		{
57
			$moduleClass = $this->_optionArray['namespace'] . '\\' . $value . '\\' . $value;
58
			if ($key % 2)
59
			{
60
				$partArray[$key] = null;
61
				$json = json_decode($value, true);
62
63
				/* call with parameter */
64
65
				if (is_array($json))
66
				{
67
					foreach ($json as $moduleName => $parameterArray)
68
					{
69
						$moduleClass = $this->_optionArray['namespace'] . '\\' . $moduleName . '\\' . $moduleName;
70
71
						/* method exists */
72
73
						if (in_array($moduleName, $modulesLoaded) && method_exists($moduleClass, 'render'))
74
						{
75
							$module = new $moduleClass($this->_registry, Request::getInstance(), $this->_language, Config::getInstance());
76
							$partArray[$key] = call_user_func_array(
77
							[
78
								$module,
79
								'render'
80
							], $parameterArray);
81
						}
82
					}
83
				}
84
85
				/* else simple call */
86
87
				else if (in_array($value, $modulesLoaded) && method_exists($moduleClass, 'render'))
88
				{
89
					$module = new $moduleClass($this->_registry, Request::getInstance(), $this->_language, Config::getInstance());
90
					$partArray[$key] = call_user_func(
91
					[
92
						$module,
93
						'render'
94
					]);
95
				}
96
			}
97
		}
98
		$output = implode($partArray);
99
		return $output;
100
	}
101
}