Passed
Branch master (72fde7)
by Observer
01:31
created

Builder::getReferences()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 28
c 0
b 0
f 0
rs 7.6666
nc 18
nop 2
cc 10

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
2
3
namespace VoidBuilder;
4
5
use VoidEngine\VoidEngine;
6
7
class Builder
8
{
9
    public $appDir;
10
11
    public function __construct (string $appDir)
12
    {
13
        if (!is_dir ($appDir))
14
            throw new \Exception ('Wrong $appDir param');
15
16
        $this->appDir = $appDir;
17
    }
18
19
    public function build (string $outputDir, string $iconPath = null, bool $union = true): array
20
    {
21
        \VoidEngine\dir_clean ($outputDir .'/build');
22
        \VoidEngine\dir_copy (\VoidEngine\CORE_DIR, $outputDir .'/build');
23
24
        unlink ($outputDir .'/build/script.php');
25
        unlink ($outputDir .'/build/VoidCore.exe');
26
27
        return VoidEngine::compile ($outputDir .'/build/app.exe', \VoidEngine\text ($iconPath ?? dirname (__DIR__) .'/system/Icon.ico'), \VoidEngine\str_replace_assoc (file_get_contents (dirname (__DIR__) .'/system/preset.php'), [
0 ignored issues
show
Bug introduced by
The function text 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

27
        return VoidEngine::compile ($outputDir .'/build/app.exe', /** @scrutinizer ignore-call */ \VoidEngine\text ($iconPath ?? dirname (__DIR__) .'/system/Icon.ico'), \VoidEngine\str_replace_assoc (file_get_contents (dirname (__DIR__) .'/system/preset.php'), [
Loading history...
28
            '%VoidEngine%' => self::generateCode (self::getReferences (\VoidEngine\ENGINE_DIR .'/VoidEngine.php')),
29
            '%APP%'        => base64_encode (gzdeflate (serialize ($union ? array_merge (
30
                self::getFiles ($this->appDir),
31
                self::getFiles (dirname ($this->appDir) .'/qero-packages', 'qero-packages/KRypt0nn/VoidFramework')
32
            ) : []), 9))
33
        ]), null, null, null, null, null, '', '');
34
    }
35
36
    public static function generateCode (array $references, bool $removeNamespaces = true): string
37
    {
38
        $code = "/*\n\n\t". join ("\n\t", explode ("\n", file_get_contents (dirname (\VoidEngine\ENGINE_DIR) .'/license.txt'))) ."\n\n*/\n\n";
39
40
        foreach ($references as $path)
41
            $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

41
            $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

41
            $code .= join (/** @scrutinizer ignore-type */ array_slice (array_map (function ($line)
Loading history...
42
            {
43
                return substr ($line, 0, 7) != 'require' ? $line : '';
44
            }, 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

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

87
        foreach (array_slice (/** @scrutinizer ignore-type */ scandir ($path), 2) as $name)
Loading history...
88
            if ($prefixBlacklist === null || substr ($path .'/'. $name, $originalPathLength, $len) != $prefixBlacklist)
89
            {
90
                if (is_dir ($file = $path .'/'. $name))
91
                    $files = self::getFiles ($file, $prefixBlacklist, $files, $originalPathLength);
92
93
                else $files[substr ($file, $originalPathLength)] = file_get_contents ($file);
94
            }
95
96
        return $files;
97
    }
98
}
99