Completed
Push — master ( 829e7b...9495f2 )
by Nate
01:24
created

OrganizationsValidator::validateAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\validators;
10
11
use craft\helpers\Json;
12
use flipbox\organizations\behaviors\OrganizationsAssociatedToUserBehavior;
13
use flipbox\organizations\Organizations as OrganizationPlugin;
14
use yii\base\Model;
15
use yii\validators\Validator;
16
17
/**
18
 * Validates all associated
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class OrganizationsValidator extends Validator
23
{
24
    const DEFAULT_MESSAGE = 'Invalid organizations.';
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function init()
30
    {
31
        parent::init();
32
33
        if ($this->message === null) {
34
            $this->message = OrganizationPlugin::t(static::DEFAULT_MESSAGE);
35
        }
36
    }
37
38
    /**
39
     * Validates a single attribute.
40
     * Child classes must implement this method to provide the actual validation logic.
41
     * @param Model|OrganizationsAssociatedToUserBehavior $model the data model to be validated
42
     * @param string $attribute the name of the attribute to be validated.
43
     */
44
    public function validateAttribute($model, $attribute)
45
    {
46
        if (!$model->getOrganizationManager()->isMutated()) {
0 ignored issues
show
Bug introduced by
The method getOrganizationManager does only exist in flipbox\organizations\be...ssociatedToUserBehavior, but not in yii\base\Model.

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...
47
            return;
48
        }
49
50
        $result = $this->validateOrganizations($model->getOrganizations());
0 ignored issues
show
Bug introduced by
The method getOrganizations does only exist in flipbox\organizations\be...ssociatedToUserBehavior, but not in yii\base\Model.

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...
51
        if (!empty($result)) {
52
            $this->addError($model, $attribute, $result[0], $result[1]);
0 ignored issues
show
Bug introduced by
It seems like $model defined by parameter $model on line 44 can also be of type object<flipbox\organizat...sociatedToUserBehavior>; however, yii\validators\Validator::addError() does only seem to accept object<yii\base\Model>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
53
        }
54
    }
55
56
    /**
57
     * @param array $organizations
58
     * @return array|null
59
     */
60
    private function validateOrganizations(array $organizations)
61
    {
62
        $hasError = false;
63
64
        foreach ($organizations as $organization) {
65
            if (null === $organization->id && !$organization->validate()) {
66
                $hasError = true;
67
68
                OrganizationPlugin::warning(
69
                    sprintf(
70
                        "Invalid organization: '%s'",
71
                        Json::encode($organization->getFirstErrors())
72
                    ),
73
                    __METHOD__
74
                );
75
            }
76
        }
77
78
        if ($hasError) {
79
            return [$this->message, []];
80
        }
81
82
        return null;
83
    }
84
}
85