Skip to content

Commit 757fc86

Browse files
author
Iosif Chiriluta | eMAG, Technology
committed
feat: Upgrade PHPStan and fix new errors
Upgraded PHPStan to version 2.1 to resolve a static analysis issue with outdated MongoDB stubs. This upgrade introduced stricter type checks, which revealed a redundant PHPDoc in `MongoTransport.php`. The PHPDoc has been removed. Also includes minor code style improvements in tests and updates to the Docker environment to match the current development setup.
1 parent d127e82 commit 757fc86

7 files changed

Lines changed: 95 additions & 48 deletions

File tree

GEMINI.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Project Overview
2+
3+
This project is a Symfony Bundle that provides a MongoDB transport for the Symfony Messenger component. It allows developers to use MongoDB as a message queue for their Symfony applications. This is particularly useful when an application already uses MongoDB and wants to avoid adding another dependency for a message queue.
4+
5+
The core of the bundle is the `MongoTransport` class, which implements the Symfony `TransportInterface`. This class handles the sending, receiving, acknowledging, and rejecting of messages using a MongoDB collection.
6+
7+
The project uses Composer for dependency management, PHPUnit for testing, Infection for mutation testing, and PHPStan for static analysis.
8+
9+
# Building and Running
10+
11+
## Dependencies
12+
13+
Install dependencies using Composer:
14+
15+
```bash
16+
composer install
17+
```
18+
19+
## Running Tests
20+
21+
Run the test suite using PHPUnit:
22+
23+
```bash
24+
vendor/bin/phpunit
25+
```
26+
27+
## Static Analysis
28+
29+
Run PHPStan for static analysis:
30+
31+
```bash
32+
vendor/bin/phpstan analyse src tests
33+
```
34+
35+
## Mutation Testing
36+
37+
Run Infection for mutation testing:
38+
39+
```bash
40+
vendor/bin/infection
41+
```
42+
43+
# Development Conventions
44+
45+
## Coding Style
46+
47+
The project follows the PSR-12 coding style guide.
48+
49+
## Testing
50+
51+
The project has a comprehensive test suite using PHPUnit. Tests are located in the `tests` directory and are separated into unit and integration tests.
52+
53+
## Contribution
54+
55+
Contributions are welcome. Please open an issue or create a pull request on the [GitHub repository](https://github.com/eMAGTechLabs/messenger-mongo-bundle).

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
"require": {
2020
"php": "^8.1",
2121
"ext-json": "*",
22-
"ext-mongodb": "*",
23-
"symfony/messenger": "^5.0 || ^6.0 || ^7.0",
22+
"ext-mongodb": "^2.1.4",
23+
"symfony/messenger": "^6.0 || ^7.0",
2424
"mongodb/mongodb": "^2.1"
2525
},
2626
"require-dev": {
27-
"symfony/serializer": "^5.0 || ^6.0 || ^7.0",
28-
"symfony/property-access": "^5.0 || ^6.0 || ^7.0",
29-
"symfony/var-dumper": "^5.0 || ^6.0 || ^7.0",
30-
"symfony/framework-bundle": "^5.0 || ^6.0 || ^7.0",
27+
"symfony/serializer": "^6.0 || ^7.0",
28+
"symfony/property-access": "^6.0 || ^7.0",
29+
"symfony/var-dumper": "^6.0 || ^7.0",
30+
"symfony/framework-bundle": "^6.0 || ^7.0",
3131
"phpunit/phpunit": "^10.5",
3232
"infection/infection": "^0.27.9",
33-
"phpstan/phpstan": "^1.10"
33+
"phpstan/phpstan": "^2.1"
3434
},
3535
"autoload": {
3636
"psr-4": {

docker-compose.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3.6'
21
services:
32
php:
43
build:
@@ -14,5 +13,3 @@ services:
1413
environment:
1514
- MONGO_INITDB_ROOT_USERNAME=root
1615
- MONGO_INITDB_ROOT_PASSWORD=rootpass
17-
18-

docker/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:8.3-alpine
1+
FROM php:8.4-alpine
22

33
WORKDIR /var/www/html
44

@@ -7,7 +7,7 @@ RUN apk update \
77
&& apk add --no-cache git zip make autoconf g++ openssl-dev linux-headers \
88
&& apk add --no-cache --virtual .build-deps $PHPIZE_DEPS
99

10-
RUN pecl -q install mongodb-1.17.2 \
10+
RUN pecl -q install mongodb-2.1.4 \
1111
&& docker-php-ext-enable mongodb \
1212
&& pecl -q install xdebug \
1313
&& docker-php-ext-enable xdebug
@@ -18,4 +18,4 @@ RUN curl --silent --show-error https://getcomposer.org/installer | php \
1818
&& mv composer.phar /usr/local/bin/composer \
1919
&& chmod o+x /usr/local/bin/composer
2020

21-
ENTRYPOINT ["tail", "-f", "/dev/null"]
21+
ENTRYPOINT ["tail", "-f", "/dev/null"]

phpstan.neon.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
parameters:
22
level: 5
3-
checkMissingIterableValueType: false
43
paths:
54
- src
65
- tests
7-
ignoreErrors:
8-
- '#Access to an undefined property MongoDB\\Collection::\$documents.#'

src/MongoTransport.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public function get(): iterable
102102
*/
103103
private function removeMessage(Envelope $envelope): void
104104
{
105-
/** @var TransportMessageIdStamp $transportMessageIdStamp */
106105
$transportMessageIdStamp = $envelope->last(TransportMessageIdStamp::class);
107106

108107
if (!$transportMessageIdStamp instanceof TransportMessageIdStamp) {
@@ -161,7 +160,7 @@ public function send(Envelope $envelope): Envelope
161160
return $envelope->with(new TransportMessageIdStamp($objectId));
162161
}
163162

164-
public function all(int $limit = null): iterable
163+
public function all(?int $limit = null): iterable
165164
{
166165
$documents = $this->collection->find([], ['limit' => $limit]);
167166

@@ -170,7 +169,7 @@ public function all(int $limit = null): iterable
170169
}
171170
}
172171

173-
public function find($id): ?Envelope
172+
public function find(mixed $id): ?Envelope
174173
{
175174
$document = $this->collection->findOne(['_id' => is_string($id) ? new ObjectId($id) : $id]);
176175

tests/Unit/MongoTransportTest.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
use EmagTechLabs\MessengerMongoBundle\MongoTransport;
88
use EmagTechLabs\MessengerMongoBundle\Tests\Unit\Fixtures\HelloMessage;
9+
use MongoDB\BSON\Int64;
910
use MongoDB\BSON\ObjectId;
1011
use MongoDB\Collection;
12+
use MongoDB\Driver\CursorInterface;
13+
use MongoDB\Driver\Server;
1114
use MongoDB\InsertOneResult;
1215
use PHPUnit\Framework\Attributes\Test;
1316
use PHPUnit\Framework\TestCase;
17+
use RuntimeException;
1418
use Symfony\Component\Messenger\Envelope;
1519
use Symfony\Component\Messenger\Stamp\DelayStamp;
1620
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
@@ -195,7 +199,24 @@ public function itShouldReturnNothingIfIdCouldNotBeFound(): void
195199
#[Test]
196200
public function itShouldSendAMessage(): void
197201
{
198-
$collection = $this->createCollection();
202+
$collection = new class extends Collection {
203+
public array $documents = [];
204+
205+
public function __construct()
206+
{
207+
}
208+
209+
public function insertOne($document, array $options = []): InsertOneResult
210+
{
211+
$this->documents[] = $document;
212+
213+
return new class extends InsertOneResult {
214+
public function __construct()
215+
{
216+
}
217+
};
218+
}
219+
};
199220

200221
$transport = new MongoTransport(
201222
$collection,
@@ -252,28 +273,6 @@ public function itShouldDeleteTheDocumentOnAckOrReject(): void
252273
$transport->reject($envelope);
253274
}
254275

255-
private function createCollection(array $documents = []): Collection
256-
{
257-
return new class extends Collection {
258-
public array $documents = [];
259-
260-
public function __construct()
261-
{
262-
}
263-
264-
public function insertOne($document, array $options = []): InsertOneResult
265-
{
266-
$this->documents[] = $document;
267-
268-
return new class extends InsertOneResult {
269-
public function __construct()
270-
{
271-
}
272-
};
273-
}
274-
};
275-
}
276-
277276
private function createDocument(): array
278277
{
279278
return [
@@ -291,9 +290,9 @@ private function createSerializer(): SerializerInterface
291290
return new Serializer();
292291
}
293292

294-
private function createCursor(array $documents): \MongoDB\Driver\CursorInterface
293+
private function createCursor(array $documents): CursorInterface
295294
{
296-
return new class($documents) implements \MongoDB\Driver\CursorInterface
295+
return new class($documents) implements CursorInterface
297296
{
298297
private array $data;
299298
private int $position = 0;
@@ -342,14 +341,14 @@ public function setTypeMap(array $typemap): void
342341
{
343342
}
344343

345-
public function getId(): \MongoDB\BSON\Int64
344+
public function getId(): Int64
346345
{
347-
throw new \RuntimeException('Not implemented in test cursor.');
346+
throw new RuntimeException('Not implemented in test cursor.');
348347
}
349348

350-
public function getServer(): \MongoDB\Driver\Server
349+
public function getServer(): Server
351350
{
352-
throw new \RuntimeException('Not implemented in test cursor.');
351+
throw new RuntimeException('Not implemented in test cursor.');
353352
}
354353
};
355354
}

0 commit comments

Comments
 (0)