AbstractCommand::parseArgs()   C
last analyzed

Complexity

Conditions 12
Paths 15

Size

Total Lines 62
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 62
ccs 33
cts 33
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 15
nop 1
crap 12

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/interop-config for the canonical source repository
6
 * @copyright Copyright (c) 2017-2020 Sandro Keil
7
 * @license   http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License
8
 */
9
10
namespace Interop\Config\Tool;
11
12
use Interop\Config\RequiresConfig;
13
14
abstract class AbstractCommand
15
{
16
    const COMMAND_DUMP = 'dump';
17
    const COMMAND_ERROR = 'error';
18
    const COMMAND_HELP = 'help';
19
20
    /**
21
     * @var ConsoleHelper
22
     */
23
    protected $helper;
24
25 19
    public function __construct(ConsoleHelper $helper = null)
26
    {
27 19
        $this->helper = $helper ?: new ConsoleHelper();
28 19
    }
29
30 13
    protected function help($resource = STDOUT): void
0 ignored issues
show
Unused Code introduced by
The parameter $resource is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

30
    protected function help(/** @scrutinizer ignore-unused */ $resource = STDOUT): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32 13
        $this->helper->writeErrorMessage(sprintf(static::HELP_TEMPLATE, static::COMMAND_CLI_NAME));
0 ignored issues
show
Bug introduced by
The constant Interop\Config\Tool\Abst...mmand::COMMAND_CLI_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Interop\Config\Tool\AbstractCommand::HELP_TEMPLATE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
33 13
    }
34
35
    /**
36
     * @param string $command
37
     * @param string $configFile File from which config originates, and to
38
     *     which it will be written.
39
     * @param array $config Parsed configuration.
40
     * @param string $class Name of class to reflect.
41
     * @return \stdClass
42
     */
43 6
    protected function createArguments(string $command, string $configFile, array $config, string $class): \stdClass
44
    {
45
        return (object)[
46 6
            'command' => $command,
47 6
            'configFile' => $configFile,
48 6
            'config' => $config,
49 6
            'class' => $class,
50
        ];
51
    }
52
53 9
    protected function createErrorArgument(string $message): \stdClass
54
    {
55
        return (object)[
56 9
            'command' => static::COMMAND_ERROR,
57 9
            'message' => $message,
58
        ];
59
    }
60
61 4
    protected function createHelpArgument(): \stdClass
62
    {
63
        return (object)[
64 4
            'command' => static::COMMAND_HELP,
65
        ];
66
    }
67
68 19
    protected function parseArgs(array $args): \stdClass
69
    {
70 19
        if (!count($args)) {
71 2
            return $this->createHelpArgument();
72
        }
73
74 17
        $arg1 = array_shift($args);
75
76 17
        if (in_array($arg1, ['-h', '--help', 'help'], true)) {
77 2
            return $this->createHelpArgument();
78
        }
79
80 15
        if (!count($args)) {
81 2
            return $this->createErrorArgument('<error>Missing class name</error>');
82
        }
83
84 13
        $class = array_shift($args);
85
86 13
        if (!class_exists($class)) {
87 2
            return $this->createErrorArgument(
88 2
                sprintf('<error>Class "%s" does not exist or could not be autoloaded.</error>', $class)
89
            );
90
        }
91
92 11
        $reflectionClass = new \ReflectionClass($class);
93
94 11
        if (!in_array(RequiresConfig::class, $reflectionClass->getInterfaceNames(), true)) {
95 2
            return $this->createErrorArgument(
96 2
                sprintf('<error>Class "%s" does not implement "%s".</error>', $class, RequiresConfig::class)
97
            );
98
        }
99
100 9
        $configFile = $arg1;
101 9
        switch (file_exists($configFile)) {
102
            case true:
103 6
                $config = require $configFile;
104
105 6
                if ($config instanceof \Iterator) {
106 1
                    $config = iterator_to_array($config);
107 5
                } elseif ($config instanceof \IteratorAggregate) {
108 1
                    $config = iterator_to_array($config->getIterator());
109
                }
110
111 6
                if (!is_array($config)) {
112 1
                    return $this->createErrorArgument(
113 1
                        sprintf('<error>Configuration at path "%s" does not return an array.</error>', $configFile)
114
                    );
115
                }
116
117 5
                break;
118 3
            case false:
119
                // fall-through
120
            default:
121 3
                if ($command = $this->checkFile($configFile)) {
122 2
                    return $command;
123
                }
124
125 1
                $config = [];
126 1
                break;
127
        }
128
129 6
        return $this->createArguments(self::COMMAND_DUMP, $configFile, $config, $class);
130
    }
131
132
    abstract protected function checkFile(string $configFile): ?\stdClass;
133
}
134