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; |
|
|
|
|
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()); |
|
|
|
|
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); |
|
|
|
|
98
|
4 |
|
$this->setWith($manager); |
99
|
|
|
} catch (InvalidModelException $exception) { |
100
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
101
|
|
|
throw new Exception( |
102
|
|
|
'Cannot instance records [' . $name . '] in ' . $this->debugString() |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.