serializeDefaultsWithDescriptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
namespace Consolidation\AnnotatedCommand\Parser;
3
4
use Symfony\Component\Console\Input\InputOption;
5
use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParser;
6
use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParserFactory;
7
use Consolidation\AnnotatedCommand\AnnotationData;
8
9
/**
10
 * Serialize a CommandInfo object
11
 */
12
class CommandInfoSerializer
13
{
14
    public function serialize(CommandInfo $commandInfo)
15
    {
16
        $allAnnotations = $commandInfo->getAnnotations();
17
        $path = $allAnnotations['_path'];
18
        $className = $allAnnotations['_classname'];
19
20
        // Include the minimum information for command info (including placeholder records)
21
        $info = [
22
            'schema' => CommandInfo::SERIALIZATION_SCHEMA_VERSION,
23
            'class' => $className,
24
            'method_name' => $commandInfo->getMethodName(),
25
            'mtime' => filemtime($path),
26
            'injected_classes' => [],
27
        ];
28
29
        // If this is a valid method / hook, then add more information.
30
        if ($commandInfo->valid()) {
31
            $info += [
32
                'name' => $commandInfo->getName(),
33
                'description' => $commandInfo->getDescription(),
34
                'help' => $commandInfo->getHelp(),
35
                'aliases' => $commandInfo->getAliases(),
36
                'annotations' => $commandInfo->getRawAnnotations()->getArrayCopy(),
37
                'example_usages' => $commandInfo->getExampleUsages(),
38
                'return_type' => $commandInfo->getReturnType(),
39
            ];
40
            $info['arguments'] = $this->serializeDefaultsWithDescriptions($commandInfo->arguments());
41
            $info['options'] = $this->serializeDefaultsWithDescriptions($commandInfo->options());
42
            $info['injected_classes'] = $commandInfo->getInjectedClasses();
43
        }
44
45
        return $info;
46
    }
47
48
    protected function serializeDefaultsWithDescriptions(DefaultsWithDescriptions $defaults)
49
    {
50
        $result = [];
51
        foreach ($defaults->getValues() as $key => $val) {
52
            $result[$key] = [
53
                'description' => $defaults->getDescription($key),
54
            ];
55
            if ($defaults->hasDefault($key)) {
56
                $result[$key]['default'] = $val;
57
            }
58
        }
59
        return $result;
60
    }
61
}
62