Skip to content

Commit

Permalink
PHPStan level max
Browse files Browse the repository at this point in the history
  • Loading branch information
othillo committed Mar 8, 2020
1 parent d2acc95 commit d6be1be
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 13 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ test:
vendor/bin/phpunit --testdox --exclude-group=none --colors=always

.PHONY: qa
qa: php-cs-fixer
qa: php-cs-fixer phpstan

.PHONY: php-cs-fixer
php-cs-fixer:
vendor/bin/php-cs-fixer fix --no-interaction --allow-risky=yes --diff --verbose

PHONY: phpstan
phpstan:
vendor/bin/phpstan analyse --level=max src/

.PHONY: changelog
changelog:
git log $$(git describe --abbrev=0 --tags)...HEAD --no-merges --pretty=format:"* [%h](http://github.com/${TRAVIS_REPO_SLUG}/commit/%H) %s (%cN)"
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"ext-pdo_sqlite": "*",
"phpunit/phpunit": "^8.0",
"ramsey/uuid": "^3.0",
"broadway/coding-standard": "^1.0"
"broadway/coding-standard": "^1.0",
"phpstan/phpstan": "@stable"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 17 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
parameters:
ignoreErrors:
-
message: "#^Cannot call static method fromString\\(\\) on Broadway\\\\UuidGenerator\\\\Converter\\\\BinaryUuidConverterInterface\\|null\\.$#"
count: 1
path: src/DBALEventStore.php

-
message: "#^Cannot call static method fromBytes\\(\\) on Broadway\\\\UuidGenerator\\\\Converter\\\\BinaryUuidConverterInterface\\|null\\.$#"
count: 1
path: src/DBALEventStore.php

-
message: "#^Method Broadway\\\\EventStore\\\\Dbal\\\\DBALEventStore\\:\\:prepareVisitEventsStatement\\(\\) should return Doctrine\\\\DBAL\\\\Driver\\\\Statement but returns Doctrine\\\\DBAL\\\\Driver\\\\ResultStatement\\.$#"
count: 1
path: src/DBALEventStore.php

6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
includes:
- phpstan-baseline.neon

parameters:
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
49 changes: 40 additions & 9 deletions src/DBALEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use Broadway\UuidGenerator\Converter\BinaryUuidConverterInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Version;
Expand All @@ -40,18 +42,39 @@
*/
class DBALEventStore implements EventStore, EventStoreManagement
{
/**
* @var Connection
*/
private $connection;

/**
* @var Serializer
*/
private $payloadSerializer;

/**
* @var Serializer
*/
private $metadataSerializer;

/**
* @var Statement|null
*/
private $loadStatement = null;

/**
* @var string
*/
private $tableName;

/**
* @var bool
*/
private $useBinary;

/**
* @var BinaryUuidConverterInterface|null
*/
private $binaryUuidConverter;

public function __construct(
Expand Down Expand Up @@ -150,7 +173,7 @@ public function append($id, DomainEventStream $eventStream): void
}
}

private function insertMessage(Connection $connection, DomainMessage $domainMessage)
private function insertMessage(Connection $connection, DomainMessage $domainMessage): void
{
$data = [
'uuid' => $this->convertIdentifierToStorageValue((string) $domainMessage->getId()),
Expand All @@ -167,7 +190,7 @@ private function insertMessage(Connection $connection, DomainMessage $domainMess
/**
* @return \Doctrine\DBAL\Schema\Table|null
*/
public function configureSchema(Schema $schema)
public function configureSchema(Schema $schema): ?\Doctrine\DBAL\Schema\Table
{
if ($schema->hasTable($this->tableName)) {
return null;
Expand All @@ -176,7 +199,7 @@ public function configureSchema(Schema $schema)
return $this->configureTable($schema);
}

public function configureTable(Schema $schema = null)
public function configureTable(Schema $schema = null): \Doctrine\DBAL\Schema\Table
{
$schema = $schema ?: new Schema();

Expand Down Expand Up @@ -211,7 +234,7 @@ public function configureTable(Schema $schema = null)
return $table;
}

private function prepareLoadStatement()
private function prepareLoadStatement(): Statement
{
if (null === $this->loadStatement) {
$query = 'SELECT uuid, playhead, metadata, payload, recorded_on
Expand All @@ -225,7 +248,7 @@ private function prepareLoadStatement()
return $this->loadStatement;
}

private function deserializeEvent($row)
private function deserializeEvent(array $row): DomainMessage
{
return new DomainMessage(
$this->convertStorageValueToIdentifier($row['uuid']),
Expand All @@ -236,11 +259,15 @@ private function deserializeEvent($row)
);
}

/**
* @param mixed $id
* @return mixed
*/
private function convertIdentifierToStorageValue($id)
{
if ($this->useBinary) {
try {
return $this->binaryUuidConverter->fromString($id);
return $this->binaryUuidConverter::fromString($id);
} catch (\Exception $e) {
throw new InvalidIdentifierException('Only valid UUIDs are allowed to by used with the binary storage mode.');
}
Expand All @@ -249,11 +276,15 @@ private function convertIdentifierToStorageValue($id)
return $id;
}

/**
* @param mixed $id
* @return mixed
*/
private function convertStorageValueToIdentifier($id)
{
if ($this->useBinary) {
try {
return $this->binaryUuidConverter->fromBytes($id);
return $this->binaryUuidConverter::fromBytes($id);
} catch (\Exception $e) {
throw new InvalidIdentifierException('Could not convert binary storage value to UUID.');
}
Expand All @@ -274,7 +305,7 @@ public function visitEvents(Criteria $criteria, EventVisitor $eventVisitor): voi
}
}

private function prepareVisitEventsStatement(Criteria $criteria)
private function prepareVisitEventsStatement(Criteria $criteria): Statement
{
list($where, $bindValues, $bindValueTypes) = $this->prepareVisitEventsStatementWhereAndBindValues($criteria);
$query = 'SELECT uuid, playhead, metadata, payload, recorded_on
Expand All @@ -287,7 +318,7 @@ private function prepareVisitEventsStatement(Criteria $criteria)
return $statement;
}

private function prepareVisitEventsStatementWhereAndBindValues(Criteria $criteria)
private function prepareVisitEventsStatementWhereAndBindValues(Criteria $criteria): array
{
if ($criteria->getAggregateRootTypes()) {
throw new CriteriaNotSupportedException('DBAL implementation cannot support criteria based on aggregate root types.');
Expand Down
4 changes: 2 additions & 2 deletions src/DBALEventStoreException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
class DBALEventStoreException extends EventStoreException
{
public static function create(DBALException $exception)
public static function create(DBALException $exception): DBALEventStoreException
{
return new DBALEventStoreException(null, 0, $exception);
return new DBALEventStoreException('', 0, $exception);
}
}

0 comments on commit d6be1be

Please sign in to comment.