HasWithTrait::getWithPK()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Nip\Records\Relations\Traits;
4
5
use Exception;
6
use Nip\Records\AbstractModels\RecordManager;
7
use Nip\Records\Locator\Exceptions\InvalidModelException;
8
use Nip\Records\Relations\Relation;
9
use Nip\Records\Traits\Relations\HasRelationsRecordsTrait;
10
11
/**
12
 * Trait HasWithTrait
13
 * @package Nip\Records\Relations\Traits
14
 */
15
trait HasWithTrait
16
{
17
    /**
18
     * @var RecordManager
19
     */
20
    protected $with = null;
21
22
    /**
23
     * @var null|string
24
     */
25
    protected $withPK = null;
26
27
    /** @noinspection PhpDocMissingThrowsInspection
28
     * @return RecordManager
29
     */
30 10
    public function getWith()
31
    {
32 10
        if ($this->with == null) {
33
            /** @noinspection PhpUnhandledExceptionInspection */
34 4
            $this->initWith();
35
        }
36
37 10
        return $this->with;
38
    }
39
40
    /**
41
     * @param RecordManager|HasRelationsRecordsTrait $object
42
     * @return $this
43
     */
44 10
    public function setWith($object)
45
    {
46 10
        $this->with = $object;
0 ignored issues
show
Documentation Bug introduced by
It seems like $object can also be of type Nip\Records\Traits\Relat...asRelationsRecordsTrait. However, the property $with is declared as type Nip\Records\AbstractModels\RecordManager. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
48 10
        return $this;
49
    }
50
51
    /**
52
     * @throws Exception
53
     */
54 4
    public function initWith()
55
    {
56 4
        $className = $this->getWithClass();
57 4
        $this->setWithClass($className);
58 4
    }
59
60
    /** @noinspection PhpDocMissingThrowsInspection
61
     * @return string
62
     */
63 3
    public function getWithClass()
64
    {
65
        /** @noinspection PhpUnhandledExceptionInspection */
66 3
        return inflector()->pluralize($this->getName());
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        return inflector()->pluralize($this->/** @scrutinizer ignore-call */ getName());
Loading history...
67
    }
68
69
    /**
70
     * @param $params
71
     */
72 6
    public function checkParamWith($params)
73
    {
74 6
        if (isset($params['with'])) {
75
            $this->setWith($params['with']);
76
            unset($params['with']);
77
        }
78 6
    }
79
80
    /**
81
     * @param $params
82
     */
83 6
    public function checkParamWithPK($params)
84
    {
85 6
        if (isset($params['withPK'])) {
86 3
            $this->setWithPK($params['withPK']);
87 3
            unset($params['withPK']);
88
        }
89 6
    }
90
91
    /** @noinspection PhpDocMissingThrowsInspection
92
     * @param string $name
93
     */
94 4
    public function setWithClass($name)
95
    {
96
        try {
97 4
            $manager = $this->getModelManagerInstance($name);
0 ignored issues
show
Bug introduced by
It seems like getModelManagerInstance() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
            /** @scrutinizer ignore-call */ 
98
            $manager = $this->getModelManagerInstance($name);
Loading history...
98 4
            $this->setWith($manager);
99
        } catch (InvalidModelException $exception) {
100
            /** @noinspection PhpUnhandledExceptionInspection */
101
            throw new Exception(
102
                'Cannot instance records [' . $name . '] in ' . $this->debugString()
0 ignored issues
show
Bug introduced by
It seems like debugString() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
                'Cannot instance records [' . $name . '] in ' . $this->/** @scrutinizer ignore-call */ debugString()
Loading history...
103
                . '|| with message ' . $exception->getMessage()
104
            );
105
        }
106 4
    }
107
108
    /**
109
     * @return string
110
     */
111 3
    public function getWithPK()
112
    {
113 3
        if ($this->withPK === null) {
114 2
            $this->initWithPK();
115
        }
116 3
        return $this->withPK;
117
    }
118
119
    /**
120
     * @param string|null $withPK
121
     */
122 3
    public function setWithPK(string $withPK)
123
    {
124 3
        $this->withPK = $withPK;
125 3
    }
126
127 2
    protected function initWithPK()
128
    {
129 2
        $this->withPK = $this->generateWithPK();
130 2
    }
131
132
    /**
133
     * @return string
134
     */
135 2
    protected function generateWithPK()
136
    {
137 2
        return $this->getWith()->getPrimaryKey();
138
    }
139
}
140