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
|
|
View Code Duplication |
protected static function cachedMethodExists($cache) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$cache = (array)$cache; |
23
|
|
|
|
24
|
|
|
$className = $cache['class']; |
25
|
|
|
$methodName = $cache['method_name']; |
26
|
|
|
|
27
|
|
|
return method_exists($className, $methodName); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function isValidSerializedData($cache) |
31
|
|
|
{ |
32
|
|
|
return |
33
|
|
|
isset($cache['schema']) && |
34
|
|
|
isset($cache['method_name']) && |
35
|
|
|
isset($cache['mtime']) && |
36
|
|
|
($cache['schema'] > 0) && |
37
|
|
|
($cache['schema'] <= CommandInfo::SERIALIZATION_SCHEMA_VERSION) && |
38
|
|
|
self::cachedMethodExists($cache); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function constructFromCache(CommandInfo $commandInfo, $info_array) |
42
|
|
|
{ |
43
|
|
|
$info_array += $this->defaultSerializationData(); |
44
|
|
|
|
45
|
|
|
$commandInfo |
46
|
|
|
->setName($info_array['name']) |
47
|
|
|
->replaceRawAnnotations($info_array['annotations']) |
48
|
|
|
->setAliases($info_array['aliases']) |
49
|
|
|
->setHelp($info_array['help']) |
50
|
|
|
->setDescription($info_array['description']) |
51
|
|
|
->replaceExampleUsages($info_array['example_usages']) |
52
|
|
|
->setReturnType($info_array['return_type']) |
53
|
|
|
; |
54
|
|
|
|
55
|
|
|
$this->constructDefaultsWithDescriptions($commandInfo->arguments(), (array)$info_array['arguments']); |
56
|
|
|
$this->constructDefaultsWithDescriptions($commandInfo->options(), (array)$info_array['options']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function constructDefaultsWithDescriptions(DefaultsWithDescriptions $defaults, $data) |
60
|
|
|
{ |
61
|
|
|
foreach ($data as $key => $info) { |
62
|
|
|
$info = (array)$info; |
63
|
|
|
$defaults->add($key, $info['description']); |
64
|
|
|
if (array_key_exists('default', $info)) { |
65
|
|
|
$defaults->setDefaultValue($key, $info['default']); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Default data. Everything should be provided during serialization; |
73
|
|
|
* this is just as a fallback for unusual circumstances. |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function defaultSerializationData() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
'description' => '', |
80
|
|
|
'help' => '', |
81
|
|
|
'aliases' => [], |
82
|
|
|
'annotations' => [], |
83
|
|
|
'example_usages' => [], |
84
|
|
|
'return_type' => [], |
85
|
|
|
'parameters' => [], |
86
|
|
|
'arguments' => [], |
87
|
|
|
'options' => [], |
88
|
|
|
'mtime' => 0, |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.