Completed
Pull Request — master (#4)
by Matt
01:51
created

main_module::main()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 101
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 101
rs 4.8196
cc 10
eloc 49
nc 24
nop 2

How to fix   Long Method    Complexity   

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
/**
3
 *
4
 * phpBB Media Embed PlugIn extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2016 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\mediaembed\acp;
12
13
/**
14
 * phpBB Media Embed Plugin ACP module.
15
 */
16
class main_module
17
{
18
	public $u_action;
19
20
	public function main($id, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
	{
22
		global $phpbb_container;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
23
24
		/** @var \phpbb\cache\driver\driver_interface $cache */
25
		$cache = $phpbb_container->get('cache');
26
27
		/** @var \phpbb\config\config $config */
28
		$config = $phpbb_container->get('config');
29
30
		/** @var \phpbb\request\request $request */
31
		$request = $phpbb_container->get('request');
32
33
		/** @var \phpbb\template\template $template */
34
		$template = $phpbb_container->get('template');
35
36
		/** @var \phpbb\language\language $language */
37
		$language = $phpbb_container->get('language');
38
		$language->add_lang('acp', 'phpbb/mediaembed');
39
40
		$form_key = 'phpbb/mediaembed';
41
		add_form_key($form_key);
42
43
		switch ($mode)
44
		{
45
			case 'manage':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
46
47
				$this->tpl_name = 'acp_phpbb_mediaembed_manage';
0 ignored issues
show
Bug introduced by
The property tpl_name 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...
48
				$this->page_title = $language->lang('ACP_MEDIA_MANAGE');
0 ignored issues
show
Bug introduced by
The property page_title 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...
49
50
				/** @var \phpbb\config\db_text $config_text */
51
				$config_text = $phpbb_container->get('config_text');
52
53
				if ($request->is_set_post('submit'))
54
				{
55
					if (!check_form_key($form_key))
56
					{
57
						trigger_error('FORM_INVALID');
58
					}
59
60
					$config_text->set('media_embed_sites', json_encode($request->variable('mark', [''])));
61
62
					$cache->destroy($phpbb_container->getParameter('text_formatter.cache.parser.key'));
63
					$cache->destroy($phpbb_container->getParameter('text_formatter.cache.renderer.key'));
64
65
					trigger_error($language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
66
				}
67
68
				$sites = [];
69
70
				$allowed_sites = $config_text->get('media_embed_sites');
71
				$allowed_sites = $allowed_sites ? json_decode($allowed_sites, true) : [];
72
73
				/** @var \s9e\TextFormatter\Configurator $configurator */
74
				$configurator = $phpbb_container->get('text_formatter.s9e.factory')->get_configurator();
75
				foreach ($configurator->MediaEmbed->defaultSites->getIds() as $siteId)
76
				{
77
					if (isset($configurator->BBCodes[$siteId]))
78
					{
79
						continue;
80
					}
81
82
					$sites[] = [
83
						'name'		=> $siteId,
84
						'checked'	=> in_array($siteId, $allowed_sites),
85
					];
86
				}
87
88
				$template->assign_vars([
89
					'U_ACTION'		=> $this->u_action,
90
					'MEDIA_SITES'	=> $sites,
91
				]);
92
93
			break;
94
95
			case 'settings':
96
			default:
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
97
98
				$this->tpl_name = 'acp_phpbb_mediaembed_settings';
99
				$this->page_title = $language->lang('ACP_MEDIA_SETTINGS');
100
101
				if ($request->is_set_post('submit'))
102
				{
103
					if (!check_form_key($form_key))
104
					{
105
						trigger_error('FORM_INVALID');
106
					}
107
108
					$config->set('media_embed_bbcode', $request->variable('media_embed_bbcode', 0));
109
110
					trigger_error($language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
111
				}
112
113
				$template->assign_vars([
114
					'U_ACTION'				=> $this->u_action,
115
					'S_MEDIA_EMBED_BBCODE'	=> $config['media_embed_bbcode'],
116
				]);
117
118
			break;
119
		}
120
	}
121
}
122