Passed
Push — master ( 055768...84c376 )
by Ioannes
01:40
created

Application::__construct()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 37
rs 8.0555
cc 9
nc 6
nop 2
1
<?php
2
namespace App\BxConsole;
3
4
use Bitrix\Main\Loader;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Loader 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...
5
use Bitrix\Main\ModuleManager;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\ModuleManager 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...
6
use Composer\Autoload\ClassLoader;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class Application extends \Symfony\Component\Console\Application {
12
13
    const VERSION = '1.0.0';
14
15
    private $isBitrixLoaded;
16
    
17
    private $bitrixCommands = [];
0 ignored issues
show
introduced by
The private property $bitrixCommands is not used, and could be removed.
Loading history...
18
19
    public function __construct($name = 'BxConsole', $version = self::VERSION)
20
    {
21
        parent::__construct($name, $version);
22
23
        EnvHelper::loadEnv();
24
        EnvHelper::getDocumentRoot();
25
26
        $loader = new \App\BxConsole\Bitrix\Loader();
27
28
        $this->isBitrixLoaded = $loader->initializeBitrix();
29
30
        if($this->isBitrixLoaded) {
31
            foreach($loader->getModulesCommands() as $command) {
32
                $this->add($command);
33
            }
34
        }
35
36
        $pas4CliNamespace = EnvHelper::getPsr4CliNamespace();
37
        $loaders = ClassLoader::getRegisteredLoaders();
0 ignored issues
show
Bug introduced by
The method getRegisteredLoaders() does not exist on Composer\Autoload\ClassLoader. ( 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 */ 
38
        $loaders = ClassLoader::getRegisteredLoaders();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
        if(!empty($loaders)) {
39
            $loader = current($loaders);
40
            if($loader instanceof ClassLoader) {
41
                $map = $loader->getClassMap();
42
                foreach($map as $class => $path) {
43
                    if(strpos($class, $pas4CliNamespace) === 0) {
44
                        try {
45
                            $obj = new $class();
46
                            if($obj instanceof Command) {
47
                                $this->add($obj);
48
                            }
49
                        } catch (\Exception $e) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
50
                    }
51
                }
52
            }
53
        }
54
55
        $this->add(new Cron());
56
    }
57
58
    public function doRun(InputInterface $input, OutputInterface $output) {
59
60
        $exitCode = parent::doRun($input, $output);
61
62
        if($this->isBitrixLoaded) {
63
            if ($this->getCommandName($input) === null) {
64
                $output->writeln(PHP_EOL . sprintf('Using Bitrix <info>kernel v%s</info>.</info>', SM_VERSION),
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\SM_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
                    OutputInterface::VERBOSITY_VERY_VERBOSE);
66
            }
67
68
        } else {
69
            $output->writeln(PHP_EOL . sprintf('<error>No Bitrix kernel found in %s.</error> ' .
70
                    'Please set DOCUMENT_ROOT value in composer.json <info>{"extra":{"document-root":DOCUMENT_ROOT}}</info>', $this->getDocumentRoot()));
71
        }
72
73
        return $exitCode;
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function isBitrixLoaded(): bool
80
    {
81
        return $this->isBitrixLoaded;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getDocumentRoot() {
88
89
        return (EnvHelper::getDocumentRoot() ?: '');
90
    }
91
}