Completed
Push — php7.2-travis ( 46fda5...100f27 )
by
unknown
249:00 queued 239:17
created

pinp_keepurl   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 95
Duplicated Lines 3.16 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 10.98%

Importance

Changes 0
Metric Value
dl 3
loc 95
ccs 9
cts 82
cp 0.1098
rs 10
c 0
b 0
f 0
wmc 21
lcom 0
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B _make_path() 3 15 6
A _loadConfig() 0 14 3
A _currentsection() 0 4 1
A _currentsite() 0 4 1
A _get() 0 17 2
C _make_real_path() 0 32 8

How to fix   Duplicated Code   

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:

1
<?php
2
3
	class pinp_keepurl {
4
5
		public static function _make_path($path=".") {
6
		global $ARCurrent;
7
			$context = pobject::getContext();
8
			$me = $context["arCurrentObject"];
9
			$path = $me->make_path($path);
10
			$newpath = $path;
11
			$redirects = $ARCurrent->shortcut_redirect;
12
			if (is_array($redirects)) {
13 View Code Duplication
				while (count($redirects) && ($redir = array_pop($redirects)) && substr($newpath, 0, strlen($redir['dest'])) == $redir['dest'] && $redir['keepurl']) {
14
					$newpath = $redir['src'].substr($newpath, strlen($redir['dest']));
15
				}
16
			}
17
18
			return $newpath;
19
		}
20
21
		public static function _loadConfig($path='.') {
22
			global $ARCurrent;
23
			$context = pobject::getContext();
24
			$me = $context["arCurrentObject"];
25
			$path = $me->make_path($path);
26
			if (@count($ARCurrent->shortcut_redirect)) {
27
				$redir = reset($ARCurrent->shortcut_redirect);
28
				if ($redir["keepurl"]) { // && substr($path, 0, strlen($redir["dest"])) == $redir["dest"]) {
29
					$path = $redir["src"];
30
				}
31
			}
32
			$config=$me->loadConfig($path);
33
			return $config;
34
		}
35
36
		public static function _currentsection($path=".") {
37
			$config = self::_loadConfig($path);
38
			return $config->section;
39
		}
40
41
		public static function _currentsite($path=".") {
42
			$config = self::_loadConfig($path);
43
			return $config->site;
44
		}
45
46
		public static function _get($path, $template, $args='') {
47
		global $ARCurrent;
48
			// for now we have to remove all current redirects
49
			$old_redirects = $ARCurrent->shortcut_redirect;
50
			$ARCurrent->shortcut_redirect = Array();
51
			$realpath = self::_make_real_path($path, $ARCurrent->shortcut_redirect);
52
			if ($realpath) {
53
				$context = pobject::getContext();
54
				$me = $context["arCurrentObject"];
55
				$result = $me->get( $realpath, $template, $args );
56
			} else {
57
				$result = Array();
58
			}
59
			// restore redirects
60
			$ARCurrent->shortcut_redirect = $old_redirects;
61
			return $result;
62
		}
63
64 1
		public static function _make_real_path($path, &$redirects = Array()) {
65 1
			$context = pobject::getContext();
66 1
			$me = $context["arCurrentObject"];
67
68 1
			$path = $me->make_path($path);
69 1
			$originalPath = $path;
70 1
			while ($path != $prevPath && !$me->exists($path)) {
0 ignored issues
show
Bug introduced by
The variable $prevPath 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...
71
				$prevPath = $path;
72
				$path = $me->make_path($path.'../');
73
			}
74 1
			if ($path != $originalPath) {
75
				$shortcut = current($me->get($path, 'system.get.phtml'));
76
				if (!$shortcut || !$shortcut->AR_implements('pshortcut')) {
77
					$result = $originalPath;
78
				} else {
79
					if (!is_array($redirects)) {
80
						$redirects = Array();
81
					}
82
					$subpath = substr($originalPath, strlen($path));
83
					$target = $shortcut->call('system.get.target.phtml');
84
					array_push($redirects, Array("src" => $path, "dest" => $target, "keepurl" => $shortcut->data->keepurl));
85
					if ($me->exists($target.$subpath)) {
86
						$result = $target.$subpath;
87
					} else {
88
						$result = self::_make_real_path($target.$subpath, $redirects);
89
					}
90
				}
91
			} else {
92 1
				$result = $path;
93
			}
94 1
			return $result;
95
		}
96
97
	}
98