Completed
Push — master ( 3dab4c...0f5459 )
by Max
01:20
created

MetaDataHelper   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 148
Duplicated Lines 33.11 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 49
loc 148
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCache() 0 4 1
A setCache() 0 4 1
A classMeta() 0 9 2
A classRules() 7 7 2
A methodsMeta() 14 14 4
A methodsRules() 7 7 2
A fieldsMeta() 14 14 4
A fieldsRules() 7 7 2
A prepareRules() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * Get rules for class
61
     * 
62
     * @param string $className
63
     * @return array
64
     */
65 View Code Duplication
    public function classRules(string $className) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
66
    {
67
        if(!isset($this->cache[$className][__METHOD__])) {
68
            $this->cache[$className][__METHOD__] = $this->prepareRules($this->classMeta($className));
69
        }
70
        return $this->cache[$className][__METHOD__];
71
    }
72
    
73
    /**
74
     * Metadata for methods
75
     * meta at name for future add return type info
76
     * 
77
     * @param string $className
78
     * @return array[]
79
     */
80 View Code Duplication
    public function methodsMeta(string $className) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
81
    {
82
        if(!isset($this->cache[$className][__METHOD__])) {
83
            $this->cache[$className][__METHOD__] = [];
84
            $classReflector = new \ReflectionClass($className);
85
            foreach($classReflector->getMethods(\ReflectionMethod::IS_PUBLIC) as $line) {
86
                if(!$line->isStatic()) {
87
                    $doc = $line->getDocComment();
88
                    $this->cache[$className][__METHOD__][$line->name] = StrHelper::parseDocComment($doc);
89
                }
90
            }
91
        }
92
        return $this->cache[$className][__METHOD__];
93
    }
94
95
    /**
96
     * Get methods rules
97
     * 
98
     * @param string $className
99
     * @return array
100
     */
101 View Code Duplication
    public function methodsRules(string $className) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
102
    {
103
        if(!isset($this->cache[$className][__METHOD__])) {
104
            $this->cache[$className][__METHOD__] = $this->prepareRules($this->methodsMeta($className));
105
        }
106
        return $this->cache[$className][__METHOD__];
107
    }
108
    
109
    /**
110
     * Metadata for fields
111
     * meta at name for future add return type info
112
     * 
113
     * @param string $className
114
     * @return array
115
     */
116 View Code Duplication
    public function fieldsMeta(string $className) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
117
    {
118
        if(!isset($this->cache[$className][__METHOD__])) {
119
            $this->cache[$className][__METHOD__] = [];
120
            $classReflector = new \ReflectionClass($className);
121
            foreach($classReflector->getProperties(\ReflectionProperty::IS_PUBLIC) as $line) {
122
                if(!$line->isStatic()) {
123
                    $doc = $line->getDocComment();
124
                    $this->cache[$className][__METHOD__][$line->name] = StrHelper::parseDocComment($doc);
125
                }
126
            }
127
        }
128
        return $this->cache[$className][__METHOD__];
129
    }
130
131
    /**
132
     * Get fields rules
133
     * 
134
     * @param string $className
135
     * @return array
136
     */
137 View Code Duplication
    public function fieldsRules(string $className) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
138
    {
139
        if(!isset($this->cache[$className][__METHOD__])) {
140
            $this->cache[$className][__METHOD__] = $this->prepareRules($this->fieldsMeta($className));
141
        }
142
        return $this->cache[$className][__METHOD__];
143
    }
144
    
145
    /**
146
     * Prepare rules i.e. array of "meta" parameters group by first word
147
     * 
148
     * @param array $data
149
     * @return array
150
     */
151
    protected function prepareRules(array $data) : array
152
    {
153
        $result = [];
154
        foreach($data as $name=>$lines) {
155
            foreach($lines as $line) {
156
                $data = explode(' ', $line);
157
                $key = array_shift($data); // take first
158
                $result[$name][$key][] = $data;                
159
            }
160
        }
161
        return $result;
162
    }
163
}
164