GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SubmitFormAction   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 14

Importance

Changes 0
Metric Value
wmc 22
lcom 0
cbo 14
dl 0
loc 129
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 120 22
1
<?php
2
3
namespace app\actions;
4
5
use Yii;
6
use app\behaviors\spamchecker\SpamCheckerBehavior;
7
use app\models\Form;
8
use app\models\BaseObject;
9
use app\models\ObjectPropertyGroup;
10
use app\models\Property;
11
use app\models\SpamChecker;
12
use app\models\Submission;
13
use app\properties\AbstractModel;
14
use app\properties\HasProperties;
15
use kartik\form\ActiveForm;
16
use yii\base\Action;
17
use yii\helpers\ArrayHelper;
18
use yii\web\NotFoundHttpException;
19
use yii\web\Response;
20
21
class SubmitFormAction extends Action
22
{
23
    /**
24
     * @inheritdoc
25
     * @param int $id
26
     * @return int|mixed
27
     * @throws NotFoundHttpException
28
     */
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();
0 ignored issues
show
Documentation Bug introduced by
The method getAbstractModel does not exist on object<app\models\Form>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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());
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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(
0 ignored issues
show
Bug introduced by
The method attachBehavior does only exist in app\properties\AbstractModel, but not in app\behaviors\spamchecker\SpamCheckerBehavior.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
109
                'spamChecker',
110
                [
111
                    'class' => SpamCheckerBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
112
                    'data' => $data,
113
                ]
114
            );
115
            $haveSpam = $model->isSpam();
0 ignored issues
show
Bug introduced by
The method isSpam does only exist in app\behaviors\spamchecker\SpamCheckerBehavior, but not in app\properties\AbstractModel.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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())) {
0 ignored issues
show
Bug introduced by
The method save does only exist in app\models\Submission, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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()),
0 ignored issues
show
Bug introduced by
The method formName does only exist in app\models\Submission, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Documentation Bug introduced by
The method getPropertyGroups does not exist on object<app\models\Form>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method saveProperties does only exist in app\properties\HasProperties, but not in app\models\Submission.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
146
        }
147
        return $submission->id;
148
    }
149
}
150