Passed
Pull Request — master (#32)
by Jitendra
01:45
created

Factory::isVersion()   A

Complexity

Conditions 2
Paths 2

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
cc 2
nc 2
nop 1
1
<?php
2
3
namespace PhalconExt\Cli\Middleware;
4
5
use Ahc\Cli\Input\Command;
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 Command and responds 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
        $writer  = $this->di('interactor')->writer();
24
25
        if ($this->isVersion($rawArgv)) {
26
            return $command->showVersion($writer);
27
        }
28
29
        if ($this->isGlobalHelp($rawArgv)) {
30
            return $app->showHelp($writer);
31
        }
32
33
        if ($this->isHelp($rawArgv)) {
34
            return $command->showHelp($writer);
35
        }
36
37
        $this->di()->setShared('command', $app->parse($argv));
38
39
        return true;
40
    }
41
42
    protected function isGlobalHelp(array $argv): bool
43
    {
44
        // For a specific help, it would be [cmd, task, action, --help]
45
        // If it is just [cmd, --help] then we deduce it is global help!
46
47
        $isGlobal = ($argv[1][0] ?? '-') === '-' && ($argv[2][0] ?? '-') === '-';
48
49
        return $isGlobal && $this->isHelp($argv);
50
    }
51
52
    protected function isHelp(array $argv): bool
53
    {
54
        return \in_array('--help', $argv) || \in_array('-h', $argv);
55
    }
56
57
    protected function isVersion(array $argv): bool
58
    {
59
        return \in_array('--version', $argv) || \in_array('-V', $argv);
60
    }
61
}
62