scan()   B
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 22
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 35
rs 7.3166

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
<?
0 ignored issues
show
Security Best Practice introduced by
It is not recommended to use PHP's short opening tag <?, better use <?php, or <?= in case of outputting.

Short opening tags are disabled in PHP?s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.

As a precaution to avoid these problems better use the long opening tag <?php.

Loading history...
2
3
die('Run this script to batch-compress the current Xinha snapshot. To run the script, open the file and comment out the die() command');
4
$repository_url = 'http://svn.xinha.org/trunk';
5
$version        = '';
6
$date           = date('r');
7
error_reporting(E_ALL);
8
ini_set('show_errors', 1);
9
10
$return = [];
11
function scan($dir, $durl = '', $min_size = '0')
12
{
13
    static $seen = [];
14
    global $return;
15
    $files = [];
16
17
    $dir = realpath($dir);
18
    if (isset($seen[$dir])) {
19
        return $files;
20
    }
21
    $seen[$dir] = true;
22
    $dh         = @opendir($dir);
23
24
    while ($dh && ($file = readdir($dh))) {
25
        if ('.' !== $file && '..' !== $file) {
26
            $path = realpath($dir . '/' . $file);
27
            $url  = $durl . '/' . $file;
28
29
            if (preg_match("/\.svn|lang/", $path)) {
30
                continue;
31
            }
32
33
            if (is_dir($path)) {
34
                scan($path);
35
            } elseif (is_file($path)) {
36
                if (!preg_match("/\.js$/", $path) || filesize($path) < $min_size) {
37
                    continue;
38
                }
39
                $return[] = $path;
40
            }
41
        }
42
    }
43
    @closedir($dh);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for closedir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

43
    /** @scrutinizer ignore-unhandled */ @closedir($dh);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
It seems like $dh can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
    @closedir(/** @scrutinizer ignore-type */ $dh);
Loading history...
Bug introduced by
Are you sure the usage of closedir($dh) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
45
    return $files;
46
}
47
48
scan('../');
49
$cwd = getcwd();
50
51
$root_dir = realpath($cwd . '/..');
52
53
print 'Processing ' . count($return) . ' files<br />';
54
55
$prefix = '/* This compressed file is part of Xinha. For uncompressed sources, forum, and bug reports, go to xinha.org */';
56
if ($version) {
57
    $prefix .= "\n/* This file is part of version $version released $date */";
58
}
59
60
$core_prefix = '
61
  /*--------------------------------------------------------------------------
62
    --  Xinha (is not htmlArea) - http://xinha.org
63
    --
64
    --  Use of Xinha is granted by the terms of the htmlArea License (based on
65
    --  BSD license)  please read license.txt in this package for details.
66
    --
67
    --  Copyright (c) 2005-2008 Xinha Developer Team and contributors
68
    --  
69
    --  Xinha was originally based on work by Mihai Bazon which is:
70
    --      Copyright (c) 2003-2004 dynarch.com.
71
    --      Copyright (c) 2002-2003 interactivetools.com, inc.
72
    --      This copyright notice MUST stay intact for use.
73
    -------------------------------------------------------------------------*/
74
';
75
foreach ($return as $file) {
76
    set_time_limit(60);
77
    print "Processed $file<br />";
78
    flush();
79
    $file_url = $repository_url . str_replace($root_dir, '', $file);
80
81
    copy($file, $file . '_uncompr.js');
82
83
    $file_prefix = $prefix . "\n/* The URL of the most recent version of this file is $file_url */";
84
85
    exec('echo "' . (preg_match('/XinhaCore.js$/', $file) ? $file_prefix . $core_prefix : $file_prefix) . "\" > $file && java -jar ${cwd}/dojo_js_compressor.jar -c ${file}_uncompr.js >> $file 2>&1");
86
    if (preg_match('/js: ".*?", line \d+:/', file_get_contents($file)) || preg_match('/sh: java: command not found/', file_get_contents($file))) {
87
        unlink($file);
88
        rename($file . '_uncompr.js', $file);
89
    } else {
90
        unlink($file . '_uncompr.js');
91
    }
92
}
93
print "Operation complete."
94
?>
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...
95
96
97