Factory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 11
eloc 18
c 4
b 1
f 0
dl 0
loc 46
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isVersion() 0 3 2
A before() 0 22 4
A isHelp() 0 3 2
A isGlobalHelp() 0 8 3
1
<?php
2
3
/*
4
 * This file is part of the PHALCON-EXT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace PhalconExt\Cli\Middleware;
13
14
use Ahc\Cli\Input\Command;
15
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...
16
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...
17
use PhalconExt\Di\ProvidesDi;
18
19
/**
20
 * Factory middleware that injects Command and responds to `--help` or `--version`.
21
 */
22
class Factory
23
{
24
    use ProvidesDi;
25
26
    public function before(Console $console)
27
    {
28
        $rawArgv = $console->argv($raw = true);
29
        $argv    = $console->argv($raw = false);
30
        $app     = $console->app();
31
        $command = $app->commandFor($argv);
32
33
        if ($this->isVersion($rawArgv)) {
34
            return $command->showVersion();
35
        }
36
37
        if ($this->isGlobalHelp($rawArgv)) {
38
            return $app->showHelp();
39
        }
40
41
        if ($this->isHelp($rawArgv)) {
42
            return $command->showHelp();
43
        }
44
45
        $this->di()->setShared('command', $app->parse($argv));
46
47
        return true;
48
    }
49
50
    protected function isGlobalHelp(array $argv): bool
51
    {
52
        // For a specific help, it would be [cmd, task, action, --help]
53
        // If it is just [cmd, --help] then we deduce it is global help!
54
55
        $isGlobal = ($argv[1][0] ?? '-') === '-' && ($argv[2][0] ?? '-') === '-';
56
57
        return $isGlobal && $this->isHelp($argv);
58
    }
59
60
    protected function isHelp(array $argv): bool
61
    {
62
        return \in_array('--help', $argv) || \in_array('-h', $argv);
63
    }
64
65
    protected function isVersion(array $argv): bool
66
    {
67
        return \in_array('--version', $argv) || \in_array('-V', $argv);
68
    }
69
}
70