Test Failed
Push — master ( 384f45...5dc28a )
by Php Easy Api
04:09
created

ClientAnnotationManager::getRules()   D

Complexity

Conditions 21
Paths 14

Size

Total Lines 69
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 39
nc 14
nop 1
dl 0
loc 69
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Resta\Client;
4
5
use Resta\Contracts\ApplicationContracts;
6
7
/**
8
 * @property $this inputsKey
9
 * @property $this inputsValue
10
 */
11
class ClientAnnotationManager extends ClientAnnotationAbstract
12
{
13
    /**
14
     * @var array $exceptionParams
15
     */
16
    protected $exceptionParams = [];
17
18
    /**
19
     * @var string $annotation
20
     */
21
    protected $annotation;
22
23
    /**
24
     * RequestAnnotationManager constructor.
25
     * @param ApplicationContracts $app
26
     * @param $request
27
     */
28
    public function __construct(ApplicationContracts $app,$request)
29
    {
30
        parent::__construct($app);
31
32
        $this->setReflection($request);
33
34
        $this->getInputs();
35
    }
36
37
    /**
38
     * check annotation
39
     *
40
     * @param $method
41
     * @param $key
42
     * @return mixed|void
43
     */
44
    public function annotation($method,$key)
45
    {
46
        //set annotation value with getting reflection
47
        $reflection = $this->getReflection('reflection')->reflectionMethodParams($method);
48
        $this->annotation = $reflection->document;
49
50
        //get remove from request object
51
        $this->getRemove($key);
52
53
        //get exception values from request object
54
        $this->getException($key);
55
56
        //get regex from request object
57
        $this->getRegex($key);
58
59
        //get regex from request object
60
        $this->getRules($key);
61
    }
62
63
    /**
64
     * catch exception from regex method
65
     *
66
     * @param string $key
67
     * @param null|string $data
68
     */
69
    private function catchException($key,$data)
70
    {
71
        if(isset($this->exceptionParams[$key])){
72
73
            //get key params for exception params
74
            $keyParams = ($this->exceptionParams[$key]['params']) ?? [];
75
76
            //catch exception
77
            exception($this->exceptionParams[$key]['name'],$keyParams)
78
                ->unexpectedValue($this->exceptionParams[$key]['name'].' input value is not valid as format ('.$data.')');
79
        }
80
        else{
81
            //catch exception
82
            exception()->unexpectedValue($key.' input value is not valid as format ('.$data.')');
83
        }
84
    }
85
86
    /**
87
     * get request exception from annotation
88
     *
89
     * @param $key
90
     */
91
    private function getException($key)
92
    {
93
        if(preg_match('@exception\((.*?)\)|exception\((.*?)\)\r\n@is',$this->annotation,$exception)){
94
95
            $exceptionSpaceExplode = explode(" ",$exception[1]);
96
97
            foreach ($exceptionSpaceExplode as $exceptions){
98
                $exceptionsDotExplode = explode(":",$exceptions);
99
                $this->exceptionParams[$key][$exceptionsDotExplode[0]] = $exceptionsDotExplode[1];
100
            }
101
102
            if(isset($this->exceptionParams[$key]['params'])){
103
104
                $paramsCommaExplode = explode(",",$this->exceptionParams[$key]['params']);
105
                unset($this->exceptionParams[$key]['params']);
106
107
                foreach ($paramsCommaExplode as $params){
108
                    $paramsEqualExplode = explode("=",$params);
109
                    if(isset($paramsEqualExplode[0]) && isset($paramsEqualExplode[1])){
110
                        $this->exceptionParams[$key]['params'][$paramsEqualExplode[0]] = $paramsEqualExplode[1];
111
                    }
112
                }
113
            }
114
        }
115
    }
116
117
    /**
118
     * get regular expression from request object
119
     *
120
     * @param $key
121
     */
122
    private function getRegex($key)
123
    {
124
        // with the method based regex annotation,
125
        // we check the rule definition for our requests.
126
        if(preg_match('@regex\((.*?)\)|regex\((.*?)\)\r\n@is',$this->annotation,$regex)){
127
            if(isset($this->inputs[$key])){
128
129
                // for the definition of rules,
130
                // our inputs can be array and in this case we offer array option for user comfort.
131
                if(is_array($this->inputs[$key])){
132
133
                    foreach ($this->inputs[$key] as $this->inputsKey => $this->inputsValue){
134
135
                        if(is_array($this->inputsValue)){
136
137
                            foreach ($this->inputsValue as $inputsValueKey => $inputsValueItem){
138
                                if(!preg_match('@'.$regex[1].'@is',$inputsValueItem)){
139
                                    $this->catchException($key,$regex[1]);
140
                                }
141
                            }
142
143
                        }
144
                        else{
145
                            if(!preg_match('@'.$regex[1].'@is',$this->inputsValue)){
146
                                $this->catchException($key,$regex[1]);
147
                            }
148
                        }
149
150
                    }
151
                }
152
                else{
153
154
                    // we control the regex rule that evaluates when only string arrives.
155
                    if(!preg_match('@'.$regex[1].'@is',$this->inputs[$key])){
156
                        $this->catchException($key,$regex[1]);
157
                    }
158
                }
159
            }
160
        }
161
    }
162
163
    /**
164
     * get remove regex pattern with request object
165
     *
166
     * @param string $key
167
     * @return void|mixed
168
     */
169
    private function getRemove($key)
170
    {
171
        if(preg_match('@remove\((.*?)\)\r\n@is',$this->annotation,$remove)){
172
            if(isset($this->inputs[$key])){
173
                if(preg_match('@'.$remove[1].'@is',$this->inputs[$key])){
174
                    unset($this->inputs[$key]);
175
                }
176
            }
177
        }
178
    }
179
180
    /**
181
     * check rules from request
182
     *
183
     * @param $key
184
     */
185
    private function getRules($key)
186
    {
187
        if(preg_match('@rule\((.*?)\)|rule\((.*?)\)\r\n@is',$this->annotation,$rule)){
188
189
            $requestRules = $this->getReflection('rules');
190
191
            $rules = explode(":",$rule[1]);
192
            if(isset($this->inputs[$key]) && !is_array($this->inputs[$key])){
193
                foreach($rules as $rule){
194
195
                    $ruleExplode = explode('#',$rule);
196
                    $rule = $ruleExplode[0];
197
198
                    if(isset($requestRules[$rule])){
199
                        if(!preg_match('@'.$requestRules[$rule].'@',$this->inputs[$key])){
200
                            exception($rule,['key'=>$key.':'.$this->inputs[$key]])
201
                                ->invalidArgument($key.':'.$this->inputs[$key].' input value is not valid for '.$rule.' request rule');
202
                        }
203
                    }
204
205
                    //rule method
206
                    if(!isset($requestRules[$rule])){
207
                        $this->request->call(function() use($rule,$key,$ruleExplode){
208
                            if(method_exists($this,$ruleMethod = '__rule'.ucfirst($rule))){
209
                                if(isset($ruleExplode[1])){
210
211
                                    $reValueList = [];
212
                                    foreach (explode(',',$ruleExplode[1]) as $reValue){
213
                                        $reValueExplode = explode('=',$reValue);
214
                                        $reValueListKey = $reValueExplode[0];
215
                                        $reValueListValue = (isset($reValueExplode[1])) ? $reValueExplode[1] : null;
216
217
                                        $reValueList[$reValueListKey] = $reValueListValue;
218
219
                                    }
220
221
                                    $this->{$ruleMethod}($key,$this->inputs[$key],$reValueList);
222
                                }
223
                                else{
224
                                    $this->{$ruleMethod}($key,$this->inputs[$key]);
225
                                }
226
227
                            }
228
                        });
229
                    }
230
                }
231
            }
232
            else{
233
234
                foreach ($this->inputs[$key] as $ikey=>$input){
235
236
                    if(!is_array($input)){
237
                        foreach($rules as $rule){
238
                            if(isset($requestRules[$rule])){
239
                                if(!preg_match('@'.$requestRules[$rule].'@',$input)){
240
                                    exception($rule,['key'=>''.$key.':'.$input])
241
                                        ->invalidArgument($key.':'.$input.' input value is not valid for '.$rule.' request rule');
242
                                }
243
                            }
244
                        }
245
                    }
246
                    else{
247
248
                        foreach ($input as $ikey=>$item){
0 ignored issues
show
Comprehensibility Bug introduced by
$ikey is overwriting a variable from outer foreach loop.
Loading history...
249
                            foreach($rules as $rule){
250
                                if(isset($requestRules[$rule])){
251
                                    if(!preg_match('@'.$requestRules[$rule].'@',$item)){
252
                                        exception($rule,['key'=>''.$key.':'.$item])
253
                                            ->invalidArgument($key.':'.$item.' input value is not valid for '.$rule.' request rule');
254
                                    }
255
                                }
256
                            }
257
                        }
258
259
                    }
260
261
                }
262
            }
263
        }
264
    }
265
}