possible assignment of non-compatible types.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace DoctrineModule\Options; |
||
6 | |||
7 | use Doctrine\Persistence\Mapping\ClassMetadata; |
||
8 | use Doctrine\Persistence\ObjectManager; |
||
9 | use Doctrine\Persistence\ObjectRepository; |
||
10 | use Laminas\Authentication\Adapter\Exception; |
||
11 | use Laminas\Authentication\Storage\StorageInterface; |
||
12 | use Laminas\Stdlib\AbstractOptions; |
||
13 | use function gettype; |
||
14 | use function interface_exists; |
||
15 | use function is_callable; |
||
16 | use function is_string; |
||
17 | use function sprintf; |
||
18 | |||
19 | /** |
||
20 | * This options class can be consumed by five different classes: |
||
21 | * |
||
22 | * DoctrineModule\Authentication\Adapter\ObjectRepository |
||
23 | * DoctrineModule\Service\Authentication\AdapterFactory |
||
24 | * DoctrineModule\Authentication\Storage\ObjectRepository |
||
25 | * DoctrineModule\Service\Authentication\ServiceFactory |
||
26 | * DoctrineModule\Service\Authentication\AuthenticationServiceFactory |
||
27 | * |
||
28 | * When using with DoctrineModule\Authentication\Adapter\ObjectRepository the following |
||
29 | * options are required: |
||
30 | * |
||
31 | * $identityProperty |
||
32 | * $credentialProperty |
||
33 | * |
||
34 | * In addition either $objectRepository or $objectManager and $identityClass must be set. |
||
35 | * If $objectRepository is set, it takes precedence over $objectManager and $identityClass. |
||
36 | * If $objectManager is used, it must be an instance of ObjectManager. |
||
37 | * |
||
38 | * All remains the same using with DoctrineModule\Service\AuthenticationAdapterFactory, |
||
39 | * however, a string may be passed to $objectManager. This string must be a valid key to |
||
40 | * retrieve an ObjectManager instance from the ServiceManager. |
||
41 | * |
||
42 | * When using with DoctrineModule\Authentication\Service\Object repository the following |
||
43 | * options are required: |
||
44 | * |
||
45 | * Either $objectManager, or $classMetadata and $objectRepository. |
||
46 | * |
||
47 | * All remains the same using with DoctrineModule\Service\AuthenticationStorageFactory, |
||
48 | * however, a string may be passed to $objectManager. This string must be a valid key to |
||
49 | * retrieve an ObjectManager instance from the ServiceManager. |
||
50 | * |
||
51 | * @link http://www.doctrine-project.org/ |
||
52 | */ |
||
53 | class Authentication extends AbstractOptions |
||
54 | { |
||
55 | /** |
||
56 | * A valid object implementing ObjectManager interface |
||
57 | * |
||
58 | * @var string | ObjectManager |
||
59 | */ |
||
60 | protected $objectManager; |
||
61 | |||
62 | /** |
||
63 | * A valid object implementing ObjectRepository interface (or ObjectManager/identityClass) |
||
64 | * |
||
65 | * @var ObjectRepository |
||
66 | */ |
||
67 | protected $objectRepository; |
||
68 | |||
69 | /** |
||
70 | * Entity's class name |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $identityClass; |
||
75 | |||
76 | /** |
||
77 | * Property to use for the identity |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $identityProperty; |
||
82 | |||
83 | /** |
||
84 | * Property to use for the credential |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $credentialProperty; |
||
89 | |||
90 | /** |
||
91 | * Callable function to check if a credential is valid |
||
92 | * |
||
93 | * @var mixed |
||
94 | */ |
||
95 | protected $credentialCallable; |
||
96 | |||
97 | /** |
||
98 | * If an objectManager is not supplied, this metadata will be used |
||
99 | * by DoctrineModule/Authentication/Storage/ObjectRepository |
||
100 | * |
||
101 | * @var ClassMetadata |
||
102 | */ |
||
103 | protected $classMetadata; |
||
104 | |||
105 | /** |
||
106 | * When using this options class to create a DoctrineModule/Authentication/Storage/ObjectRepository |
||
107 | * this is the storage instance that the object key will be stored in. |
||
108 | * |
||
109 | * When using this options class to create an AuthenticationService with and |
||
110 | * the option storeOnlyKeys == false, this is the storage instance that the whole |
||
111 | * object will be stored in. |
||
112 | * |
||
113 | * @var StorageInterface|string; |
||
114 | */ |
||
115 | protected $storage = 'DoctrineModule\Authentication\Storage\Session'; |
||
116 | |||
117 | /** |
||
118 | * @param string | ObjectManager $objectManager |
||
119 | */ |
||
120 | 7 | public function setObjectManager($objectManager) : Authentication |
|
121 | { |
||
122 | 7 | $this->objectManager = $objectManager; |
|
123 | |||
124 | 7 | return $this; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Causes issue with unit test StorageFactoryTest::testCanInstantiateStorageFromServiceLocator |
||
129 | * when return type is specified |
||
130 | * : ObjectManager |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | 4 | public function getObjectManager() |
|
135 | { |
||
136 | 4 | return $this->objectManager; |
|
137 | } |
||
138 | |||
139 | 6 | public function setObjectRepository(ObjectRepository $objectRepository) : Authentication |
|
140 | { |
||
141 | 6 | $this->objectRepository = $objectRepository; |
|
142 | |||
143 | 6 | return $this; |
|
144 | } |
||
145 | |||
146 | 7 | public function getObjectRepository() : ObjectRepository |
|
147 | { |
||
148 | 7 | if ($this->objectRepository) { |
|
149 | 6 | return $this->objectRepository; |
|
150 | } |
||
151 | |||
152 | 1 | return $this->objectManager->getRepository($this->identityClass); |
|
153 | } |
||
154 | |||
155 | 7 | public function setIdentityClass(string $identityClass) : Authentication |
|
156 | { |
||
157 | 7 | $this->identityClass = $identityClass; |
|
158 | |||
159 | 7 | return $this; |
|
160 | } |
||
161 | |||
162 | public function getIdentityClass() : string |
||
163 | { |
||
164 | return $this->identityClass; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @throws Exception\InvalidArgumentException |
||
169 | */ |
||
170 | 10 | public function setIdentityProperty(string $identityProperty) : Authentication |
|
171 | { |
||
172 | 10 | if (! is_string($identityProperty) || $identityProperty === '') { |
|
173 | 1 | throw new Exception\InvalidArgumentException( |
|
174 | 1 | sprintf('Provided $identityProperty is invalid, %s given', gettype($identityProperty)) |
|
175 | ); |
||
176 | } |
||
177 | |||
178 | 9 | $this->identityProperty = $identityProperty; |
|
179 | |||
180 | 9 | return $this; |
|
181 | } |
||
182 | |||
183 | 6 | public function getIdentityProperty() : string |
|
184 | { |
||
185 | 6 | return $this->identityProperty; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * @throws Exception\InvalidArgumentException |
||
190 | */ |
||
191 | 10 | public function setCredentialProperty(string $credentialProperty) : Authentication |
|
192 | { |
||
193 | 10 | if (! is_string($credentialProperty) || $credentialProperty === '') { |
|
194 | 1 | throw new Exception\InvalidArgumentException( |
|
195 | 1 | sprintf('Provided $credentialProperty is invalid, %s given', gettype($credentialProperty)) |
|
196 | ); |
||
197 | } |
||
198 | |||
199 | 9 | $this->credentialProperty = $credentialProperty; |
|
200 | |||
201 | 9 | return $this; |
|
202 | } |
||
203 | |||
204 | 6 | public function getCredentialProperty() : string |
|
205 | { |
||
206 | 6 | return $this->credentialProperty; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param mixed $credentialCallable |
||
211 | * |
||
212 | * @throws Exception\InvalidArgumentException |
||
213 | */ |
||
214 | 2 | public function setCredentialCallable($credentialCallable) : Authentication |
|
215 | { |
||
216 | 2 | if (! is_callable($credentialCallable)) { |
|
217 | 1 | throw new Exception\InvalidArgumentException( |
|
218 | 1 | sprintf( |
|
219 | 1 | '"%s" is not a callable', |
|
220 | 1 | is_string($credentialCallable) ? $credentialCallable : gettype($credentialCallable) |
|
221 | ) |
||
222 | ); |
||
223 | } |
||
224 | |||
225 | 1 | $this->credentialCallable = $credentialCallable; |
|
226 | |||
227 | 1 | return $this; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return mixed |
||
232 | */ |
||
233 | 4 | public function getCredentialCallable() |
|
234 | { |
||
235 | 4 | return $this->credentialCallable; |
|
236 | } |
||
237 | |||
238 | 1 | public function getClassMetadata() : ClassMetadata |
|
239 | { |
||
240 | 1 | if ($this->classMetadata) { |
|
241 | 1 | return $this->classMetadata; |
|
242 | } |
||
243 | |||
244 | return $this->objectManager->getClassMetadata($this->identityClass); |
||
245 | } |
||
246 | |||
247 | 1 | public function setClassMetadata(ClassMetadata $classMetadata) : void |
|
248 | { |
||
249 | 1 | $this->classMetadata = $classMetadata; |
|
250 | 1 | } |
|
251 | |||
252 | /** |
||
253 | * @return StorageInterface|string |
||
254 | */ |
||
255 | 4 | public function getStorage() |
|
256 | { |
||
257 | 4 | return $this->storage; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param StorageInterface|string $storage |
||
262 | */ |
||
263 | 4 | public function setStorage($storage) : void |
|
264 | { |
||
265 | 4 | $this->storage = $storage; |
|
0 ignored issues
–
show
|
|||
266 | 4 | } |
|
267 | } |
||
268 | |||
269 | 1 | interface_exists(ClassMetadata::class); |
|
270 | interface_exists(ObjectRepository::class); |
||
271 |
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.