Completed
Push — master ( e5ef44...29e0f1 )
by Auke
18s
created

upgrade.templates.php ➔ recompile()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 28
Code Lines 22

Duplication

Lines 3
Ratio 10.71 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 22
nc 6
nop 1
dl 3
loc 28
rs 4.8196
c 2
b 1
f 0

How to fix   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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
	$code		= $store->get_config("code");
3
	$templates = $store->get_config("files")."templates/";
4
   $templateStore = $store->get_filestore("templates");
5
6
	require_once($code."modules/mod_pinp.phtml");
7
8
	function showWithLineNumber($text) {
9
		$textarray = explode("\n",$text);
10
		$i=1;
11
		$result = '';
12
13
		foreach($textarray as $line){
14
			$result .= sprintf("%4d: %s\n",$i++,$line);
15
		}
16
17
		return $result;
18
	}
19
20 View Code Duplication
	function pathToObjectID($path) {
0 ignored issues
show
Duplication introduced by
This function 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...
Best Practice introduced by
The function pathToObjectID() has been defined more than once; this definition is ignored, only the first definition in www/install/upgrade/8.4/upgrade.files.php (L6-18) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
21
		global $templates;
22
		$objectID = 0;
23
		$subpath = substr($path,strlen($templates));
24
		$numbers = explode('/',$subpath);;
25
		while (count($numbers)){
26
			$pathicle = array_pop($numbers);
27
			#print "objectID  == ".$objectID." and pathicle == ".$pathicle."\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
			$objectID = $objectID * 100;
29
			$objectID += (int)$pathicle;
30
		}
31
32
		#print "End Result: ".$objectID."\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
		return $objectID;
34
	}
35
36
	function showCompilerError($compiler, $pinp_template) {
37
		echo "\n------------------\n";
38
		echo "Error in '$pinp_template': ".$compiler->error."\n";
39
		echo "------------------\n\n\n";
40
		echo showWithLineNumber($compiler->scanner->YYBUFFER)."\n\n\n";
41
		echo "------------------\n";
42
		echo "in_pinp:           ".$compiler->in_pinp."\n";
43
		echo "token_ahead:       ".$compiler->token_ahead."\n";
44
		echo "token_ahead_value: ".$compiler->token_ahead_value."\n";
45
		echo "token:             ".$compiler->token."\n";
46
		echo "token_value:       ".$compiler->token_value."\n";
47
		echo "YYLINE:            ".$compiler->scanner->YYLINE."\n";
48
		echo "YYCURSOR:          ".$compiler->scanner->YYCURSOR."\n";
49
		echo "YYSTATE:           ".$compiler->scanner->YYSTATE."\n";
50
		echo "------------------\n\n";
51
	}
52
53
54
	function recompile($path) {
55
		global $AR,$templateStore;
56
		$dh = opendir($path);
57
		$objectID = pathToObjectID($path);
58
		while ( false !== ($file = readdir($dh))) {
59
			if ($file != "." && $file != "..") {
60
				$f = $path.$file;
61
				if (substr($file, -strlen(".pinp")) == ".pinp" && is_file($f)) {
62
					$templateName =  substr($file, 1, -strlen(".pinp"));
63
					echo "Recompiling $templateName<br>\n";
64
					$pinp_code = file_get_contents($f);
65
66
					$compiler = new pinp($AR->PINP_Functions, "local->", "\$AR_this->_");
67
					$optimized = sprintf($AR->PINPtemplate, $compiled);
0 ignored issues
show
Bug introduced by
The variable $compiled 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...
Unused Code introduced by
$optimized 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...
68
					$compiled = $compiler->compile(strtr($pinp_code, "\r", ""));
69
					if ($compiler->error) {
70
						showCompilerError($compiler, $path.$file);
71
					} else {
72
						$optimized = sprintf($AR->PINPtemplate, $compiled);
73
						$templateStore->write($optimized, $objectID, $templateName.".inc");
74
					}
75 View Code Duplication
				} else if (is_dir($f) && $file != "CVS" && $file != ".svn") {
76
					recompile("$f/");
77
				}
78
			}
79
		}
80
		closedir($dh);
81
	}
82
83
	recompile($templates);
84
85
	echo "Done with recompiling all PINP templates.<br>\n";
86
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
87