CommandInfoDeserializer   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 0
loc 78
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 4 1
A cachedMethodExists() 0 4 1
A isValidSerializedData() 0 10 6
A constructFromCache() 0 18 1
A constructDefaultsWithDescriptions() 0 10 3
A defaultSerializationData() 0 17 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
 * Deserialize a CommandInfo object
11
 */
12
class CommandInfoDeserializer
13
{
14
    // TODO: in a future version, move CommandInfo::deserialize here
15
    public function deserialize($data)
16
    {
17
        return CommandInfo::deserialize((array)$data);
18
    }
19
20
    protected static function cachedMethodExists($cache)
21
    {
22
        return method_exists($cache['class'], $cache['method_name']);
23
    }
24
25
    public static function isValidSerializedData($cache)
26
    {
27
        return
28
            isset($cache['schema']) &&
29
            isset($cache['method_name']) &&
30
            isset($cache['mtime']) &&
31
            ($cache['schema'] > 0) &&
32
            ($cache['schema'] == CommandInfo::SERIALIZATION_SCHEMA_VERSION) &&
33
            self::cachedMethodExists($cache);
34
    }
35
36
    public function constructFromCache(CommandInfo $commandInfo, $info_array)
37
    {
38
        $info_array += $this->defaultSerializationData();
39
40
        $commandInfo
41
            ->setName($info_array['name'])
42
            ->replaceRawAnnotations($info_array['annotations'])
43
            ->setAliases($info_array['aliases'])
44
            ->setHelp($info_array['help'])
45
            ->setDescription($info_array['description'])
46
            ->replaceExampleUsages($info_array['example_usages'])
47
            ->setReturnType($info_array['return_type'])
48
            ->setInjectedClasses($info_array['injected_classes'])
49
            ;
50
51
        $this->constructDefaultsWithDescriptions($commandInfo->arguments(), (array)$info_array['arguments']);
52
        $this->constructDefaultsWithDescriptions($commandInfo->options(), (array)$info_array['options']);
53
    }
54
55
    protected function constructDefaultsWithDescriptions(DefaultsWithDescriptions $defaults, $data)
56
    {
57
        foreach ($data as $key => $info) {
58
            $info = (array)$info;
59
            $defaults->add($key, $info['description']);
60
            if (array_key_exists('default', $info)) {
61
                $defaults->setDefaultValue($key, $info['default']);
62
            }
63
        }
64
    }
65
66
67
    /**
68
     * Default data. Everything should be provided during serialization;
69
     * this is just as a fallback for unusual circumstances.
70
     * @return array
71
     */
72
    protected function defaultSerializationData()
73
    {
74
        return [
75
            'name' => '',
76
            'description' => '',
77
            'help' => '',
78
            'aliases' => [],
79
            'annotations' => [],
80
            'example_usages' => [],
81
            'return_type' => [],
82
            'parameters' => [],
83
            'arguments' => [],
84
            'options' => [],
85
            'injected_classes' => [],
86
            'mtime' => 0,
87
        ];
88
    }
89
}
90