Builder::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
c 0
b 0
f 0
rs 10
nc 2
nop 1
cc 2
1
<?php
2
3
namespace VoidBuilder;
4
5
if (!defined ('VoidBuilder\ENGINE_DIR') && defined ('VoidEngine\ENGINE_DIR'))
6
    define ('VoidBuilder\ENGINE_DIR', \VoidEngine\ENGINE_DIR);
7
8
class Builder
9
{
10
    public $appDir;
11
12
    public function __construct (string $appDir)
13
    {
14
        if (!is_dir ($appDir))
15
            throw new \Exception ('Wrong $appDir param');
16
17
        $this->appDir = $appDir;
18
    }
19
20
    public function build (string $outputDir, string $iconPath = null, bool $union = true): array
21
    {
22
        \VoidEngine\dir_clean ($outputDir .'/build');
0 ignored issues
show
Bug introduced by
The function dir_clean was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

22
        /** @scrutinizer ignore-call */ 
23
        \VoidEngine\dir_clean ($outputDir .'/build');
Loading history...
23
        \VoidEngine\dir_copy (CORE_DIR, $outputDir .'/build');
0 ignored issues
show
Bug introduced by
The function dir_copy was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

23
        /** @scrutinizer ignore-call */ 
24
        \VoidEngine\dir_copy (CORE_DIR, $outputDir .'/build');
Loading history...
24
25
        unlink ($outputDir .'/build/script.php');
26
        unlink ($outputDir .'/build/VoidCore.exe');
27
        
28
        \VoidEngine\dir_clean ($outputDir .'/build/qero-packages');
29
        \VoidEngine\dir_copy (dirname ($this->appDir) .'/qero-packages', $outputDir .'/build/qero-packages');
30
        \VoidEngine\dir_delete ($outputDir .'/build/qero-packages/winforms-php/VoidFramework');
0 ignored issues
show
Bug introduced by
The function dir_delete was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

30
        /** @scrutinizer ignore-call */ 
31
        \VoidEngine\dir_delete ($outputDir .'/build/qero-packages/winforms-php/VoidFramework');
Loading history...
31
32
        return \VoidEngine\EngineAdditions::compile ($outputDir .'/build/app.exe', $iconPath ?? dirname (__DIR__) .'/system/Icon.ico', self::optimizeCode (\VoidEngine\str_replace_assoc (file_get_contents (dirname (__DIR__) .'/system/preset.php'), [
0 ignored issues
show
Bug introduced by
The type VoidEngine\EngineAdditions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The function str_replace_assoc was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

32
        return \VoidEngine\EngineAdditions::compile ($outputDir .'/build/app.exe', $iconPath ?? dirname (__DIR__) .'/system/Icon.ico', self::optimizeCode (/** @scrutinizer ignore-call */ \VoidEngine\str_replace_assoc (file_get_contents (dirname (__DIR__) .'/system/preset.php'), [
Loading history...
33
            '%VoidEngine%' => self::generateCode (self::getReferences (ENGINE_DIR .'/VoidEngine.php')),
34
            '%APP%'        => base64_encode (gzdeflate (serialize ($union ? self::getFiles ($this->appDir) : []), 9))
35
        ]))/*, null, null, null, null, null, '', '', null, null*/);
36
    }
37
38
    public static function generateCode (array $references, bool $removeNamespaces = true): string
39
    {
40
        $code = "/*\n\n\t". join ("\n\t", explode ("\n", file_get_contents (dirname (ENGINE_DIR) .'/license.txt'))) ."\n\n*/\n\n";
41
42
        foreach ($references as $path)
43
            $code .= join (array_slice (array_map (function ($line)
0 ignored issues
show
Bug introduced by
The call to join() has too few arguments starting with pieces. ( Ignorable by Annotation )

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

43
            $code .= /** @scrutinizer ignore-call */ join (array_slice (array_map (function ($line)

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
array_slice(array_map(fu... */ }, file($path)), 1) of type array is incompatible with the type string expected by parameter $glue of join(). ( Ignorable by Annotation )

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

43
            $code .= join (/** @scrutinizer ignore-type */ array_slice (array_map (function ($line)
Loading history...
44
            {
45
                return substr ($line, 0, 7) != 'require' ? $line : '';
46
            }, file ($path)), 1));
0 ignored issues
show
Bug introduced by
It seems like file($path) can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, 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

46
            }, /** @scrutinizer ignore-type */ file ($path)), 1));
Loading history...
47
48
        return $removeNamespaces ?
49
            preg_replace ('/'. "\n" .'namespace [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*;'. "\n" .'/', "\n\n", $code) : $code;
50
    }
51
52
    public static function getReferences (string $file, bool $parseExtensions = true): array
53
    {
54
        $references = [];
55
56
        foreach (file ($file) as $line)
57
            if (substr ($line, 0, 7) == 'require')
58
                try
59
                {
60
                    $begin = strpos ($line, "'");
61
                    $end   = strrpos ($line, "'") - $begin + 1;
62
63
                    $references = array_merge ($references, self::getReferences (dirname ($file) .'/'. eval ('namespace VoidEngine; return '. substr ($line, $begin, $end) .';'), false));
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
64
                }
65
66
                catch (\Throwable $e)
67
                {
68
                    continue;
69
                }
70
71
        if ($parseExtensions)
72
            if (is_dir (ENGINE_DIR .'/extensions') && is_array ($exts = scandir (ENGINE_DIR .'/extensions')))
73
                foreach ($exts as $ext)
74
                    if (is_dir (ENGINE_DIR .'/extensions/'. $ext) && file_exists ($ext = ENGINE_DIR .'/extensions/'. $ext .'/main.php'))
75
                        $references = array_merge ($references, self::getReferences ($ext, false));
76
77
        $references[] = $file;
78
79
        return $references;
80
    }
81
82
    public static function getFiles (string $path, string $prefixBlacklist = null, array $files = [], int $originalPathLength = -1): array
83
    {
84
        if ($originalPathLength == -1)
85
            $originalPathLength = strlen (dirname ($path)) + 1;
86
87
        $len = strlen ($prefixBlacklist);
88
        
89
        foreach (array_slice (scandir ($path), 2) as $name)
0 ignored issues
show
Bug introduced by
It seems like scandir($path) can also be of type false; however, parameter $array of array_slice() does only seem to accept array, 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

89
        foreach (array_slice (/** @scrutinizer ignore-type */ scandir ($path), 2) as $name)
Loading history...
90
            if ($prefixBlacklist === null || substr ($path .'/'. $name, $originalPathLength, $len) != $prefixBlacklist)
91
            {
92
                if (is_dir ($file = $path .'/'. $name))
93
                    $files = self::getFiles ($file, $prefixBlacklist, $files, $originalPathLength);
94
95
                else $files[substr ($file, $originalPathLength)] = file_get_contents ($file);
96
            }
97
98
        return $files;
99
    }
100
101
    public static function optimizeCode (string $code): string
102
    {
103
        $tokens = token_get_all ('<?php '. $code);
104
        $return = '';
105
106
        foreach ($tokens as $id => $token)
107
            if (is_string ($token))
108
                $return .= $token;
109
110
            else
111
            {
112
                list ($token_id, $text) = $token;
113
114
                switch ($token_id)
115
                {
116
                    case T_COMMENT:
117
                    case T_DOC_COMMENT:
118
                        break;
119
120
                    case T_WHITESPACE:
121
                        if (!isset ($tokens[$id + 1]) || !is_array ($tokens[$id + 1]) || $tokens[$id + 1][0] != T_WHITESPACE)
122
                            $return .= ' ';
123
124
                        break;
125
126
                    default:
127
                        $return .= $text;
128
129
                        break;
130
                }
131
            }
132
133
        return substr ($return, 6);
134
    }
135
}
136