Passed
Push — master ( 72f7b3...4061f4 )
by Observer
02:00
created

VoidStudioBuilder::optimizeCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
nc 1
nop 1
cc 1
1
<?php
2
3
namespace VoidEngine;
4
5
use VoidBuilder\Builder;
6
7
class VoidStudioBuilder
8
{
9
    public static function compileProject (string $save, string $enteringPoint, array $references, array $settings = [], bool $printSuccessCompile = false, bool $debug = false): array
10
    {
11
        $savePath    = dirname ($save) .'/'. basenameNoExt ($save);
0 ignored issues
show
Bug introduced by
The function basenameNoExt 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

11
        $savePath    = dirname ($save) .'/'. /** @scrutinizer ignore-call */ basenameNoExt ($save);
Loading history...
12
        $globalCode  = file_get_contents (APP_DIR .'/system/presets/compile_parser_preset.cs') ."\n\n";
13
        $objectsCode = '';
14
        $forms       = [];
15
16
        for ($i = 0; $i < 5; ++$i)
17
            if (!isset ($settings[$i]) || !strlen (trim ($settings[$i])))
18
                $settings[$i] = null;
19
20
        $settings = array_slice ($settings, 0, 5);
21
22
        foreach (VoidStudioAPI::getObjects ('main')['Designer__FormsList']->items->names as $id => $item)
23
        {
24
            $designer    = VoidStudioAPI::getObjects ('main')['Designer__'. $item .'Designer'];
0 ignored issues
show
Bug introduced by
The type VoidEngine\VoidStudioAPI 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...
25
            $globalCode .= self::appendDesignerData ($designer->getSharpCode ($item), $designer);
26
27
            $forms[] = $item;
28
29
            if (!isset (VoidStudioAPI::$events[$item]))
30
                VoidStudioAPI::$events[$item] = "class $item extends Form\n{}\n";
31
32
            if (isset (VoidStudioAPI::$events[$item]))
33
                $objectsCode .= ClassWorker::applyClass (VoidStudioAPI::$events[$item], $item, substr ($voidCode = $designer->getVoidCode ($item, false), $pos = strpos ($voidCode, '{') + 1, strrpos ($voidCode, '}') - $pos - 1) ."\n") ."\n\n";
34
        }
35
36
        dir_clean ($savePath);
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

36
        /** @scrutinizer ignore-call */ dir_clean ($savePath);
Loading history...
37
        dir_copy (CORE_DIR, $savePath);
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

37
        /** @scrutinizer ignore-call */ dir_copy (CORE_DIR, $savePath);
Loading history...
38
        
39
        unlink ($savePath .'/script.php');
40
        unlink ($savePath .'/VoidCore.exe');
41
42
        $errors = EngineAdditions::compile ($savePath .'/'. basename ($save), APP_DIR .'/system/icons/Icon.ico', self::optimizeCode (str_replace_assoc (file_get_contents (APP_DIR .'/system/presets/compile_main_preset.php'), [
0 ignored issues
show
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

42
        $errors = EngineAdditions::compile ($savePath .'/'. basename ($save), APP_DIR .'/system/icons/Icon.ico', self::optimizeCode (/** @scrutinizer ignore-call */ str_replace_assoc (file_get_contents (APP_DIR .'/system/presets/compile_main_preset.php'), [
Loading history...
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...
43
            '%VoidEngine%' => self::generateCode ($references),
44
45
            '%modules%' => (file_exists ($modulesFile = VoidStudioProjectManager::$projectPath .'/modules/Qero.json') && sizeof ($modules = json_decode (file_get_contents ($modulesFile))) ? "require 'qero-packages/autoload.php';\n\n" : "\n\n") . implode ("\n", array_map (function ($module)
0 ignored issues
show
Bug introduced by
The type VoidEngine\VoidStudioProjectManager 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...
46
            {
47
                $module = trim (file_get_contents ($module));
48
    
49
                if (substr ($module, 0, 2) == '<?')
50
                    $module = substr ($module, 2);
51
    
52
                if (substr ($module, 0, 3) == 'php')
53
                    $module = substr ($module, 3);
54
    
55
                if (substr ($module, -2) == '?>')
56
                    $module = substr ($module, 0, -2);
57
    
58
                return "\$module = <<<'__MODULE'\n". self::optimizeCode ($module) ."\n__MODULE;\n\neval (\$module);";
59
            }, array_merge (glob (VoidStudioProjectManager::$projectPath .'/modules/*.php'), $debug ? [APP_DIR .'/system/debug/DebugHook.php'] : []))),
0 ignored issues
show
Bug introduced by
It seems like glob(VoidEngine\VoidStud...ath . '/modules/*.php') can also be of type false; however, parameter $array1 of array_merge() 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

59
            }, array_merge (/** @scrutinizer ignore-type */ glob (VoidStudioProjectManager::$projectPath .'/modules/*.php'), $debug ? [APP_DIR .'/system/debug/DebugHook.php'] : []))),
Loading history...
60
61
            '%objects%'        => trim ($objectsCode),
62
            '%forms%'          => join ('\', \'', $forms),
63
            '%entering_point%' => $enteringPoint,
64
            '%author_id%'      => sha1 (shell_exec ('wmic csproduct'))
65
        ])), $settings[0], $settings[1], $settings[2], $settings[3], $settings[4], /*str_replace_assoc (file_get_contents (APP_DIR .'/system/presets/compile_main_preset.cs'), [
66
            '%forms%' => join ('", "', $forms)
67
        ])*/'', $globalCode);
68
69
        if (isset ($modules) && sizeof ($modules) > 0)
70
        {
71
            dir_delete (APP_DIR .'/Qero/qero-packages');
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

71
            /** @scrutinizer ignore-call */ dir_delete (APP_DIR .'/Qero/qero-packages');
Loading history...
72
73
            $manager = new \Qero\PackagesManager\PackagesManager;
0 ignored issues
show
Bug introduced by
The type Qero\PackagesManager\PackagesManager 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...
74
75
            foreach ($modules as $package)
76
                $manager->installPackage ($package);
77
78
            dir_copy (APP_DIR .'/Qero/qero-packages', $savePath .'/qero-packages');
79
            dir_delete (APP_DIR .'/Qero/qero-packages');
80
        }
81
82
        // pre ($errors);
83
        // pre ($globalCode->toString ());
84
85
        $log = VoidStudioAPI::getObjects ('main')['ToolsPanel__LogList'];
86
        $log->items->add ('Проект скомпилирован по пути "'. $save .'". '. (($errorsCount = sizeof ($errors)) > 0 ? ('Обнаружено '. $errorsCount .' ошибок') : 'Ошибок не обнаружено'));
87
88
        if ($errorsCount > 0)
89
        {
90
            $log->items->addRange (array_map (function ($error)
91
            {
92
                return "\t". $error;
93
            }, $errors));
94
95
            messageBox ('Обнаружено '. $errorsCount .' ошибок', 'Ошибка компиляции', enum ('System.Windows.Forms.MessageBoxButtons.OK'), enum ('System.Windows.Forms.MessageBoxIcon.Error'));
0 ignored issues
show
Bug introduced by
The function enum 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

95
            messageBox ('Обнаружено '. $errorsCount .' ошибок', 'Ошибка компиляции', /** @scrutinizer ignore-call */ enum ('System.Windows.Forms.MessageBoxButtons.OK'), enum ('System.Windows.Forms.MessageBoxIcon.Error'));
Loading history...
Bug introduced by
The function messageBox 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

95
            /** @scrutinizer ignore-call */ messageBox ('Обнаружено '. $errorsCount .' ошибок', 'Ошибка компиляции', enum ('System.Windows.Forms.MessageBoxButtons.OK'), enum ('System.Windows.Forms.MessageBoxIcon.Error'));
Loading history...
96
        }
97
98
        elseif ($printSuccessCompile)
99
            messageBox ('Проект успешно скомпилирован', 'Успешная компиляция', enum ('System.Windows.Forms.MessageBoxButtons.OK'), enum ('System.Windows.Forms.MessageBoxIcon.Information'));
100
101
        return $errors;
102
    }
103
104
    public static function appendDesignerData (string $code, VoidDesigner $designer): string
0 ignored issues
show
Bug introduced by
The type VoidEngine\VoidDesigner 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...
105
    {
106
        $offset = 0;
107
108
        while (($pos = strpos ($code, ')(resources.GetObject("', $offset)) !== false)
109
        {
110
            $offset   = $pos + 23;
111
            $property = explode ('.', substr ($code, $offset, ($end = strpos ($code, '")));', $offset)) - $offset));
112
113
            $object = $property[0] == '$this' ?
114
                $designer->form->selector :
115
                $designer->getComponentByName ($property[0]);
116
117
            foreach (array_slice ($property, 1) as $path)
118
                $object = \VoidCore::getProperty ($object, $path);
0 ignored issues
show
Bug introduced by
The type VoidCore 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...
119
120
            $code = str_replace (substr ($code, $pos + 2, $end - $pos), 'WinForms_PHP.ZendProgram.getResource ("'. \VoidCore::exportObject ($object) .'")', $code);
121
        }
122
123
        return $code;
124
    }
125
126
    public static function generateCode (array $references, bool $removeNamespaces = true): string
127
    {
128
        return Builder::generateCode ($references, $removeNamespaces);
129
    }
130
131
    public static function getReferences (string $file, bool $parseExtensions = true): array
132
    {
133
        return Builder::getReferences ($file, $parseExtensions);
134
    }
135
	
136
	public static function optimizeCode (string $code): string
137
	{
138
		return Builder::optimizeCode ($code);
139
	}
140
}
141