Completed
Push — master ( eed049...0ceb6c )
by Nate
06:45
created

OrganizationsValidator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 63
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A validateAttribute() 0 11 3
A validateOrganizations() 0 24 5
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 Tightenco\Collect\Support\Collection;
15
use yii\base\Model;
16
use yii\validators\Validator;
17
18
/**
19
 * Validates all associated
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class OrganizationsValidator extends Validator
24
{
25
    const DEFAULT_MESSAGE = 'Invalid organizations.';
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function init()
31
    {
32
        parent::init();
33
34
        if ($this->message === null) {
35
            $this->message = OrganizationPlugin::t(static::DEFAULT_MESSAGE);
36
        }
37
    }
38
39
    /**
40
     * Validates a single attribute.
41
     * Child classes must implement this method to provide the actual validation logic.
42
     * @param Model|OrganizationsAssociatedToUserBehavior $model the data model to be validated
43
     * @param string $attribute the name of the attribute to be validated.
44
     */
45
    public function validateAttribute($model, $attribute)
46
    {
47
        if (!$model->getOrganizations()->isMutated()) {
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...
48
            return;
49
        }
50
51
        $result = $this->validateOrganizations($model->getOrganizations()->getCollection());
52
        if (!empty($result)) {
53
            $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 45 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...
54
        }
55
    }
56
57
    /**
58
     * @param Collection $organizations
59
     * @return array|null
60
     */
61
    private function validateOrganizations(Collection $organizations)
62
    {
63
        $hasError = false;
64
65
        foreach ($organizations as $organization) {
66
            if (null === $organization->id && !$organization->validate()) {
67
                $hasError = true;
68
69
                OrganizationPlugin::warning(
70
                    sprintf(
71
                        "Invalid organization: '%s'",
72
                        Json::encode($organization->getFirstErrors())
73
                    ),
74
                    __METHOD__
75
                );
76
            }
77
        }
78
79
        if ($hasError) {
80
            return [$this->message, []];
81
        }
82
83
        return null;
84
    }
85
}
86