Skip to content

Commit

Permalink
chore: use psalm level 2 (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas authored Oct 3, 2023
1 parent 2a84341 commit ff40d42
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function __invoke(Request $request): JsonResponse
$response = $this->queryBus->ask(
new SearchBackofficeCoursesByCriteriaQuery(
(array) $request->query->get('filters'),
$orderBy === null ? null : (string) $orderBy,
$order === null ? null : (string) $order,
$orderBy === null ? null : $orderBy,
$order === null ? null : $order,
$limit === null ? null : (int) $limit,
$offset === null ? null : (int) $offset
)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"vimeo/psalm": "^5.15",
"rector/rector": "^0.18.4",
"psalm/plugin-mockery": "^1.1",
"psalm/plugin-symfony": "^5.0"
"psalm/plugin-symfony": "^5.0",
"psalm/plugin-phpunit": "^0.18.4"
},
"autoload": {
"psr-4": {
Expand Down
62 changes: 61 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorLevel="3"
errorLevel="2"
resolveFromConfigFile="true"
findUnusedBaselineEntry="false"
findUnusedCode="false"
allowStringToStandInForClass="true"
>
<projectFiles>
<directory name="apps"/>
Expand Down Expand Up @@ -44,15 +45,26 @@
<directory name="tests"/>
</errorLevel>
</PossiblyNullArgument>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PropertyNotSetInConstructor>
<MoreSpecificReturnType>
<errorLevel type="suppress">
<file name="apps/*/*/src/*Kernel.php"/>
</errorLevel>
</MoreSpecificReturnType>
<UnresolvableInclude>
<errorLevel type="suppress">
<file name="apps/*/*/src/*Kernel.php"/>
</errorLevel>
</UnresolvableInclude>
</issueHandlers>

<plugins>
<pluginClass class="Psalm\MockeryPlugin\Plugin"/>
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin"/>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>
4 changes: 2 additions & 2 deletions src/Mooc/Shared/Infrastructure/Doctrine/DbalTypesSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static function modulesInPath(string $path): array
private static function possibleDbalPaths(string $path): array
{
return map(
static function ($unused, string $module) use ($path) {
static function (mixed $_unused, string $module) use ($path) {
$mappingsPath = self::MAPPINGS_PATH;

return realpath("$path/$module/$mappingsPath");
Expand All @@ -51,7 +51,7 @@ private static function dbalClassesSearcher(string $contextName): callable
{
return static function (array $totalNamespaces, string $path) use ($contextName): array {
$possibleFiles = scandir($path);
$files = filter(static fn ($file): bool => Utils::endsWith('Type.php', $file), $possibleFiles);
$files = filter(static fn (string $file): bool => Utils::endsWith('Type.php', $file), $possibleFiles);

$namespaces = map(
static function (string $file) use ($path, $contextName): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static function modulesInPath(string $path): array
private static function possibleMappingPaths(string $path): array
{
return map(
static function ($unused, string $module) use ($path) {
static function (mixed $_unused, string $module) use ($path) {
$mappingsPath = self::MAPPINGS_PATH;

return realpath("$path/$module/$mappingsPath");
Expand Down
13 changes: 7 additions & 6 deletions src/Mooc/Videos/Application/Find/FindVideoQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
use CodelyTv\Shared\Domain\Bus\Query\QueryHandler;

use function Lambdish\Phunctional\apply;
use function Lambdish\Phunctional\pipe;

final class FindVideoQueryHandler implements QueryHandler
final readonly class FindVideoQueryHandler implements QueryHandler
{
private $finder;
private VideoResponseConverter $responseConverter;

public function __construct(VideoFinder $finder)
public function __construct(private VideoFinder $finder)
{
$this->finder = pipe($finder, new VideoResponseConverter());
$this->responseConverter = new VideoResponseConverter();
}

public function __invoke(FindVideoQuery $query): VideoResponse
{
$id = new VideoId($query->id());

return apply($this->finder, [$id]);
$video = apply($this->finder, [$id]);

return apply($this->responseConverter, [$video]);
}
}
2 changes: 1 addition & 1 deletion src/Shared/Domain/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function arrayOf(string $class, array $items): void
}
}

public static function instanceOf(string $class, $item): void
public static function instanceOf(string $class, mixed $item): void
{
if (!$item instanceof $class) {
throw new InvalidArgumentException(
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Domain/ValueObject/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

abstract class Uuid implements Stringable
{
public function __construct(protected string $value)
final public function __construct(protected string $value)
{
$this->ensureIsValidUuid($value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function forPipedCallables(iterable $callables): array

private static function classExtractor(self $parameterExtractor): callable
{
return static fn (callable $handler): ?string => $parameterExtractor->extract($handler);
return static fn (object $handler): ?string => $parameterExtractor->extract($handler);
}

private static function pipedCallablesReducer(): callable
Expand All @@ -46,10 +46,10 @@ private static function pipedCallablesReducer(): callable

private static function unflatten(): callable
{
return static fn ($value): array => [$value];
return static fn (mixed $value): array => [$value];
}

public function extract($class): ?string
public function extract(object $class): ?string
{
$reflector = new ReflectionClass($class);
$method = $reflector->getMethod('__invoke');
Expand All @@ -63,7 +63,7 @@ public function extract($class): ?string

private function firstParameterClassFrom(ReflectionMethod $method): string
{
/** @var ReflectionNamedType $fistParameterType */
/** @var ReflectionNamedType|null $fistParameterType */
$fistParameterType = $method->getParameters()[0]->getType();

if ($fistParameterType === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use CodelyTv\Shared\Domain\Bus\Event\DomainEvent;
use CodelyTv\Shared\Domain\Utils;
use RuntimeException;

final readonly class DomainEventJsonDeserializer
{
Expand All @@ -18,10 +17,6 @@ public function deserialize(string $domainEvent): DomainEvent
$eventName = $eventData['data']['type'];
$eventClass = $this->mapping->for($eventName);

if ($eventClass === null) {
throw new RuntimeException("The event <$eventName> doesn't exist or has no subscribers");
}

return $eventClass::fromPrimitives(
$eventData['data']['attributes']['id'],
$eventData['data']['attributes'],
Expand Down
4 changes: 2 additions & 2 deletions src/Shared/Infrastructure/Bus/Event/DomainEventMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

final class DomainEventMapping
{
private $mapping;
private array $mapping;

public function __construct(iterable $mapping)
{
$this->mapping = reduce($this->eventsExtractor(), $mapping, []);
}

public function for(string $name)
public function for(string $name): string
{
if (!isset($this->mapping[$name])) {
throw new RuntimeException("The Domain Event Class for <$name> doesn't exists or have no subscribers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use CodelyTv\Shared\Infrastructure\Bus\Event\DomainEventMapping;
use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Doctrine\ORM\EntityManager;
use RuntimeException;

Expand All @@ -28,14 +27,14 @@ public function consume(callable $subscribers, int $eventsToConsume): void
{
$events = $this->connection
->executeQuery("SELECT * FROM domain_events ORDER BY occurred_on ASC LIMIT $eventsToConsume")
->fetchAll(FetchMode::ASSOCIATIVE);
->fetchAllAssociative();

each($this->executeSubscribers($subscribers), $events);

$ids = implode(', ', map($this->idExtractor(), $events));

if (!empty($ids)) {
$this->connection->executeUpdate("DELETE FROM domain_events WHERE id IN ($ids)");
$this->connection->executeStatement("DELETE FROM domain_events WHERE id IN ($ids)");
}
}

Expand All @@ -57,7 +56,7 @@ private function executeSubscribers(callable $subscribers): callable
};
}

private function formatDate($stringDate): string
private function formatDate(mixed $stringDate): string
{
return Utils::dateToString(new DateTimeImmutable($stringDate));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function publisher(): callable
Utils::stringToDate($domainEvent->occurredOn())->format(self::DATABASE_TIMESTAMP_FORMAT)
);

$this->connection->executeUpdate(
$this->connection->executeStatement(
<<<SQL
INSERT INTO domain_events (id, aggregate_id, name, body, occurred_on)
VALUES ($id, $aggregateId, $name, $body, $occurredOn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CodelyTv\Shared\Infrastructure\Doctrine;

use CodelyTv\Shared\Infrastructure\Doctrine\Dbal\DbalCustomTypesRegistrar;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Schema\MySQLSchemaManager;
Expand Down Expand Up @@ -35,7 +36,9 @@ public static function create(

DbalCustomTypesRegistrar::register($dbalCustomTypesClasses);

return EntityManager::create($parameters, self::createConfiguration($contextPrefixes, $isDevMode));
$config = self::createConfiguration($contextPrefixes, $isDevMode);

return new EntityManager(DriverManager::getConnection($parameters, $config, new EventManager()), $config);
}

private static function generateDatabaseIfNotExists(array $parameters, string $schemaFile): void
Expand All @@ -51,14 +54,14 @@ private static function generateDatabaseIfNotExists(array $parameters, string $s
if (!self::databaseExists($databaseName, $schemaManager)) {
$schemaManager->createDatabase($databaseName);

$connection->exec(sprintf('USE %s', $databaseName));
$connection->exec(file_get_contents(realpath($schemaFile)));
$connection->executeStatement(sprintf('USE %s', $databaseName));
$connection->executeStatement(file_get_contents(realpath($schemaFile)));
}

$connection->close();
}

private static function databaseExists($databaseName, MySqlSchemaManager $schemaManager): bool
private static function databaseExists(string $databaseName, MySqlSchemaManager $schemaManager): bool
{
return in_array($databaseName, $schemaManager->listDatabases(), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private function buildComparison(): callable
};
}

private function mapFieldValue(FilterField $field)
private function mapFieldValue(FilterField $field): mixed
{
return array_key_exists($field->value(), $this->criteriaToDoctrineFields)
? $this->criteriaToDoctrineFields[$field->value()]
Expand All @@ -80,19 +80,19 @@ private function formatOrder(Criteria $criteria): ?array
return [$this->mapOrderBy($criteria->order()->orderBy()) => $criteria->order()->orderType()];
}

private function mapOrderBy(OrderBy $field)
private function mapOrderBy(OrderBy $field): mixed
{
return array_key_exists($field->value(), $this->criteriaToDoctrineFields)
? $this->criteriaToDoctrineFields[$field->value()]
: $field->value();
}

private function existsHydratorFor($field): bool
private function existsHydratorFor(mixed $field): bool
{
return array_key_exists($field, $this->hydrators);
}

private function hydrate($field, string $value)
private function hydrate(mixed $field, string $value): mixed
{
return $this->hydrators[$field]($value);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Shared/Domain/DuplicatorMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

final class DuplicatorMother
{
public static function with($object, array $newParams): mixed
public static function with(mixed $object, array $newParams): mixed
{
$duplicated = clone $object;
$reflection = new ReflectionObject($duplicated);
Expand Down
Loading

0 comments on commit ff40d42

Please sign in to comment.