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 ( 6d6c9c...eb5d2b )
by gyeong-won
08:55
created

FrontEndFileHandler::getJsFileList()   D

Complexity

Conditions 9
Paths 28

Size

Total Lines 42
Code Lines 23

Duplication

Lines 33
Ratio 78.57 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 28
nop 1
dl 33
loc 42
rs 4.909
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
4
/**
5
 * Handle front end files
6
 * @author NAVER ([email protected])
7
 * */
8
class FrontEndFileHandler extends Handler
9
{
10
11
	static $isSSL = null;
12
13
	/**
14
	 * Map for css
15
	 * @var array
16
	 */
17
	var $cssMap = array();
18
19
	/**
20
	 * Map for Javascript at head
21
	 * @var array
22
	 */
23
	var $jsHeadMap = array();
24
25
	/**
26
	 * Map for Javascript at body
27
	 * @var array
28
	 */
29
	var $jsBodyMap = array();
30
31
	/**
32
	 * Index for css
33
	 * @var array
34
	 */
35
	var $cssMapIndex = array();
36
37
	/**
38
	 * Index for javascript at head
39
	 * @var array
40
	 */
41
	var $jsHeadMapIndex = array();
42
43
	/**
44
	 * Index for javascript at body
45
	 * @var array
46
	 */
47
	var $jsBodyMapIndex = array();
48
49
	/**
50
	 * Check SSL
51
	 *
52
	 * @return bool If using ssl returns true, otherwise returns false.
53
     * @deprecated
54
	 */
55
	function isSsl()
56
	{
57
		if(!is_null(self::$isSSL))
58
		{
59
			return self::$isSSL;
60
		}
61
62
		$url_info = parse_url(Context::getRequestUrl());
63
		self::$isSSL = ($url_info['scheme'] == 'https');
64
65
		return self::$isSSL;
66
	}
67
68
	/**
69
	 * Load front end file
70
	 *
71
	 * The $args is use as below. File type(js, css) is detected by file extension.
72
	 *
73
	 * <pre>
74
	 * case js
75
	 * 		$args[0]: file name
76
	 * 		$args[1]: type (head | body)
77
	 * 		$args[2]: target IE
78
	 * 		$args[3]: index
79
	 * case css
80
	 * 		$args[0]: file name
81
	 * 		$args[1]: media
82
	 * 		$args[2]: target IE
83
	 * 		$args[3]: index
84
	 * </pre>
85
	 *
86
	 * @param array $args Arguments
87
	 * @return void
88
	 * */
89
	function loadFile($args)
90
	{
91
		if(!is_array($args))
92
		{
93
			$args = array($args);
94
		}
95
		$file = $this->getFileInfo($args[0], $args[2], $args[1]);
96
97
		$availableExtension = array('css' => 1, 'js' => 1);
98
		if(!isset($availableExtension[$file->fileExtension]))
99
		{
100
			return;
101
		}
102
103
		$file->index = (int) $args[3];
104
105
		if($file->fileExtension == 'css')
106
		{
107
			$map = &$this->cssMap;
108
			$mapIndex = &$this->cssMapIndex;
109
110
			$this->_arrangeCssIndex($pathInfo['dirname'], $file);
0 ignored issues
show
Bug introduced by
The variable $pathInfo 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...
Documentation introduced by
$file is of type object<stdClass>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
		}
112 View Code Duplication
		else if($file->fileExtension == 'js')
113
		{
114
			if($args[1] == 'body')
115
			{
116
				$map = &$this->jsBodyMap;
117
				$mapIndex = &$this->jsBodyMapIndex;
118
			}
119
			else
120
			{
121
				$map = &$this->jsHeadMap;
122
				$mapIndex = &$this->jsHeadMapIndex;
123
			}
124
		}
125
126
		(is_null($file->index)) ? $file->index = 0 : $file->index = $file->index;
127
		if(!isset($mapIndex[$file->key]) || $mapIndex[$file->key] > $file->index)
128
		{
129
			$this->unloadFile($args[0], $args[2], $args[1]);
130
			$map[$file->index][$file->key] = $file;
0 ignored issues
show
Bug introduced by
The variable $map 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...
131
			$mapIndex[$file->key] = $file->index;
0 ignored issues
show
Bug introduced by
The variable $mapIndex 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...
132
		}
133
	}
134
135
	/**
136
	 * Get file information
137
	 *
138
	 * @param string $fileName The file name
139
	 * @param string $targetIe Target IE of file
140
	 * @param string $media Media of file
141
	 * @return stdClass The file information
142
	 */
143
	private function getFileInfo($fileName, $targetIe = '', $media = 'all')
144
	{
145
		static $existsInfo = array();
146
147
		if(isset($existsInfo[$existsKey]))
148
		{
149
			return $existsInfo[$existsKey];
0 ignored issues
show
Bug introduced by
The variable $existsKey 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...
150
		}
151
152
		$url_info = parse_url($fileName);
153
		$pathInfo = pathinfo(str_replace('?' . $url_info['query'], '', $fileName));
154
155
		$file = new stdClass();
156
		$file->fileName = basename($url_info['path']);
157
		$file->filePath = $this->_getAbsFileUrl($pathInfo['dirname']);
158
		$file->fileRealPath = FileHandler::getRealPath($pathInfo['dirname']);
159
		$file->fileExtension = strtolower($pathInfo['extension']);
160
		$file->fileNameNoExt = preg_replace('/\.min$/', '', $pathInfo['filename']);
161
		$file->query = $url_info['query'];
162
		$file->external = !!$url_info['host'];
163
		$file->keyName = implode('.', array($file->fileNameNoExt, $file->fileExtension));
164
		$file->cdnPath = $this->_normalizeFilePath($pathInfo['dirname']);
165
166
		if(!$file->external)
167
		{
168
			if(!__DEBUG__ && __XE_VERSION_STABLE__)
169
			{
170
				// if no debug mode, load minifed file
171
				$minifiedFileName = implode('.', array($file->fileNameNoExt, 'min', $file->fileExtension));
172
				$minifiedRealPath = implode('/', array($file->fileRealPath, $minifiedFileName));
173
				if(file_exists($minifiedRealPath))
174
				{
175
					$file->fileName = $minifiedFileName;
176
				}
177
			}
178
			else
179
			{
180
				// Remove .min
181
				if(file_exists(implode('/', array($file->fileRealPath, $file->keyName))))
182
				{
183
					$file->fileName = $file->keyName;
184
				}
185
			}
186
		}
187
188
		$file->targetIe = $targetIe;
189
190
		if($file->fileExtension == 'css')
191
		{
192
			$file->media = $media;
193
			if(!$file->media)
194
			{
195
				$file->media = 'all';
196
			}
197
			$file->key = $file->filePath . $file->keyName . "\t" . $file->targetIe . "\t" . $file->media;
198
		}
199
		else if($file->fileExtension == 'js')
200
		{
201
			$file->key = $file->filePath . $file->keyName . "\t" . $file->targetIe;
202
		}
203
204
		return $file;
205
	}
206
207
	/**
208
	 * Unload front end file
209
	 *
210
	 * @param string $fileName The file name to unload
211
	 * @param string $targetIe Target IE of file to unload
212
	 * @param string $media Media of file to unload. Only use when file is css.
213
	 * @return void
214
	 */
215
	function unloadFile($fileName, $targetIe = '', $media = 'all')
216
	{
217
		$file = $this->getFileInfo($fileName, $targetIe, $media);
218
219
		if($file->fileExtension == 'css')
220
		{
221
			if(isset($this->cssMapIndex[$file->key]))
222
			{
223
				$index = $this->cssMapIndex[$file->key];
224
				unset($this->cssMap[$index][$file->key], $this->cssMapIndex[$file->key]);
225
			}
226
		}
227
		else
228
		{
229
			if(isset($this->jsHeadMapIndex[$file->key]))
230
			{
231
				$index = $this->jsHeadMapIndex[$file->key];
232
				unset($this->jsHeadMap[$index][$file->key], $this->jsHeadMapIndex[$file->key]);
233
			}
234
			if(isset($this->jsBodyMapIndex[$file->key]))
235
			{
236
				$index = $this->jsBodyMapIndex[$file->key];
237
				unset($this->jsBodyMap[$index][$file->key], $this->jsBodyMapIndex[$file->key]);
238
			}
239
		}
240
	}
241
242
	/**
243
	 * Unload all front end file
244
	 *
245
	 * @param string $type Type to unload. all, css, js
246
	 * @return void
247
	 */
248
	function unloadAllFiles($type = 'all')
249
	{
250
		if($type == 'css' || $type == 'all')
251
		{
252
			$this->cssMap = array();
253
			$this->cssMapIndex = array();
254
		}
255
256
		if($type == 'js' || $type == 'all')
257
		{
258
			$this->jsHeadMap = array();
259
			$this->jsBodyMap = array();
260
			$this->jsHeadMapIndex = array();
261
			$this->jsBodyMapIndex = array();
262
		}
263
	}
264
265
	/**
266
	 * Get css file list
267
	 *
268
	 * @return array Returns css file list. Array contains file, media, targetie.
269
	 */
270
	function getCssFileList()
271
	{
272
		$map = &$this->cssMap;
273
		$mapIndex = &$this->cssMapIndex;
274
275
		$this->_sortMap($map, $mapIndex);
276
277
		$result = array();
278 View Code Duplication
		foreach($map as $indexedMap)
279
		{
280
			foreach($indexedMap as $file)
281
			{
282
				$query = '';
283
				if(!$file->external && is_readable($file->cdnPath . '/' . $file->fileName))
284
				{
285
					$query = date('YmdHis', filemtime($file->cdnPath . '/' . $file->fileName));
286
				}
287
				if($file->query)
288
				{
289
					if($query) $query .= '&';
290
					$query .= $file->query;
291
				}
292
				$query = ($query) ? '?' . $query : '';
293
294
				$fullFilePath = $file->filePath . '/' . $file->fileName . $query;
295
				$result[] = array(
296
					'file' => $fullFilePath,
297
					'media' => $file->media,
298
					'targetie' => $file->targetIe
299
				);
300
			}
301
		}
302
303
		return $result;
304
	}
305
306
	/**
307
	 * Get javascript file list
308
	 *
309
	 * @param string $type Type of javascript. head, body
310
	 * @return array Returns javascript file list. Array contains file, targetie.
311
	 */
312
	function getJsFileList($type = 'head')
313
	{
314 View Code Duplication
		if($type == 'head')
315
		{
316
			$map = &$this->jsHeadMap;
317
			$mapIndex = &$this->jsHeadMapIndex;
318
		}
319
		else
320
		{
321
			$map = &$this->jsBodyMap;
322
			$mapIndex = &$this->jsBodyMapIndex;
323
		}
324
325
		$this->_sortMap($map, $mapIndex);
326
327
		$result = array();
328 View Code Duplication
		foreach($map as $indexedMap)
329
		{
330
			foreach($indexedMap as $file)
331
			{
332
				$query = '';
333
				if(!$file->external && is_readable($file->cdnPath . '/' . $file->fileName))
334
				{
335
					$query = date('YmdHis', filemtime($file->cdnPath . '/' . $file->fileName));
336
				}
337
				if($file->query)
338
				{
339
					if($query) $query .= '&';
340
					$query .= $file->query;
341
				}
342
				$query = ($query) ? '?' . $query : '';
343
344
				$fullFilePath = $file->filePath . '/' . $file->fileName . $query;
345
				$result[] = array(
346
					'file' => $fullFilePath,
347
					'targetie' => $file->targetIe
348
				);
349
			}
350
		}
351
352
		return $result;
353
	}
354
355
	/**
356
	 * Sort a map
357
	 *
358
	 * @param array $map Array to sort
359
	 * @param array $index Not used
360
	 * @return void
361
	 */
362
	function _sortMap(&$map, &$index)
0 ignored issues
show
Unused Code introduced by
The parameter $index 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...
363
	{
364
		ksort($map);
365
	}
366
367
	/**
368
	 * Normalize File path
369
	 *
370
	 * @param string $path Path to normalize
371
	 * @return string Normalized path
372
	 */
373
	function _normalizeFilePath($path)
374
	{
375
		if(strpos($path, '://') === FALSE && $path{0} != '/' && $path{0} != '.')
376
		{
377
			$path = './' . $path;
378
		}
379
		elseif(!strncmp($path, '//', 2))
380
		{
381
			return preg_replace('#^//+#', '//', $path);
382
		}
383
384
		$path = preg_replace('@/\./|(?<!:)\/\/@', '/', $path);
385
386
		while(strpos($path, '/../'))
387
		{
388
			$path = preg_replace('/\/([^\/]+)\/\.\.\//s', '/', $path, 1);
389
		}
390
391
		return $path;
392
	}
393
394
	/**
395
	 * Get absolute file url
396
	 *
397
	 * @param string $path Path to get absolute url
398
	 * @return string Absolute url
399
	 */
400
	function _getAbsFileUrl($path)
401
	{
402
		$path = $this->_normalizeFilePath($path);
403
		$script_path = getScriptPath();
404
405
		if(strpos($path, './') === 0)
406
		{
407
			if($script_path == '/' || $script_path == '\\')
408
			{
409
				$path = '/' . substr($path, 2);
410
			}
411
			else
412
			{
413
				$path = $script_path . substr($path, 2);
414
			}
415
		}
416
		else if(strpos($file, '../') === 0)
0 ignored issues
show
Bug introduced by
The variable $file 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...
417
		{
418
			$path = $this->_normalizeFilePath($script_path . $path);
419
		}
420
421
		return $path;
422
	}
423
424
	/**
425
	 * Arrage css index
426
	 *
427
	 * @param string $dirName First directory  name of css path
428
	 * @param array $file file info.
429
	 * @return void
430
	 */
431
	function _arrangeCssIndex($dirName, &$file)
432
	{
433
		if($file->index !== 0)
434
		{
435
			return;
436
		}
437
438
		$dirName = str_replace('./', '', $dirName);
439
		$tmp = explode('/', $dirName);
440
441
		$cssSortList = array('common' => -100000, 'layouts' => -90000, 'modules' => -80000, 'widgets' => -70000, 'addons' => -60000);
442
		$file->index = $cssSortList[$tmp[0]];
443
	}
444
445
}
446
/* End of file FrontEndFileHandler.class.php */
447
/* Location: ./classes/frontendfile/FrontEndFileHandler.class.php */
448