Skip to content

Commit 14ef7e2

Browse files
chore: fix mongolid model returns
1 parent da35371 commit 14ef7e2

File tree

8 files changed

+62
-60
lines changed

8 files changed

+62
-60
lines changed

composer.lock

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LaravelCacheComponent.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ class LaravelCacheComponent implements CacheComponentInterface
2020

2121
/**
2222
* Copy cache result in memory array.
23-
*
24-
* @var mixed[]
2523
*/
26-
private $inMemoryCache = [];
24+
private array $inMemoryCache = [];
2725

2826
/**
2927
* Injects the dependencies of LaravelCacheComponent.
@@ -42,7 +40,7 @@ public function __construct(Repository $laravelCache)
4240
*
4341
* @return mixed
4442
*/
45-
public function get(string $key)
43+
public function get(string $key): mixed
4644
{
4745
if (isset($this->inMemoryCache[$key])) {
4846
return $this->inMemoryCache[$key];
@@ -61,7 +59,7 @@ public function get(string $key)
6159
* @param mixed $value value being stored in cache
6260
* @param float $minutes cache ttl
6361
*/
64-
public function put(string $key, $value, float $minutes)
62+
public function put(string $key, $value, float $minutes): void
6563
{
6664
if (is_array($value)) {
6765
foreach ($value as $index => $document) {

src/LegacyMongolidModel.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use MongoDB\Collection;
1111
use Mongolid\Cursor\CursorInterface;
1212
use Mongolid\LegacyRecord;
13+
use Mongolid\Model\ModelInterface;
1314

1415
/**
1516
* This class extends the Mongolid\LegacyRecord, so, in order
@@ -114,7 +115,7 @@ public function delete(): bool
114115
*
115116
* @return bool
116117
*/
117-
public function isValid()
118+
public function isValid(): bool
118119
{
119120
if (!$rules = $this->rules()) {
120121
return true;
@@ -208,7 +209,7 @@ public static function first(
208209
$query = [],
209210
array $projection = [],
210211
bool $useCache = false
211-
) {
212+
): ?LegacyRecord {
212213
return static::callMockOrParent('first', func_get_args());
213214
}
214215

@@ -228,7 +229,7 @@ public static function firstOrFail(
228229
$query = [],
229230
array $projection = [],
230231
bool $useCache = false
231-
): mixed {
232+
): ?ModelInterface {
232233
return static::callMockOrParent('firstOrFail', func_get_args());
233234
}
234235

@@ -241,7 +242,7 @@ public static function firstOrFail(
241242
*
242243
* @return LegacyRecord
243244
*/
244-
public static function firstOrNew($id)
245+
public static function firstOrNew($id): ?LegacyRecord
245246
{
246247
return static::callMockOrParent('firstOrNew', func_get_args());
247248
}

src/MongolidModel.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
77
use Illuminate\Support\MessageBag;
88
use Mockery;
9+
use Mockery\CompositeExpectation;
910
use Mockery\Expectation;
1011
use MongoDB\Collection;
1112
use Mongolid\Cursor\CursorInterface;
1213
use Mongolid\LegacyRecord;
1314
use Mongolid\Model\AbstractModel;
15+
use Mongolid\Model\ModelInterface;
1416

1517
/**
1618
* This class extends the Mongolid\LegacyRecord, so, in order
1719
* to understand the ODM implementation make sure to check the
1820
* base class.
1921
*
2022
* The MongolidLaravel\MongolidModel simply extends the original
21-
* and framework agnostic model of MongoLid and implements some
23+
* and framework-agnostic model of MongoLid and implements some
2224
* validation rules using Laravel validation components.
2325
*
2426
* Remember, this package is meant to be used with Laravel while
@@ -124,7 +126,7 @@ public function isValid()
124126
$attributes = $this->getDocumentAttributes();
125127

126128
// Verify attributes that are hashed and that have not changed
127-
// those doesn't need to be validated.
129+
// this doesn't need to be validated.
128130
foreach ($this->hashedAttributes as $hashedAttr) {
129131
if (isset($this->original[$hashedAttr]) && $this->$hashedAttr == $this->original[$hashedAttr]) {
130132
unset($rules[$hashedAttr]);
@@ -181,7 +183,7 @@ public function messages(): array
181183
*
182184
* @return Expectation
183185
*/
184-
public function shouldReceiveSave()
186+
public function shouldReceiveSave(): CompositeExpectation
185187
{
186188
return $this->localMockShouldReceive('save');
187189
}
@@ -191,7 +193,7 @@ public function shouldReceiveSave()
191193
*
192194
* @return Expectation
193195
*/
194-
public function shouldReceiveDelete()
196+
public function shouldReceiveDelete(): CompositeExpectation
195197
{
196198
return $this->localMockShouldReceive('delete');
197199
}
@@ -209,7 +211,7 @@ public static function first(
209211
$query = [],
210212
array $projection = [],
211213
bool $useCache = false
212-
) {
214+
): ?ModelInterface {
213215
return static::callMockOrParent('first', func_get_args());
214216
}
215217

@@ -229,7 +231,7 @@ public static function firstOrFail(
229231
$query = [],
230232
array $projection = [],
231233
bool $useCache = false
232-
) {
234+
): ?ModelInterface {
233235
return static::callMockOrParent('firstOrFail', func_get_args());
234236
}
235237

@@ -240,9 +242,9 @@ public static function firstOrFail(
240242
*
241243
* @param mixed $id document id
242244
*
243-
* @return LegacyRecord
245+
* @return AbstractModel
244246
*/
245-
public static function firstOrNew($id)
247+
public static function firstOrNew(mixed $id): AbstractModel
246248
{
247249
return static::callMockOrParent('firstOrNew', func_get_args());
248250
}
@@ -283,7 +285,7 @@ protected function collection(): Collection
283285
* Hashes the attributes specified in the hashedAttributes
284286
* array.
285287
*/
286-
protected function hashAttributes()
288+
protected function hashAttributes(): void
287289
{
288290
foreach ($this->hashedAttributes as $attr) {
289291
// Hash attribute if changed
@@ -334,7 +336,7 @@ protected function localMockShouldReceive(string $method)
334336
}
335337

336338
/**
337-
* Check for a expectation for given method on local mock.
339+
* Check for an expectation for given method on local mock.
338340
*
339341
* @param string $method name of the method being checked
340342
*/

tests/MongolidModelTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function testShouldSave()
138138
$dataMapper = $this->instance(Builder::class, m::mock(Builder::class));
139139

140140
$model = new class () extends MongolidModel {
141-
protected $collection = 'users';
141+
protected ?string $collection = 'users';
142142
};
143143

144144
// Expectations
@@ -185,7 +185,7 @@ public function testShouldHashAttributesOnSaveAndUpdate($method)
185185
$hasher = $this->instance(Hasher::class, m::mock(Hasher::class));
186186

187187
$model = new class () extends MongolidModel {
188-
protected $collection = 'users';
188+
protected ?string $collection = 'users';
189189

190190
protected $hashedAttributes = ['password'];
191191
};
@@ -280,7 +280,7 @@ public function testShouldDelete()
280280
$dataMapper = $this->instance(Builder::class, m::mock(Builder::class));
281281

282282
$model = new class () extends MongolidModel {
283-
protected $collection = 'collection_name';
283+
protected ?string $collection = 'collection_name';
284284
};
285285

286286
// Expectations
@@ -323,7 +323,7 @@ public function testShouldGetFirst()
323323
$dataMapper = $this->instance(Builder::class, m::mock(Builder::class));
324324

325325
$model = new class () extends MongolidModel {
326-
protected $collection = 'collection_name';
326+
protected ?string $collection = 'collection_name';
327327
};
328328

329329
// Expectations
@@ -345,7 +345,7 @@ public function testShouldMockFirst()
345345
{
346346
// Set
347347
$model = new class () extends MongolidModel {
348-
protected $collection = 'collection_name';
348+
protected ?string $collection = 'collection_name';
349349
};
350350

351351
// Expectations
@@ -367,7 +367,7 @@ public function testShouldGetFirstOrNew()
367367
$dataMapper = $this->instance(Builder::class, m::mock(Builder::class));
368368

369369
$model = new class () extends MongolidModel {
370-
protected $collection = 'collection_name';
370+
protected ?string $collection = 'collection_name';
371371
};
372372

373373
// Expectations
@@ -389,7 +389,7 @@ public function testShouldMockFirstOrNew()
389389
{
390390
// Set
391391
$model = new class () extends MongolidModel {
392-
protected $collection = 'collection_name';
392+
protected ?string $collection = 'collection_name';
393393
};
394394

395395
// Expectations
@@ -411,7 +411,7 @@ public function testShouldGetFirstOrFailAndFoundIt()
411411
$dataMapper = $this->instance(Builder::class, m::mock(Builder::class));
412412

413413
$model = new class () extends MongolidModel {
414-
protected $collection = 'collection_name';
414+
protected ?string $collection = 'collection_name';
415415
};
416416

417417
// Expectations
@@ -435,7 +435,7 @@ public function testShouldGetFirstOrFailAndFail()
435435
$dataMapper = $this->instance(Builder::class, m::mock(Builder::class));
436436

437437
$model = new class () extends MongolidModel {
438-
protected $collection = 'collection_name';
438+
protected ?string $collection = 'collection_name';
439439
};
440440

441441
// Expectations
@@ -456,7 +456,7 @@ public function testShouldMockFirstOrFail()
456456
{
457457
// Set
458458
$model = new class () extends MongolidModel {
459-
protected $collection = 'collection_name';
459+
protected ?string $collection = 'collection_name';
460460
};
461461

462462
// Expectations
@@ -479,7 +479,7 @@ public function testShouldGetWhere()
479479
$cursor = m::mock(CursorInterface::class);
480480

481481
$model = new class () extends MongolidModel {
482-
protected $collection = 'collection_name';
482+
protected ?string $collection = 'collection_name';
483483
};
484484

485485
// Expectations
@@ -504,7 +504,7 @@ public function testShouldGetAll()
504504
$cursor = m::mock(CursorInterface::class);
505505

506506
$model = new class () extends MongolidModel {
507-
protected $collection = 'collection_name';
507+
protected ?string $collection = 'collection_name';
508508
};
509509

510510
// Expectations
@@ -547,7 +547,7 @@ public function testShouldGetCollection()
547547
$client = m::mock(Client::class);
548548

549549
$model = new class () extends MongolidModel {
550-
protected $collection = 'collection_name';
550+
protected ?string $collection = 'collection_name';
551551

552552
public function rawCollection()
553553
{

0 commit comments

Comments
 (0)