Completed
Push — master ( 53d264...0a50d5 )
by vistart
07:06
created

MutualTrait::getRecipient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license https://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
/**
16
 * Description of MutualTrait
17
 *
18
 * @author vistart <[email protected]>
19
 */
20
trait MutualTrait
21
{
22
23
    public $otherGuidAttribute = 'other_guid';
24
25
    /**
26
     * Get initiator.
27
     * @return \vistart\Models\queries\BaseUserQuery
28
     */
29
    public function getInitiator()
30
    {
31
        return $this->getUser();
0 ignored issues
show
Bug introduced by
It seems like getUser() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
    }
33
34
    /**
35
     * Get recipient.
36
     * @return \vistart\Models\queries\BaseUserQuery
37
     */
38
    public function getRecipient()
39
    {
40
        if (!is_string($this->otherGuidAttribute)) {
41
            return null;
42
        }
43
        $userClass = $this->userClass;
0 ignored issues
show
Bug introduced by
The property userClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
        $model = $userClass::buildNoInitModel();
45
        return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->otherGuidAttribute]);
0 ignored issues
show
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46
    }
47
48
    public function setRecipient($user)
49
    {
50
        if (!is_string($this->otherGuidAttribute)) {
51
            return null;
52
        }
53
        $otherGuidAttribute = $this->otherGuidAttribute;
54
        return $this->$otherGuidAttribute = $user->guid;
55
    }
56
57 8
    public function getMutualRules()
58
    {
59 8
        $rules = [];
60 8
        if (is_string($this->otherGuidAttribute)) {
61
            $rules = [
62 8
                [$this->otherGuidAttribute, 'required'],
63 8
                [$this->otherGuidAttribute, 'string', 'max' => 36],
64 8
            ];
65 8
        }
66 8
        return $rules;
67
    }
68
}
69