Skip to content

Commit

Permalink
chore(psalm): upgrade to level 3
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Oct 3, 2023
1 parent 4e12904 commit 1d6ba6e
Show file tree
Hide file tree
Showing 35 changed files with 102 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __invoke(Request $request): JsonResponse

return new JsonResponse(
map(
fn (BackofficeCourseResponse $course) => [
fn (BackofficeCourseResponse $course): array => [
'id' => $course->id(),
'name' => $course->name(),
'duration' => $course->duration(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$queueName = (string) $input->getArgument('queue');
$queueName = $input->getArgument('queue');
$eventsToProcess = (int) $input->getArgument('quantity');

repeat($this->consumer($queueName), $eventsToProcess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$path = (string) $input->getArgument('command-path');
$path = $input->getArgument('command-path');

each($this->configCreator($path), $this->locator->all());

Expand Down
20 changes: 20 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@
<directory name="tests"/>
</errorLevel>
</PossiblyUndefinedMethod>
<PossiblyInvalidArgument>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PossiblyInvalidArgument>
<PossiblyNullReference>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PossiblyNullReference>
<PossiblyNullArgument>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PossiblyNullArgument>
<MoreSpecificReturnType>
<errorLevel type="suppress">
<file name="apps/*/*/src/*Kernel.php"/>
</errorLevel>
</MoreSpecificReturnType>
</issueHandlers>

<plugins>
Expand Down
11 changes: 10 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
Expand All @@ -13,6 +14,14 @@
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_82
LevelSetList::UP_TO_PHP_82,
SetList::TYPE_DECLARATION
]);

$rectorConfig->skip([
__DIR__ . '/apps/backoffice/backend/var',
__DIR__ . '/apps/backoffice/frontend/var',
__DIR__ . '/apps/mooc/backend/var',
__DIR__ . '/apps/mooc/frontend/var',
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ public function authenticate(AuthUsername $username, AuthPassword $password): vo
{
$auth = $this->repository->search($username);

$this->ensureUserExist($auth, $username);
$this->ensureCredentialsAreValid($auth, $password);
}

private function ensureUserExist(?AuthUser $auth, AuthUsername $username): void
{
if ($auth === null) {
throw new InvalidAuthUsername($username);
}

$this->ensureCredentialsAreValid($auth, $password);
}

private function ensureCredentialsAreValid(AuthUser $auth, AuthPassword $password): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function searchAll(): BackofficeCoursesResponse

private function toResponse(): callable
{
return static fn (BackofficeCourse $course) => new BackofficeCourseResponse(
return static fn (BackofficeCourse $course): BackofficeCourseResponse => new BackofficeCourseResponse(
$course->id(),
$course->name(),
$course->duration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function search(Filters $filters, Order $order, ?int $limit, ?int $offset

private function toResponse(): callable
{
return static fn (BackofficeCourse $course) => new BackofficeCourseResponse(
return static fn (BackofficeCourse $course): BackofficeCourseResponse => new BackofficeCourseResponse(
$course->id(),
$course->name(),
$course->duration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ protected function aggregateName(): string

private function toCourse(): callable
{
return static fn (array $primitives) => BackofficeCourse::fromPrimitives($primitives);
return static fn (array $primitives): BackofficeCourse => BackofficeCourse::fromPrimitives($primitives);
}
}
2 changes: 1 addition & 1 deletion src/Mooc/CoursesCounter/Domain/CoursesCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public function hasIncremented(CourseId $courseId): bool

private function courseIdComparator(CourseId $courseId): callable
{
return static fn (CourseId $other) => $courseId->equals($other);
return static fn (CourseId $other): bool => $courseId->equals($other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function getName(): string

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return parent::convertToDatabaseValue(map(fn (CourseId $id) => $id->value(), $value), $platform);
return parent::convertToDatabaseValue(map(fn (CourseId $id): string => $id->value(), $value), $platform);
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
$scalars = parent::convertToPHPValue($value, $platform);

return map(fn (string $value) => new CourseId($value), $scalars);
return map(fn (string $value): CourseId => new CourseId($value), $scalars);
}
}
10 changes: 5 additions & 5 deletions src/Mooc/Shared/Infrastructure/Doctrine/DbalTypesSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function inPath(string $path, string $contextName): array
private static function modulesInPath(string $path): array
{
return filter(
static fn (string $possibleModule) => !in_array($possibleModule, ['.', '..'], true),
static fn (string $possibleModule): bool => !in_array($possibleModule, ['.', '..'], true),
scandir($path)
);
}
Expand All @@ -44,17 +44,17 @@ static function ($unused, string $module) use ($path) {

private static function isExistingDbalPath(): callable
{
return static fn (string $path) => !empty($path);
return static fn (string $path): bool => !empty($path);
}

private static function dbalClassesSearcher(string $contextName): callable
{
return static function (array $totalNamespaces, string $path) use ($contextName) {
return static function (array $totalNamespaces, string $path) use ($contextName): array {
$possibleFiles = scandir($path);
$files = filter(static fn ($file) => Utils::endsWith('Type.php', $file), $possibleFiles);
$files = filter(static fn ($file): bool => Utils::endsWith('Type.php', $file), $possibleFiles);

$namespaces = map(
static function (string $file) use ($path, $contextName) {
static function (string $file) use ($path, $contextName): string {
$fullPath = "$path/$file";
$splittedPath = explode("/src/$contextName/", $fullPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function inPath(string $path, string $baseNamespace): array
private static function modulesInPath(string $path): array
{
return filter(
static fn (string $possibleModule) => !in_array($possibleModule, ['.', '..'], true),
static fn (string $possibleModule): bool => !in_array($possibleModule, ['.', '..'], true),
scandir($path)
);
}
Expand All @@ -42,11 +42,11 @@ static function ($unused, string $module) use ($path) {

private static function isExistingMappingPath(): callable
{
return static fn (string $path) => !empty($path);
return static fn (string $path): bool => !empty($path);
}

private static function namespaceFormatter(string $baseNamespace): callable
{
return static fn (string $path, string $module) => "$baseNamespace\\$module\Domain";
return static fn (string $path, string $module): string => "$baseNamespace\\$module\Domain";
}
}
3 changes: 2 additions & 1 deletion src/Mooc/Videos/Application/Find/VideoFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CodelyTv\Mooc\Videos\Application\Find;

use CodelyTv\Mooc\Videos\Domain\Video;
use CodelyTv\Mooc\Videos\Domain\VideoFinder as DomainVideoFinder;
use CodelyTv\Mooc\Videos\Domain\VideoId;
use CodelyTv\Mooc\Videos\Domain\VideoRepository;
Expand All @@ -17,7 +18,7 @@ public function __construct(VideoRepository $repository)
$this->finder = new DomainVideoFinder($repository);
}

public function __invoke(VideoId $id)
public function __invoke(VideoId $id): Video
{
return $this->finder->__invoke($id);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Shared/Domain/Criteria/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function serialize(): string
'%s~~%s~~%s~~%s',
$this->filters->serialize(),
$this->order->serialize(),
$this->offset,
$this->limit
$this->offset ?? 'none',
$this->limit ?? 'none'
);
}
}
8 changes: 6 additions & 2 deletions src/Shared/Domain/Criteria/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function fromValues(array $values): self

private static function filterBuilder(): callable
{
return fn (array $values) => Filter::fromValues($values);
return fn (array $values): Filter => Filter::fromValues($values);
}

public function add(Filter $filter): self
Expand All @@ -33,7 +33,11 @@ public function filters(): array
public function serialize(): string
{
return reduce(
static fn (string $accumulate, Filter $filter) => sprintf('%s^%s', $accumulate, $filter->serialize()),
static fn (string $accumulate, Filter $filter): string => sprintf(
'%s^%s',
$accumulate,
$filter->serialize()
),
$this->items(),
''
);
Expand Down
5 changes: 4 additions & 1 deletion src/Shared/Domain/Criteria/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public static function createDesc(OrderBy $orderBy): self

public static function fromValues(?string $orderBy, ?string $order): self
{
return $orderBy === null ? self::none() : new self(new OrderBy($orderBy), OrderType::from($order));
return ($orderBy === null || $order === null) ? self::none() : new self(
new OrderBy($orderBy),
OrderType::from($order)
);
}

public static function none(): self
Expand Down
5 changes: 4 additions & 1 deletion src/Shared/Domain/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public static function dot(array $array, string $prepend = ''): array

public static function filesIn(string $path, string $fileType): array
{
return filter(static fn (string $possibleModule) => strstr($possibleModule, $fileType), scandir($path));
return filter(
static fn (string $possibleModule): false|string => strstr($possibleModule, $fileType),
scandir($path)
);
}

public static function extractClassName(object $object): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static function classExtractor(self $parameterExtractor): callable

private static function pipedCallablesReducer(): callable
{
return static function ($subscribers, DomainEventSubscriber $subscriber): array {
return static function (array $subscribers, DomainEventSubscriber $subscriber): array {
$subscribedEvents = $subscriber::subscribedTo();

foreach ($subscribedEvents as $subscribedEvent) {
Expand All @@ -46,7 +46,7 @@ private static function pipedCallablesReducer(): callable

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

public function extract($class): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Infrastructure/Bus/Event/DomainEventMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function for(string $name)

private function eventsExtractor(): callable
{
return fn (array $mapping, DomainEventSubscriber $subscriber) => array_merge(
return fn (array $mapping, DomainEventSubscriber $subscriber): array => array_merge(
$mapping,
reindex($this->eventNameExtractor(), $subscriber::subscribedTo())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function allSubscribedTo(string $eventClass): array
public function withRabbitMqQueueNamed(string $queueName): callable|DomainEventSubscriber
{
$subscriber = search(
static fn (DomainEventSubscriber $subscriber) => RabbitMqQueueNameFormatter::format($subscriber) ===
static fn (DomainEventSubscriber $subscriber): bool => RabbitMqQueueNameFormatter::format($subscriber) ===
$queueName,
$this->mapping
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public static function shortFormat(DomainEventSubscriber $subscriber): string

private static function toSnakeCase(): callable
{
return static fn (string $text) => Utils::toSnakeCase($text);
return static fn (string $text): string => Utils::toSnakeCase($text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public static function register(array $customTypeClassNames): void

private static function registerType(): callable
{
return static function (string $customTypeClassName): void {
Type::addType($customTypeClassName::customTypeName(), $customTypeClassName);
return static function (mixed $customTypeClassName): void {
$name = $customTypeClassName::customTypeName();

Type::addType($name, $customTypeClassName);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use CodelyTv\Shared\Domain\Aggregate\AggregateRoot;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Exception\NotSupported;

abstract class DoctrineRepository
{
Expand All @@ -29,6 +30,15 @@ protected function remove(AggregateRoot $entity): void
$this->entityManager()->flush($entity);
}

/**
* @template T of object
*
* @psalm-param class-string<T> $entityClass
*
* @psalm-return EntityRepository<T>
*
* @throws NotSupported
*/
protected function repository(string $entityClass): EntityRepository
{
return $this->entityManager->getRepository($entityClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function searchRawElasticsearchQuery(array $params): array
try {
$result = $this->client->client()->search(array_merge(['index' => $this->indexName()], $params));

$hits = get_in(['hits', 'hits'], $result, []);
$hits = (array) get_in(['hits', 'hits'], $result, []);

return map($this->elasticValuesExtractor(), $hits);
} catch (Missing404Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function onKernelRequest(RequestEvent $event): void
foreach ($jsonData as $key => $value) {
$jsonDataLowerCase[preg_replace_callback(
'/_(.)/',
static fn ($matches) => strtoupper($matches[1]),
static fn ($matches): string => strtoupper((string) $matches[1]),
(string) $key
)] = $value;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Mooc/CoursesCounter/Domain/CoursesCounterMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public static function create(
return new CoursesCounter(
$id ?? CoursesCounterIdMother::create(),
$total ?? CoursesCounterTotalMother::create(),
...count($existingCourses) ? $existingCourses : Repeater::random(fn () => CourseIdMother::create())
...count($existingCourses) ? $existingCourses : Repeater::random(
fn (): CourseId => CourseIdMother::create()
)
);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Shared/Infrastructure/Behat/ApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public function theResponseContentShouldBe(PyStringNode $expectedResponse): void
$expected = $this->sanitizeOutput($expectedResponse->getRaw());
$actual = $this->sanitizeOutput($this->sessionHelper->getResponse());

if ($expected === false || $actual === false) {
throw new RuntimeException('The outputs could not be parsed as JSON');
}

if ($expected !== $actual) {
throw new RuntimeException(
sprintf("The outputs does not match!\n\n-- Expected:\n%s\n\n-- Actual:\n%s", $expected, $actual)
Expand Down
Loading

0 comments on commit 1d6ba6e

Please sign in to comment.