Completed
Branchmaster (8f7741)
created

Module::process()   B

↳ Parent: Module

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