GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( ca4e83...78be4d )
by gyeong-won
13:50
created

layoutView   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 412
Duplicated Lines 14.32 %

Coupling/Cohesion

Components 1
Dependencies 9
Metric Value
wmc 55
lcom 1
cbo 9
dl 59
loc 412
rs 6.8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A dispLayoutInfo() 12 12 2
F dispLayoutPreviewWithModule() 37 195 26
C procRealModule() 0 66 9
C dispLayoutPreview() 10 67 13
A getRealLayoutFile() 0 15 2
A getRealLayoutCSS() 0 15 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like layoutView often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use layoutView, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
/**
4
 * @class  layoutView
5
 * @author NAVER ([email protected])
6
 * admin view class of the layout module
7
 */
8
class layoutView extends layout
9
{
10
	/**
11
	 * Initialization
12
	 * @return void
13
	 */
14
	function init()
15
	{
16
		$this->setTemplatePath($this->module_path.'tpl');
0 ignored issues
show
Bug introduced by
The property module_path cannot be accessed from this context as it is declared private in class ModuleObject.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
17
	}
18
19
	/**
20
	 * Pop-up layout details(conf/info.xml)
21
	 * @return void
22
	 */
23 View Code Duplication
	function dispLayoutInfo()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
	{
25
		// Get the layout information
26
		$oLayoutModel = getModel('layout');
27
		$layout_info = $oLayoutModel->getLayoutInfo(Context::get('selected_layout'));
28
		if(!$layout_info) exit();
29
		Context::set('layout_info', $layout_info);
30
		// Set the layout to be pop-up
31
		$this->setLayoutFile('popup_layout');
32
		// Set a template file
33
		$this->setTemplateFile('layout_detail_info');
34
	}
35
36
	/**
37
	 * Preview a layout with module.
38
	 * 
39
	 * @return Object
40
	 */
41
	public function dispLayoutPreviewWithModule()
42
	{
43
		$content = '';
44
		$layoutSrl = Context::get('layout_srl');
45
46
		$module = Context::get('module_name');
47
		$mid = Context::get('target_mid');
48
		$skin = Context::get('skin');
49
		$skinType = Context::get('skin_type');
50
51
		try
52
		{
53
			// admin check
54
			// this act is admin view but in normal view because do not load admin css/js files
55
			$logged_info = Context::get('logged_info');
56
			if($logged_info->is_admin != 'Y')
57
			{
58
				throw new Exception(Context::getLang('msg_invalid_request'));
59
			}
60
61
			// if module is 'ARTiCLE' and from site design setting, make content directly
62
			if($module == 'ARTICLE' && !$mid)
63
			{
64
				$oDocumentModel = getModel('document');
65
				$oDocument = $oDocumentModel->getDocument(0, true);
66
67
				$t = Context::getLang('article_preview_title');
68
69
				$c = '';
70
				for($i = 0; $i < 4; $i++)
71
				{
72
					$c .= '<p>';
73
					for($j = 0; $j < 20; $j++)
74
					{
75
						$c .= Context::getLang('article_preview_content') . ' ';
76
					}
77
					$c .= '</p>';
78
				}
79
80
				$attr = new stdClass();
81
				$attr->title = $t;
82
				$attr->content = $c;
83
				$attr->document_srl = -1;
84
				$oDocument->setAttribute($attr, FALSE);
85
86
				Context::set('oDocument', $oDocument);
87
88
				if ($skinType == 'M')
89
				{
90
					$templatePath = _XE_PATH_ . 'modules/page/m.skins/' . $skin;
91
					$templateFile = 'mobile';
92
				}
93
				else
94
				{
95
					$templatePath = _XE_PATH_ . 'modules/page/skins/' . $skin;
96
					$templateFile = 'content';
97
				}
98
99
				$oTemplate = TemplateHandler::getInstance();
100
				$content = $oTemplate->compile($templatePath, $templateFile);
101
			}
102
103
			// else use real module
104
			else
105
			{
106
				$content = $this->procRealModule($module, $mid, $skin, $skinType);
107
			}
108
			Context::set('content', $content);
109
110
			// find layout
111
			if($layoutSrl)
112
			{
113
				if($layoutSrl == -1)
114
				{
115
					$site_srl = ($oModule) ? $oModule->module_info->site_srl : 0;
0 ignored issues
show
Bug introduced by
The variable $oModule does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
116
					$designInfoFile = sprintf(_XE_PATH_ . 'files/site_design/design_%s.php', $site_srl);
117
					include($designInfoFile);
118
119
					if($skinType == 'M')
120
					{
121
						$layoutSrl = $designInfo->mlayout_srl;
0 ignored issues
show
Bug introduced by
The variable $designInfo does not exist. Did you mean $designInfoFile?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
122
					}
123
					else
124
					{
125
						$layoutSrl = $designInfo->layout_srl;
0 ignored issues
show
Bug introduced by
The variable $designInfo does not exist. Did you mean $designInfoFile?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
126
					}
127
				}
128
129
				$oLayoutModel = getModel('layout');
130
				$layoutInfo = $oLayoutModel->getLayout($layoutSrl);
0 ignored issues
show
Bug introduced by
The method getLayout() does not exist on ModuleObject. Did you maybe mean getLayoutFile()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
131
132
				// If there is no layout, pass it.
133
				if($layoutInfo)
134
				{
135
					// Adhoc...
136
137
					// Input extra_vars into $layout_info
138 View Code Duplication
					if($layoutInfo->extra_var_count)
139
					{
140
141
						foreach($layoutInfo->extra_var as $var_id => $val)
142
						{
143
							if($val->type == 'image')
144
							{
145
								if(strncmp('./files/attach/images/', $val->value, 22) === 0)
146
								{
147
									$val->value = Context::getRequestUri() . substr($val->value, 2);
148
								}
149
							}
150
							$layoutInfo->{$var_id} = $val->value;
151
						}
152
					}
153
154
					// Set menus into context
155
					if($layoutInfo->menu_count)
156
					{
157
						foreach($layoutInfo->menu as $menu_id => $menu)
158
						{
159
							// set default menu set(included home menu)
160 View Code Duplication
							if(!$menu->menu_srl || $menu->menu_srl == -1)
161
							{
162
								$oMenuAdminController = getAdminController('menu');
163
								$homeMenuCacheFile = $oMenuAdminController->getHomeMenuCacheFile();
164
165
								if(file_exists($homeMenuCacheFile))
166
								{
167
									include($homeMenuCacheFile);
168
								}
169
170
								if(!$menu->menu_srl)
171
								{
172
									$menu->xml_file = str_replace('.xml.php', $homeMenuSrl . '.xml.php', $menu->xml_file);
0 ignored issues
show
Bug introduced by
The variable $homeMenuSrl does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
173
									$menu->php_file = str_replace('.php', $homeMenuSrl . '.php', $menu->php_file);
174
									$layoutInfo->menu->{$menu_id}->menu_srl = $homeMenuSrl;
175
								}
176
								else
177
								{
178
									$menu->xml_file = str_replace($menu->menu_srl, $homeMenuSrl, $menu->xml_file);
179
									$menu->php_file = str_replace($menu->menu_srl, $homeMenuSrl, $menu->php_file);
180
								}
181
							}
182
183
							$menu->php_file = FileHandler::getRealPath($menu->php_file);
184
							if(FileHandler::exists($menu->php_file))
0 ignored issues
show
Bug Best Practice introduced by
The expression \FileHandler::exists($menu->php_file) of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
185
							{
186
								include($menu->php_file);
187
							}
188
							Context::set($menu_id, $menu);
189
						}
190
					}
191
192
					Context::set('layout_info', $layoutInfo);
193
				}
194
			}
195
		}
196
		catch(Exception $e)
197
		{
198
			$content = '<div class="message error"><p id="preview_error">' . $e->getMessage() . '</p></div>';
199
			Context::set('content', $content);
200
			$layoutSrl = 0;
201
		}
202
203
		// Compile
204
		$oTemplate = TemplateHandler::getInstance();
205
		Context::clearHtmlHeader();
206
207
		if($layoutInfo)
208
		{
209
			$layout_path = $layoutInfo->path;
0 ignored issues
show
Bug introduced by
The variable $layoutInfo does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
210
			$editLayoutTPL = $this->getRealLayoutFile($layoutSrl);
211
			$editLayoutCSS = $this->getRealLayoutCSS($layoutSrl);
212
			if($editLayoutCSS != '')
213
			{
214
				Context::addCSSFile($editLayoutCSS);
0 ignored issues
show
Deprecated Code introduced by
The method Context::addCSSFile() has been deprecated.

This method has been deprecated.

Loading history...
215
			}
216
			$layout_file = 'layout';
217
			$oModuleModel = getModel('module');
218
			$part_config = $oModuleModel->getModulePartConfig('layout', $layoutSrl);
219
			Context::addHtmlHeader($part_config->header_script);
220
		}
221
		else
222
		{
223
			$layout_path = './common/tpl';
224
			$layout_file = 'default_layout';
225
		}
226
227
		$layout_tpl = $oTemplate->compile($layout_path, $layout_file, $editLayoutTPL);
0 ignored issues
show
Bug introduced by
The variable $editLayoutTPL does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
228
		Context::set('layout','none');
229
230
		// Convert widgets and others
231
		$oContext = Context::getInstance();
0 ignored issues
show
Unused Code introduced by
$oContext 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...
232
		Context::set('layout_tpl', $layout_tpl);
233
		$this->setTemplatePath($this->module_path.'tpl');
0 ignored issues
show
Bug introduced by
The property module_path cannot be accessed from this context as it is declared private in class ModuleObject.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
234
		$this->setTemplateFile('layout_preview');
235
	}
236
237
	/**
238
	 * Get content of real module
239
	 *
240
	 * @param string $module module name
241
	 * @param string $mid module id
242
	 * @param string $skin skin name
243
	 * @param string $skinType PC(P) or mobile(M)
244
	 * @return string content of real module
245
	 */
246
	private function procRealModule($module, $mid, $skin, $skinType)
247
	{
248
		// if form site design and preview module, find target module
249
		if($module && !$mid)
250
		{
251
			$args = new stdClass();
252
			$args->module = $module;
253
			$output = executeQuery('layout.getOneModuleInstanceByModuleName', $args);
254
			if(!$output->toBool())
255
			{
256
				throw new Exception($output->getMessage());
257
			}
258
259
			// if there is no module instance, error...
260
			if(!$output->data)
261
			{
262
				throw new Exception(Context::getLang('msg_unabled_preview'));
263
			}
264
		
265
			$mid = current($output->data)->mid;
266
		}
267
268
		// if form site design and preview layout, find start module
269
		elseif(!$module && !$mid)
270
		{
271
			$oModuleModel = getModel('module');
272
			$columnList = array('modules.mid', 'sites.index_module_srl');
273
			$startModuleInfo = $oModuleModel->getSiteInfo(0, $columnList);
274
			$mid = $startModuleInfo->mid;
275
		}
276
277
		$oModuleHandler = new ModuleHandler('', '', $mid, '', '');
278
279
		// Adhoc...
280
		$oModuleHandler->act = '';
0 ignored issues
show
Bug introduced by
The property act cannot be accessed from this context as it is declared private in class ModuleHandler.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
281
282
		$oModuleHandler->init();
283
284
		// Adhoc...
285
		$oModuleHandler->module_info->use_mobile = 'Y';
0 ignored issues
show
Bug introduced by
The property module_info cannot be accessed from this context as it is declared private in class ModuleHandler.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
286
		$oModuleHandler->module_info->is_skin_fix = 'Y';
0 ignored issues
show
Bug introduced by
The property module_info cannot be accessed from this context as it is declared private in class ModuleHandler.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
287
		$oModuleHandler->module_info->is_mskin_fix = 'Y';
0 ignored issues
show
Bug introduced by
The property module_info cannot be accessed from this context as it is declared private in class ModuleHandler.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
288
289
		if($skinType == 'M')
290
		{
291
			Mobile::setMobile(TRUE);
292
			$oModuleHandler->module_info->mskin = $skin;
0 ignored issues
show
Bug introduced by
The property module_info cannot be accessed from this context as it is declared private in class ModuleHandler.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
293
		}
294
		else
295
		{
296
			Mobile::setMobile(FALSE);
297
			$oModuleHandler->module_info->skin = $skin;
0 ignored issues
show
Bug introduced by
The property module_info cannot be accessed from this context as it is declared private in class ModuleHandler.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
298
		}
299
300
		// Proc module
301
		$oModule = $oModuleHandler->procModule();
302
		if(!$oModule->toBool())
303
		{
304
			throw new Exception(Context::getLang('not_support_layout_preview'));
305
		}
306
307
		// get module html
308
		require_once(_XE_PATH_ . "classes/display/HTMLDisplayHandler.php");
309
		$handler = new HTMLDisplayHandler();
310
		return $handler->toDoc($oModule);
311
	}
312
313
	/**
314
	 * Preview a layout
315
	 * @return void|Object (void : success, Object : fail)
316
	 */
317
	function dispLayoutPreview()
318
	{
319
		if(!checkCSRF())
320
		{
321
			$this->stop('msg_invalid_request');
322
			return new Object(-1, 'msg_invalid_request');
323
		}
324
325
		// admin check
326
		// this act is admin view but in normal view because do not load admin css/js files
327
		$logged_info = Context::get('logged_info');
328
		if($logged_info->is_admin != 'Y') return $this->stop('msg_invalid_request');
329
330
		$layout_srl = Context::get('layout_srl');
331
		$code = Context::get('code');
332
333
		$code_css = Context::get('code_css');
334
		if(!$layout_srl || !$code) return new Object(-1, 'msg_invalid_request');
335
		// Get the layout information
336
		$oLayoutModel = getModel('layout');
337
		$layout_info = $oLayoutModel->getLayout($layout_srl);
0 ignored issues
show
Bug introduced by
The method getLayout() does not exist on ModuleObject. Did you maybe mean getLayoutFile()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
338
		if(!$layout_info) return new Object(-1, 'msg_invalid_request');
339
		// Separately handle the layout if its type is faceoff
340
		if($layout_info && $layout_info->type == 'faceoff') $oLayoutModel->doActivateFaceOff($layout_info);
341
		// Apply CSS directly
342
		Context::addHtmlHeader("<style type=\"text/css\" charset=\"UTF-8\">".$code_css."</style>");
343
		// Set names and values of extra_vars to $layout_info
344
		if($layout_info->extra_var_count)
345
		{
346
			foreach($layout_info->extra_var as $var_id => $val)
347
			{
348
				$layout_info->{$var_id} = $val->value;
349
			}
350
		}
351
		// menu in layout information becomes an argument for Context:: set
352 View Code Duplication
		if($layout_info->menu_count)
353
		{
354
			foreach($layout_info->menu as $menu_id => $menu)
355
			{
356
				$menu->php_file = FileHandler::getRealPath($menu->php_file);
357
				if(FileHandler::exists($menu->php_file)) include($menu->php_file);
0 ignored issues
show
Bug Best Practice introduced by
The expression \FileHandler::exists($menu->php_file) of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
358
359
				Context::set($menu_id, $menu);
360
			}
361
		}
362
363
		Context::set('layout_info', $layout_info);
364
		Context::set('content', Context::getLang('layout_preview_content'));
365
		// Temporary save the codes
366
		$edited_layout_file = _XE_PATH_ . 'files/cache/layout/tmp.tpl';
367
		FileHandler::writeFile($edited_layout_file, $code);
368
369
		// Compile
370
		$oTemplate = &TemplateHandler::getInstance();
371
372
		$layout_path = $layout_info->path;
373
		$layout_file = 'layout';
374
375
		$layout_tpl = $oTemplate->compile($layout_path, $layout_file, $edited_layout_file);
376
		Context::set('layout','none');
377
		// Convert widgets and others
378
		$oContext = &Context::getInstance();
379
		Context::set('layout_tpl', $layout_tpl);
380
		// Delete Temporary Files
381
		FileHandler::removeFile($edited_layout_file);
382
		$this->setTemplateFile('layout_preview');
383
	}
384
385
	private function getRealLayoutFile($layoutSrl)
386
	{
387
		$oLayoutModel = getModel('layout');
388
		$layoutFile = $oLayoutModel->getUserLayoutHtml($layoutSrl);
389
390
		if(file_exists($layoutFile))
391
		{
392
			return $layoutFile;
393
		}
394
		else
395
		{
396
			return ''; 
397
		}
398
399
	}
400
401
	private function getRealLayoutCSS($layoutSrl)
402
	{
403
		$oLayoutModel = getModel('layout');
404
		$cssFile = $oLayoutModel->getUserLayoutCss($layoutSrl);
405
406
		if(file_exists($cssFile))
407
		{
408
			return $cssFile;
409
		}
410
		else
411
		{
412
			return ''; 
413
		}
414
415
	}
416
417
418
419
}
420
/* End of file layout.view.php */
421
/* Location: ./modules/layout/layout.view.php */
422