MutualQueryTrait::opposites()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 9.6666
cc 2
eloc 6
nc 2
nop 3
crap 2.0932
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
 * This trait is used for building query class which contains mutual relation operations.
17
 *
18
 * @version 2.0
19
 * @author vistart <[email protected]>
20
 */
21
trait MutualQueryTrait
22
{
23
24
    /**
25
     * Get the opposite relation.
26
     * @param BaseUserModel|string $user initiator
27
     * @param BaseUserModel|string $other recipient.
28
     * @param Connection $database
29
     * @return 
30
     */
31 8
    public function opposite($user, $other, $database = null)
32
    {
33 8
        $model = $this->noInitModel;
0 ignored issues
show
Bug introduced by
The property noInitModel 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...
34 8
        return $this->andWhere([$model->createdByAttribute => $other, $model->otherGuidAttribute => $user])->one($database);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
35
    }
36
37
    /**
38
     * Get all the opposites.
39
     * @param string $user initator.
40
     * @param array $others all recipients.
41
     * @param Connection $database
42
     * @return array instances.
43
     */
44 8
    public function opposites($user, $others = [], $database = null)
45
    {
46 8
        $model = $this->noInitModel;
47 8
        $query = $this->andWhere([$model->otherGuidAttribute => $user]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
48 8
        if (!empty($others)) {
49
            $query = $query->andWhere([$model->createdByAttribute => array_values($others)]);
50
        }
51 8
        return $query->all($database);
52
    }
53
54
    /**
55
     * Specify initiators.
56
     * @param string|array $users the guid of initiator if string, or guid array
57
     * of initiators if array.
58
     * @return \static $this
59
     */
60 10
    public function initiators($users = [])
61
    {
62 10
        if (empty($users)) {
63
            return $this;
64
        }
65 10
        $model = $this->noInitModel;
66 10
        return $this->andWhere([$model->createdByAttribute => $users]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
67
    }
68
69
    /**
70
     * Specify recipients.
71
     * @param string|array $users the guid of recipient if string, or guid array
72
     * of recipients if array.
73
     * @return \static $this
74
     */
75 14
    public function recipients($users = [])
76
    {
77 14
        if (empty($users)) {
78
            return $this;
79
        }
80 14
        $model = $this->noInitModel;
81 14
        return $this->andWhere([$model->otherGuidAttribute => $users]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
82
    }
83
}
84