1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright (c) 2019 Mendel <[email protected]> |
5
|
|
|
* @license see license.txt |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace drycart\data; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Metadata for some class |
12
|
|
|
* i.e. some data from comments for class, fields, methods |
13
|
|
|
* |
14
|
|
|
* @author mendel |
15
|
|
|
*/ |
16
|
|
|
class MetaDataHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Session cache |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $cache = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get current cache data for store to external cache |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function getCache() : array |
29
|
|
|
{ |
30
|
|
|
return $this->cache; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set cache data from external cache |
35
|
|
|
* @param array $cache |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function setCache(array $cache) : void |
39
|
|
|
{ |
40
|
|
|
$this->cache = $cache; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Metadata for class |
45
|
|
|
* |
46
|
|
|
* @param string $className |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function classMeta(string $className) : array |
50
|
|
|
{ |
51
|
|
|
if(!isset($this->cache[$className][__METHOD__])) { |
52
|
|
|
$classReflector = new \ReflectionClass($className); |
53
|
|
|
$doc = $classReflector->getDocComment(); |
54
|
|
|
$this->cache[$className][__METHOD__] = StrHelper::parseDocComment($doc); |
55
|
|
|
} |
56
|
|
|
return $this->cache[$className][__METHOD__]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Metadata for methods |
61
|
|
|
* meta at name for future add return type info |
62
|
|
|
* |
63
|
|
|
* @param string $className |
64
|
|
|
* @return array[] |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function methodsMeta(string $className) : array |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if(!isset($this->cache[$className][__METHOD__])) { |
69
|
|
|
$this->cache[$className][__METHOD__] = []; |
70
|
|
|
$classReflector = new \ReflectionClass($className); |
71
|
|
|
foreach($classReflector->getMethods(\ReflectionMethod::IS_PUBLIC) as $line) { |
72
|
|
|
if(!$line->isStatic()) { |
73
|
|
|
$doc = $line->getDocComment(); |
74
|
|
|
$this->cache[$className][__METHOD__][$line->name] = StrHelper::parseDocComment($doc); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return $this->cache[$className][__METHOD__]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Metadata for fields |
83
|
|
|
* meta at name for future add return type info |
84
|
|
|
* |
85
|
|
|
* @param string $className |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function fieldsMeta(string $className) : array |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if(!isset($this->cache[$className][__METHOD__])) { |
91
|
|
|
$this->cache[$className][__METHOD__] = []; |
92
|
|
|
$classReflector = new \ReflectionClass($className); |
93
|
|
|
foreach($classReflector->getProperties(\ReflectionProperty::IS_PUBLIC) as $line) { |
94
|
|
|
if(!$line->isStatic()) { |
95
|
|
|
$doc = $line->getDocComment(); |
96
|
|
|
$this->cache[$className][__METHOD__][$line->name] = StrHelper::parseDocComment($doc); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
return $this->cache[$className][__METHOD__]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $data |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function prepareRules(array $data) : array |
108
|
|
|
{ |
109
|
|
|
$result = []; |
110
|
|
|
foreach($data as $name=>$lines) { |
111
|
|
|
foreach($lines as $line) { |
112
|
|
|
$data = explode(' ', $line); |
113
|
|
|
$key = array_shift($data); // take first |
114
|
|
|
$result[$name][$key][] = $data; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.