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

src/validators/OrganizationsValidator.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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]);
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