Skip to content

Commit

Permalink
fixed phpstan according to shipmonk strict rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Lung committed Jul 11, 2024
1 parent b98aa9a commit 6515954
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 27 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"phpunit/phpunit": "^11",
"roave/security-advisories": "dev-latest",
"shipmonk/composer-dependency-analyser": "^1.3",
"shipmonk/phpstan-rules": "^3.1",
"symfony/var-dumper": "^7.0"
},
"autoload": {
Expand Down
59 changes: 58 additions & 1 deletion composer.lock

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

5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ parameters:
exceptions:
uncheckedExceptionRegexes:
- '#Exception#'
treatPhpDocTypesAsCertain: false
treatPhpDocTypesAsCertain: false
shipmonkRules:
enforceReadonlyPublicProperty:
enabled: false
1 change: 1 addition & 0 deletions src/ErrorHandlerGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function getErrorHandler(): callable
return function (Throwable $throwable, Inspector $inspector) {
if ($throwable instanceof HttpNotFoundException) {
http_response_code(404);
/** @phpstan-ignore-next-line shipmonk.checkedExceptionInCallable */
echo $this->twig->fetch('404.twig');
die;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Event/EventType/Obrok/EventTypeObrok.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function getTranslationFilePaths(): array
}

#[\Override]
public function getStylesheetNameWithoutLeadingSlash(): ?string
public function getStylesheetNameWithoutLeadingSlash(): string
{
return 'eventSpecificCss/stylesObrok.css';
}
Expand Down
1 change: 1 addition & 0 deletions src/FileHandler/LocalSaveFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function getFile(string $filename): File
{
$mimeContentType = mime_content_type($this->uploadFolder . $filename);
if ($mimeContentType === false) {
/** @phpstan-ignore shipmonk.variableTypeOverwritten */
$mimeContentType = 'unknown_mime_type';
}

Expand Down
6 changes: 5 additions & 1 deletion src/FileHandler/UploadFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public function resolveUploadedFile(Request $request): ?UploadedFile
$uploadedFile = $uploadedFiles['uploadFile'];

// check for too-big files
if ($uploadedFile->getSize() > 10_000_000) { // 10MB
$fileSize = $uploadedFile->getSize();
if ($fileSize === null) {
throw new \RuntimeException('Uploaded file size is null.');
}
if ($fileSize > 10_000_000) { // 10MB
$this->flashMessages->warning($this->translator->trans('flash.warning.fileTooBig'));

return null;
Expand Down
1 change: 1 addition & 0 deletions src/Import/ImportSrs.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function importIst(UploadedFile $istsDataFile, Event $event): void
$istsData = [];
try {
/** @var array<array<string,string>> $istsData */
/** @phpstan-ignore shipmonk.variableTypeOverwritten */
$istsData = $this->csvParser->parseCsv($istsDataFile);
} catch (\UnexpectedValueException | LeagueCsvException) {
$this->flashMessages->error($this->translator->trans('flash.error.importSrs.invalidCsv'));
Expand Down
2 changes: 2 additions & 0 deletions src/Orm/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ protected function trimNamespace(string $class): string

protected function toUnderScore(string $string): string
{
/** @phpstan-ignore shipmonk.unknownClosureParamType */
$pregReplaced = preg_replace_callback('#(?<=.)([A-Z])#', fn ($m) => '_' . strtolower($m[1]), $string);
if ($pregReplaced === null) {
throw new \RuntimeException('preg_replace_callback failed');
Expand All @@ -154,6 +155,7 @@ protected function toUnderScore(string $string): string

protected function toCamelCase(string $string): string
{
/** @phpstan-ignore shipmonk.unknownClosureParamType */
$pregReplaced = preg_replace_callback('#_(.)#', fn ($m) => strtoupper($m[1]), $string);
if ($pregReplaced === null) {
throw new \RuntimeException('preg_replace_callback failed');
Expand Down
2 changes: 1 addition & 1 deletion src/Participant/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function denyParticipant(

$this->flashMessages->info($this->translator->trans('flash.info.denied'));
$this->logger->info('Denied registration for participant with ID '
. $participantId . ' and role ' . $participant->role?->value . ' with reason: ' . $reason);
. $participantId . ' and role ' . ($participant->role?->value ?? 'missing') . ' with reason: ' . $reason);

return $this->redirect($request, $response, 'admin-show-approving');
}
Expand Down
17 changes: 13 additions & 4 deletions src/Participant/ParticipantRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ public function getParticipantsForEntry(Event $event): array
foreach($rows as $row) {
/** @var string $role */
$role = $row['role'];
$participants[$role][$row['id']] = $this->mapDataToEntryParticipant($row);
/** @var string $id */
$id = $row['id'];
$participants[$role][$id] = $this->mapDataToEntryParticipant($row);
}

$rowsDependableParticipants = $this->getRowsForEntryParticipant($event, [
Expand All @@ -390,15 +392,22 @@ public function getParticipantsForEntry(Event $event): array
]);

foreach ($rowsDependableParticipants as $rowDependableParticipant) {
$role = match ($rowDependableParticipant['role']) {
/** @var string $dpId */
$dpId = $rowDependableParticipant['id'];
/** @var string $dpRole */
$dpRole = $rowDependableParticipant['role'];
/** @var string $dpPatrolLeaderId */
$dpPatrolLeaderId = $rowDependableParticipant['patrol_leader_id'];

$role = match ($dpRole) {
ParticipantRole::TroopParticipant->value => ParticipantRole::TroopLeader->value,
ParticipantRole::PatrolParticipant->value => ParticipantRole::PatrolLeader->value,
default => 'unknown',
};

$leader = $participants[$role][$rowDependableParticipant['patrol_leader_id']] ?? null;
$leader = $participants[$role][$dpPatrolLeaderId] ?? null;
if ($leader instanceof EntryParticipant) {
$leader->participants[$rowDependableParticipant['id']]
$leader->participants[$dpId]
= $this->mapDataToEntryParticipant($rowDependableParticipant);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Payment/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function updatePayments(Event $event, int $limit = 10): void
/** @var BankPayment $bankPayment */
foreach (array_slice($freshBankPayments, 0, $limit) as $bankPayment) {
if (array_key_exists($bankPayment->variableSymbol ?? '', $participantKeydPayments)) {
$payment = $participantKeydPayments[$bankPayment->variableSymbol];
$payment = $participantKeydPayments[$bankPayment->variableSymbol ?? ''];
if ($payment->price === $bankPayment->price) {
// match!
$this->confirmPayment($payment);
Expand Down Expand Up @@ -246,7 +246,7 @@ public function calculatePaymentDueDate(
private function composeNote(Participant $participant, Event $event): string
{
if ($participant instanceof PatrolLeader) {
return $event->slug . ' ' . $participant->patrolName . ' ' . $participant->getFullName();
return $event->slug . ' ' . ($participant->patrolName ?? '') . ' ' . $participant->getFullName();
}

return $event->slug . ' ' . $participant->getFullName();
Expand Down
1 change: 1 addition & 0 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ public function getContainerDefinition(string $envPath, string $envFilename): ar
Translator $translator,
FlashMessagesBySession $flashMessages,
) {
/** @phpstan-ignore shipmonk.checkedExceptionInCallable */
$view = Twig::create(
[
__DIR__ . '/../Templates/translatable',
Expand Down
17 changes: 9 additions & 8 deletions src/Settings/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace kissj\Settings;

use kissj\Participant\Participant;
use kissj\Participant\Patrol\PatrolLeader;
use kissj\Participant\Patrol\PatrolParticipant;
use kissj\Participant\Troop\TroopLeader;
Expand All @@ -22,35 +23,35 @@ public function getTests(): array
return [
new TwigTest(
'PatrolLeader',
fn ($participant): bool => $participant instanceof PatrolLeader
fn (Participant $participant): bool => $participant instanceof PatrolLeader
),
new TwigTest(
'PatrolParticipant',
fn ($participant): bool => $participant instanceof PatrolParticipant
fn (Participant $participant): bool => $participant instanceof PatrolParticipant
),
new TwigTest(
'TroopLeader',
fn ($participant): bool => $participant instanceof TroopLeader
fn (Participant $participant): bool => $participant instanceof TroopLeader
),
new TwigTest(
'TroopParticipant',
fn ($participant): bool => $participant instanceof TroopParticipant
fn (Participant $participant): bool => $participant instanceof TroopParticipant
),
new TwigTest(
'Leader',
fn ($participant): bool => $participant instanceof PatrolLeader || $participant instanceof TroopLeader
fn (Participant $participant): bool => $participant instanceof PatrolLeader || $participant instanceof TroopLeader
),
new TwigTest(
'Troop',
fn ($participant): bool => $participant instanceof TroopLeader || $participant instanceof TroopParticipant
fn (Participant $participant): bool => $participant instanceof TroopLeader || $participant instanceof TroopParticipant
),
new TwigTest(
'hasUser',
fn ($participant): bool => !$participant instanceof PatrolParticipant
fn (Participant $participant): bool => !$participant instanceof PatrolParticipant
),
new TwigTest(
'eligibleForShowTieCode',
fn ($participant): bool => (
fn (Participant $participant): bool => (
$participant instanceof TroopLeader && $participant->getUserButNotNull()->status === UserStatus::Open
) || (
// TroopLeader TieCode is also used for pairing with some external services, so they have to be able to access it in paid status
Expand Down
2 changes: 2 additions & 0 deletions src/Skautis/SkautisController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function __construct(
*/
public function redirectFromSkautis(Request $request, Response $response): Response
{
// TODO fix into method $this->getParsedBody();
/** @phpstan-ignore shipmonk.forbiddenCast */
$this->skautisService->saveDataFromPost((array)$request->getParsedBody());

$eventSlug = $this->getParameterFromQuery($request, 'ReturnUrl');
Expand Down
12 changes: 11 additions & 1 deletion src/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,17 @@ public function getDashboard(User $user, Request $request, Response $response):

return match ($user->role) {
UserRole::Participant => $this->redirect($request, $response, 'dashboard', $routerEventSlug),
default => $this->redirect($request, $response, 'admin-dashboard', $routerEventSlug),
UserRole::Admin,
UserRole::IstAdmin,
UserRole::ContingentAdminCs,
UserRole::ContingentAdminSk,
UserRole::ContingentAdminPl,
UserRole::ContingentAdminHu,
UserRole::ContingentAdminEu,
UserRole::ContingentAdminRo,
UserRole::ContingentAdminGb,
UserRole::ContingentAdminSw,
=> $this->redirect($request, $response, 'admin-dashboard', $routerEventSlug),
};
}

Expand Down
45 changes: 39 additions & 6 deletions src/User/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,57 @@ public static function adminRoles(): array
public function isEligibleToHandlePayments(): bool
{
return match ($this) {
self::Admin, self::ContingentAdminCs, self::ContingentAdminSk => true,
default => false,
self::Admin,
self::ContingentAdminCs,
self::ContingentAdminSk
=> true,
self::Participant,
self::IstAdmin,
self::ContingentAdminPl,
self::ContingentAdminHu,
self::ContingentAdminEu,
self::ContingentAdminRo,
self::ContingentAdminGb,
self::ContingentAdminSw,
=> false,
};
}

public function isEligibleToManageTroops(): bool
{
return match ($this) {
self::Admin, self::ContingentAdminCs, self::ContingentAdminSk => true,
default => false,
self::Admin,
self::ContingentAdminCs,
self::ContingentAdminSk
=> true,
self::Participant,
self::IstAdmin,
self::ContingentAdminPl,
self::ContingentAdminHu,
self::ContingentAdminEu,
self::ContingentAdminRo,
self::ContingentAdminGb,
self::ContingentAdminSw,
=> false,
};
}

public function isEligibleToImportIst(): bool
{
return match ($this) {
self::Admin => true,
default => false,
self::Admin
=> true,
self::Participant,
self::IstAdmin,
self::ContingentAdminCs,
self::ContingentAdminSk,
self::ContingentAdminPl,
self::ContingentAdminHu,
self::ContingentAdminEu,
self::ContingentAdminRo,
self::ContingentAdminGb,
self::ContingentAdminSw,
=> false,
};
}
}
5 changes: 4 additions & 1 deletion src/User/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public function isLoginTokenValid(string $loginToken): bool
return false;
}

return $lastToken->createdAt > DateTimeUtils::getDateTime('now - 24 hours');
/** @var \DateTimeInterface $createdAt */
$createdAt = $lastToken->createdAt;

return $createdAt > DateTimeUtils::getDateTime('now - 24 hours');
}

public function getLoginTokenFromStringToken(string $token): LoginToken
Expand Down

0 comments on commit 6515954

Please sign in to comment.