Passed
Pull Request — master (#32)
by Jitendra
02:52
created

Factory::before()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 22
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace PhalconExt\Cli\Middleware;
4
5
use Ahc\Cli\Input\Command;
0 ignored issues
show
Bug introduced by
The type Ahc\Cli\Input\Command 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 Phalcon\Cli\Console;
0 ignored issues
show
Bug introduced by
The type Phalcon\Cli\Console 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...
7
use Phalcon\Cli\Task;
0 ignored issues
show
Bug introduced by
The type Phalcon\Cli\Task 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...
8
use PhalconExt\Di\ProvidesDi;
9
10
/**
11
 * Factory middleware that injects Parser instance and responses to `--help` or `--version`.
12
 */
13
class Factory
14
{
15
    use ProvidesDi;
16
17
    public function before(Console $console)
18
    {
19
        $rawArgv = $console->argv($raw = true);
20
        $argv    = $console->argv($raw = false);
21
        $app     = $console->app();
22
        $command = $app->commandFor($argv);
23
24
        if ($this->isVersion($rawArgv)) {
25
            return $command->showVersion();
26
        }
27
28
        if ($this->isGlobalHelp($rawArgv)) {
29
            return $app->showHelp();
30
        }
31
32
        if ($this->isHelp($rawArgv)) {
33
            return $command->showHelp();
34
        }
35
36
        $this->di()->setShared('command', $app->parse($argv));
37
38
        return true;
39
    }
40
41
    protected function isGlobalHelp(array $argv): bool
42
    {
43
        // For a specific help, it would be [cmd, task, action, --help]
44
        // If it is just [cmd, --help] then we deduce it is global help!
45
46
        $isGlobal = ($argv[1][0] ?? '-') === '-' && ($argv[2][0] ?? '-') === '-';
47
48
        return $isGlobal && $this->isHelp($argv);
49
    }
50
51
    protected function isHelp(array $argv): bool
52
    {
53
        return \in_array('--help', $argv) || \in_array('-h', $argv);
54
    }
55
56
    protected function isVersion(array $argv): bool
57
    {
58
        return \in_array('--version', $argv) || \in_array('-V', $argv);
59
    }
60
}
61