Conditions | 22 |
Paths | 69 |
Total Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
29 | public function run($id) |
||
30 | { |
||
31 | /** @var Form|HasProperties $form */ |
||
32 | if (null === $form = Form::findById($id)) { |
||
33 | throw new NotFoundHttpException(); |
||
34 | } |
||
35 | |||
36 | $post = Yii::$app->request->post(); |
||
37 | |||
38 | // удаляем required правило для файлов |
||
39 | $intersectKeys = []; |
||
40 | if (isset($_FILES[$form->abstractModel->formName()]) && isset($post[$form->abstractModel->formName()])) { |
||
41 | $intersectKeys = array_intersect_key( |
||
42 | $post[$form->abstractModel->formName()], |
||
43 | $_FILES[$form->abstractModel->formName()]['name'] |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | if(!empty($intersectKeys)) { |
||
48 | $intersectKeys = array_keys($intersectKeys); |
||
49 | $oldRulesModel = $form->abstractModel->getRules(); |
||
50 | $newRulesModel = []; |
||
51 | foreach($oldRulesModel as $curRule){ |
||
52 | if(!is_array($curRule[1]) |
||
53 | && $curRule[1] == 'required' |
||
54 | && in_array($curRule[0], $intersectKeys)) { |
||
55 | continue; |
||
56 | } |
||
57 | $newRulesModel[] = $curRule; |
||
58 | } |
||
59 | $form->abstractModel->clearRules(); |
||
60 | $form->abstractModel->addRules($newRulesModel); |
||
61 | } |
||
62 | // удаляем required правило для файлов |
||
63 | |||
64 | |||
65 | $form->abstractModel->setAttributesValues($post); |
||
66 | /** @var AbstractModel|SpamCheckerBehavior $model */ |
||
67 | $model = $form->getAbstractModel(); |
||
|
|||
68 | |||
69 | if (Yii::$app->request->isAjax && isset($post['ajax'])) { |
||
70 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
71 | return ActiveForm::validate($model); |
||
72 | } |
||
73 | |||
74 | /** @var \app\models\BaseObject $object */ |
||
75 | $object = BaseObject::getForClass(Form::className()); |
||
76 | $propGroups = ObjectPropertyGroup::find()->where( |
||
77 | [ |
||
78 | 'and', |
||
79 | 'object_id = :object', |
||
80 | 'object_model_id = :id' |
||
81 | ], |
||
82 | [ |
||
83 | ':object' => $object->id, |
||
84 | ':id' => $id |
||
85 | ] |
||
86 | )->asArray()->all(); |
||
87 | $propIds = ArrayHelper::getColumn($propGroups, 'property_group_id'); |
||
88 | |||
89 | // Spam checking |
||
90 | $activeSpamChecker = SpamChecker::getActive(); |
||
91 | $data = []; |
||
92 | $haveSpam = false; |
||
93 | if ($activeSpamChecker !== null && !empty($activeSpamChecker->api_key)) { |
||
94 | $data[$activeSpamChecker->name]['class'] = $activeSpamChecker->behavior; |
||
95 | $data[$activeSpamChecker->name]['value']['key'] = $activeSpamChecker->api_key; |
||
96 | $properties = Property::getForGroupId($propIds[0]); |
||
97 | foreach ($properties as $prop) { |
||
98 | if (!isset($activeSpamChecker->{$prop->interpret_as}) |
||
99 | || empty($activeSpamChecker->{$prop->interpret_as}) |
||
100 | ) { |
||
101 | continue; |
||
102 | } |
||
103 | $data[$activeSpamChecker->name]['value'][$activeSpamChecker->{$prop->interpret_as}] = |
||
104 | is_array($post[$form->abstractModel->formName()][$prop->key]) |
||
105 | ? implode(' ', $post[$form->abstractModel->formName()][$prop->key]) |
||
106 | : $post[$form->abstractModel->formName()][$prop->key]; |
||
107 | } |
||
108 | $model->attachBehavior( |
||
109 | 'spamChecker', |
||
110 | [ |
||
111 | 'class' => SpamCheckerBehavior::className(), |
||
112 | 'data' => $data, |
||
113 | ] |
||
114 | ); |
||
115 | $haveSpam = $model->isSpam(); |
||
116 | } |
||
117 | $date = new \DateTime(); |
||
118 | /** @var Submission|HasProperties $submission */ |
||
119 | $submission = new Submission( |
||
120 | [ |
||
121 | 'form_id' => $form->id, |
||
122 | 'date_received' => $date->format('Y-m-d H:i:s'), |
||
123 | 'ip' => Yii::$app->request->userIP, |
||
124 | 'user_agent' => Yii::$app->request->userAgent, |
||
125 | 'spam' => (int)$haveSpam, |
||
126 | 'submission_referrer' => Yii::$app->request->referrer |
||
127 | ] |
||
128 | ); |
||
129 | if (false === Yii::$app->user->isGuest) { |
||
130 | $submission->processed_by_user_id = Yii::$app->user->identity->getId(); |
||
131 | } |
||
132 | if (!($form->abstractModel->validate() && $submission->save())) { |
||
133 | return "0"; |
||
134 | } |
||
135 | if (isset($post[$form->abstractModel->formName()])) { |
||
136 | $data = [ |
||
137 | HasProperties::FIELD_ADD_PROPERTY_GROUP => [ |
||
138 | $submission->formName() => array_keys($form->getPropertyGroups()), |
||
139 | ], |
||
140 | $submission->abstractModel->formName() => $post[$form->abstractModel->formName()], |
||
141 | ]; |
||
142 | if (isset($_FILES[$form->abstractModel->formName()])) { |
||
143 | $_FILES[$submission->abstractModel->formName()] = $_FILES[$form->abstractModel->formName()]; |
||
144 | } |
||
145 | $submission->saveProperties($data); |
||
146 | } |
||
147 | return $submission->id; |
||
148 | } |
||
149 | } |
||
150 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: