Skip to content

Commit 7ce6a77

Browse files
committed
Add return types for PHPstan to detect issues
Verified by looking at the code or dumping one of the resulting objects. If we can have: array{0_string, ?, 1_string?: ?...} so array of length 3 but with strings as index we can further limit some of those findings. We remove the rank if you're not in the top x%, which leads to the empty string instead of rank number For the problems we have a copy of the overall array in one key as we allow different formats. This can be simplified.
1 parent b83960f commit 7ce6a77

File tree

5 files changed

+104
-143
lines changed

5 files changed

+104
-143
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ parameters:
6969
count: 1
7070
path: webapp/src/FosRestBundle/FlattenExceptionHandler.php
7171

72-
-
73-
74-
message: "#^Method App\\\\Service\\\\ExternalContestSourceService\\:\\:addOrUpdateWarning\\(\\) has parameter \\$content with no value type specified in iterable type array\\.$#"
75-
count: 1
76-
path: webapp/src/Service/ExternalContestSourceService.php
77-
7872
-
7973
message: "#^Method App\\\\Service\\\\ExternalContestSourceService\\:\\:addPendingEvent\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#"
8074
count: 1
@@ -190,127 +184,7 @@ parameters:
190184
count: 1
191185
path: webapp/src/Service/ExternalContestSourceService.php
192186

193-
-
194-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:getContestYamlData\\(\\) return type has no value type specified in iterable type array\\.$#"
195-
count: 1
196-
path: webapp/src/Service/ImportExportService.php
197-
198-
-
199-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:getDjRoles\\(\\) return type has no value type specified in iterable type array\\.$#"
200-
count: 1
201-
path: webapp/src/Service/ImportExportService.php
202-
203-
-
204-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:getGroupData\\(\\) return type has no value type specified in iterable type array\\.$#"
205-
count: 1
206-
path: webapp/src/Service/ImportExportService.php
207-
208-
-
209-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:getResultsData\\(\\) return type has no value type specified in iterable type array\\.$#"
210-
count: 1
211-
path: webapp/src/Service/ImportExportService.php
212-
213-
-
214-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:getTeamData\\(\\) return type has no value type specified in iterable type array\\.$#"
215-
count: 1
216-
path: webapp/src/Service/ImportExportService.php
217-
218-
-
219-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importAccountData\\(\\) has parameter \\$accountData with no value type specified in iterable type array\\.$#"
220-
count: 1
221-
path: webapp/src/Service/ImportExportService.php
222-
223-
-
224-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importAccountsJson\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#"
225-
count: 1
226-
path: webapp/src/Service/ImportExportService.php
227-
228-
-
229-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importAccountsTsv\\(\\) has parameter \\$content with no value type specified in iterable type array\\.$#"
230-
count: 1
231-
path: webapp/src/Service/ImportExportService.php
232-
233-
-
234-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importGroupData\\(\\) has parameter \\$groupData with no value type specified in iterable type array\\.$#"
235-
count: 1
236-
path: webapp/src/Service/ImportExportService.php
237-
238-
-
239-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importGroupsJson\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#"
240-
count: 1
241-
path: webapp/src/Service/ImportExportService.php
242-
243-
-
244-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importGroupsTsv\\(\\) has parameter \\$content with no value type specified in iterable type array\\.$#"
245-
count: 1
246-
path: webapp/src/Service/ImportExportService.php
247-
248-
-
249-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importOrganizationData\\(\\) has parameter \\$organizationData with no value type specified in iterable type array\\.$#"
250-
count: 1
251-
path: webapp/src/Service/ImportExportService.php
252-
253-
-
254-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importOrganizationsJson\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#"
255-
count: 1
256-
path: webapp/src/Service/ImportExportService.php
257-
258-
-
259-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importProblemsData\\(\\) has parameter \\$ids with no value type specified in iterable type array\\.$#"
260-
count: 1
261-
path: webapp/src/Service/ImportExportService.php
262-
263-
-
264-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importProblemsData\\(\\) has parameter \\$problems with no value type specified in iterable type array\\.$#"
265-
count: 1
266-
path: webapp/src/Service/ImportExportService.php
267-
268-
-
269-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importTeamData\\(\\) has parameter \\$teamData with no value type specified in iterable type array\\.$#"
270-
count: 1
271-
path: webapp/src/Service/ImportExportService.php
272-
273-
-
274-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importTeamsJson\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#"
275-
count: 1
276-
path: webapp/src/Service/ImportExportService.php
277-
278-
-
279-
message: "#^Method App\\\\Service\\\\ImportExportService\\:\\:importTeamsTsv\\(\\) has parameter \\$content with no value type specified in iterable type array\\.$#"
280-
count: 1
281-
path: webapp/src/Service/ImportExportService.php
282-
283-
-
284-
message: "#^Method App\\\\Service\\\\ScoreboardService\\:\\:getGroupedAffiliations\\(\\) return type has no value type specified in iterable type array\\.$#"
285-
count: 1
286-
path: webapp/src/Service/ScoreboardService.php
287-
288-
-
289-
message: "#^Method App\\\\Service\\\\ScoreboardService\\:\\:getScoreboardTwigData\\(\\) return type has no value type specified in iterable type array\\.$#"
290-
count: 1
291-
path: webapp/src/Service/ScoreboardService.php
292-
293187
-
294188
message: "#^Method App\\\\Service\\\\StatisticsService\\:\\:getGroupedProblemsStats\\(\\) return type has no value type specified in iterable type array\\.$#"
295189
count: 1
296190
path: webapp/src/Service/StatisticsService.php
297-
298-
-
299-
message: "#^Method App\\\\Service\\\\StatisticsService\\:\\:getMiscContestStatistics\\(\\) return type has no value type specified in iterable type array\\.$#"
300-
count: 1
301-
path: webapp/src/Service/StatisticsService.php
302-
303-
-
304-
message: "#^Method App\\\\Service\\\\StatisticsService\\:\\:getProblemStats\\(\\) return type has no value type specified in iterable type array\\.$#"
305-
count: 1
306-
path: webapp/src/Service/StatisticsService.php
307-
308-
-
309-
message: "#^Method App\\\\Service\\\\StatisticsService\\:\\:getTeamStats\\(\\) return type has no value type specified in iterable type array\\.$#"
310-
count: 1
311-
path: webapp/src/Service/StatisticsService.php
312-
313-
-
314-
message: "#^Method App\\\\Service\\\\StatisticsService\\:\\:setOrIncrement\\(\\) has parameter \\$array with no value type specified in iterable type array\\.$#"
315-
count: 1
316-
path: webapp/src/Service/StatisticsService.php

webapp/src/Service/ExternalContestSourceService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,9 @@ protected function warningIfUnsupported(string $operation, ?string $eventId, str
19481948
return true;
19491949
}
19501950

1951+
/**
1952+
* @param array<string, mixed> $content
1953+
*/
19511954
protected function addOrUpdateWarning(
19521955
?string $eventId,
19531956
string $entityType,

webapp/src/Service/ImportExportService.php

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public function __construct(
4242

4343
/**
4444
* Get the YAML data for a given contest.
45+
*
46+
* @return array<string, int|string|array<array{id: string, label: string, letter: string,
47+
* name: string, color: string, rgb: string}>> $contest
4548
*/
4649
public function getContestYamlData(Contest $contest, bool $includeProblems = true): array
4750
{
@@ -297,9 +300,17 @@ public function importContestData(mixed $data, ?string &$errorMessage = null, st
297300
return true;
298301
}
299302

303+
/**
304+
* @param array{name?: string, short-name?: string, id?: string, label?: string,
305+
* letter?: string, time_limit?: int, rgb?: string, color?: string,
306+
* problems?: array{name?: string, short-name?: string, id?: string, label?: string,
307+
* letter?: string, label?: string, letter?: string}} $problems
308+
* @param string[]|null $ids
309+
*/
300310
public function importProblemsData(Contest $contest, array $problems, array &$ids = null): bool
301311
{
302312
// For problemset.yaml the root key is called `problems`, so handle that case
313+
// TODO: Move this check away to make the $problems array shape easier
303314
if (isset($problems['problems'])) {
304315
$problems = $problems['problems'];
305316
}
@@ -338,6 +349,8 @@ public function importProblemsData(Contest $contest, array $problems, array &$id
338349

339350
/**
340351
* Get group data
352+
*
353+
* @return array<string[]>
341354
*/
342355
public function getGroupData(): array
343356
{
@@ -359,6 +372,8 @@ public function getGroupData(): array
359372

360373
/**
361374
* Get team data
375+
*
376+
* @return array<array<string|null>>
362377
*/
363378
public function getTeamData(): array
364379
{
@@ -390,19 +405,20 @@ public function getTeamData(): array
390405

391406
/**
392407
* Get results data for the given sortorder.
408+
*
409+
* We'll here assume that the requested file will be of the current contest,
410+
* as all our scoreboard interfaces do:
411+
* 0 ICPC ID 24314 string
412+
* 1 Rank in contest 1 integer|''
413+
* 2 Award Gold Medal string
414+
* 3 Number of problems the team has solved 4 integer
415+
* 4 Total Time 534 integer
416+
* 5 Time of the last submission 233 integer
417+
* 6 Group Winner North American string
418+
* @return array<array{0: string, 1: integer|string, 2: string, 3: integer, 4: integer, 5: integer, 6: string}>
393419
*/
394420
public function getResultsData(int $sortOrder, bool $full = false): array
395421
{
396-
// We'll here assume that the requested file will be of the current contest,
397-
// as all our scoreboard interfaces do:
398-
// 1 ICPC ID 24314 string
399-
// 2 Rank in contest 1 integer
400-
// 3 Award Gold Medal string
401-
// 4 Number of problems the team has solved 4 integer
402-
// 5 Total Time 534 integer
403-
// 6 Time of the last submission 233 integer
404-
// 7 Group Winner North American string
405-
406422
$contest = $this->dj->getCurrentContest();
407423
if ($contest === null) {
408424
throw new BadRequestHttpException('No current contest');
@@ -604,6 +620,8 @@ public function importJson(string $type, UploadedFile $file, ?string &$message =
604620

605621
/**
606622
* Import groups TSV.
623+
*
624+
* @param string[] $content
607625
*/
608626
protected function importGroupsTsv(array $content, ?string &$message = null): int
609627
{
@@ -623,6 +641,8 @@ protected function importGroupsTsv(array $content, ?string &$message = null): in
623641
/**
624642
* Import groups JSON
625643
*
644+
* @param array<array{id: string, icpc_id: string, name: string, sortorder?: int,
645+
* color?: string, hidden?: true, allow_self_registration?: bool}> $data
626646
* @param TeamCategory[]|null $saved The saved groups
627647
*/
628648
public function importGroupsJson(array $data, ?string &$message = null, ?array &$saved = null): int
@@ -646,6 +666,8 @@ public function importGroupsJson(array $data, ?string &$message = null, ?array &
646666
/**
647667
* Import group data from the given array
648668
*
669+
* @param array<array{categoryid: string, icpc_id?: string, name: string, visible?: bool,
670+
* sortorder?: int|null, color?: string|null, allow_self_registration: bool}> $groupData
649671
* @param TeamCategory[]|null $saved The saved groups
650672
*
651673
* @throws NonUniqueResultException
@@ -709,6 +731,9 @@ protected function importGroupData(array $groupData, ?array &$saved = null): int
709731
/**
710732
* Import organizations JSON.
711733
*
734+
* @param array<array{shortname?: string, short_name?: string, short-name?: string, id: string, icpc_id?: string, name: string, formal_name?: string,
735+
* country: string, logo: array{href: string, mime: string, hash: string,
736+
* filename: string, width: string|int, height: string|int}}> $data
712737
* @param TeamAffiliation[]|null $saved The saved groups
713738
*/
714739
public function importOrganizationsJson(array $data, ?string &$message = null, ?array &$saved = null): int
@@ -717,8 +742,8 @@ public function importOrganizationsJson(array $data, ?string &$message = null, ?
717742
foreach ($data as $organization) {
718743
$organizationData[] = [
719744
'externalid' => @$organization['id'],
720-
'shortname' => @$organization['short_name'] ?? @$organization['short-name'] ?? @$organization['shortname'] ?? @$organization['name'],
721-
'name' => @$organization['formal_name'] ?? @$organization['name'],
745+
'shortname' => $organization['short_name'] ?? $organization['short-name'] ?? $organization['shortname'] ?? $organization['name'],
746+
'name' => $organization['formal_name'] ?? $organization['name'],
722747
'country' => @$organization['country'],
723748
'icpc_id' => $organization['icpc_id'] ?? null,
724749
];
@@ -730,6 +755,7 @@ public function importOrganizationsJson(array $data, ?string &$message = null, ?
730755
/**
731756
* Import organization data from the given array.
732757
*
758+
* @param array<array{externalid: string, shortname?: string, icpc_id?: string, name: string, country: string}> $organizationData
733759
* @param TeamAffiliation[]|null $saved The saved groups
734760
*
735761
* @throws NonUniqueResultException
@@ -786,6 +812,8 @@ protected function importOrganizationData(array $organizationData, ?array &$save
786812

787813
/**
788814
* Import teams TSV
815+
*
816+
* @param string[] $content
789817
* @throws NonUniqueResultException
790818
*/
791819
protected function importTeamsTsv(array $content, ?string &$message = null): int
@@ -839,6 +867,9 @@ protected function importTeamsTsv(array $content, ?string &$message = null): int
839867
/**
840868
* Import teams JSON.
841869
*
870+
* @param array<array{label?: string, name?: string, organization_id?: string,
871+
* group_ids?: string[], icpc_id?: string, id?: string, display_name?: string,
872+
* location?: array{description: string}, members?: string, public_description?: string}> $data
842873
* @param Team[]|null $saved The saved teams
843874
*/
844875
public function importTeamsJson(array $data, ?string &$message = null, ?array &$saved = null): int
@@ -851,10 +882,10 @@ public function importTeamsJson(array $data, ?string &$message = null, ?array &$
851882
'icpcid' => $team['icpc_id'] ?? null,
852883
'label' => $team['label'] ?? null,
853884
'categoryid' => $team['group_ids'][0] ?? null,
854-
'name' => @$team['name'],
855-
'display_name' => @$team['display_name'],
856-
'publicdescription' => $team['public_description'] ?? @$team['members'],
857-
'location' => @$team['location']['description'],
885+
'name' => $team['name'] ?? '',
886+
'display_name' => $team['display_name'] ?? '',
887+
'publicdescription' => $team['public_description'] ?? $team['members'] ?? '',
888+
'location' => $team['location']['description'] ?? null,
858889
],
859890
'team_affiliation' => [
860891
'externalid' => $team['organization_id'] ?? null,
@@ -865,6 +896,9 @@ public function importTeamsJson(array $data, ?string &$message = null, ?array &$
865896
return $this->importTeamData($teamData, $message, $saved);
866897
}
867898

899+
/**
900+
* @return array<string, Role>
901+
*/
868902
private function getDjRoles(): array
869903
{
870904
$djRoles = [];
@@ -878,6 +912,8 @@ private function getDjRoles(): array
878912
/**
879913
* Import accounts JSON.
880914
*
915+
* @param array<array{id?: string, username?: string, name?: string, password?: string,
916+
* externalid?: string, type: string, team_id?: string, ip?: string}> $data
881917
* @param User[]|null $saved The saved users
882918
*/
883919
public function importAccountsJson(array $data, ?string &$message = null, ?array &$saved = null): int
@@ -952,6 +988,11 @@ public function importAccountsJson(array $data, ?string &$message = null, ?array
952988
/**
953989
* Import team data from the given array.
954990
*
991+
* @param array<array{team: array{teamid: string|null, icpcid: string|null, label?: string|null,
992+
* categoryid: string|null, name: string|null, display_name?: string,
993+
* publicdescription?: string, location?: string|null, affilid?: string},
994+
* team_affiliation: array{externalid: string|null, shortname?: string, name?: string,
995+
* country?: string}}> $teamData
955996
* @param Team[]|null $saved The saved teams
956997
*
957998
* @throws NonUniqueResultException
@@ -1088,7 +1129,12 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s
10881129
* Import account data from the given array.
10891130
*
10901131
* @param User[]|null $saved The saved users
1091-
*
1132+
* @param array<array{user: array{name: string|null, externalid: string, username: string,
1133+
* plain_password: string|null, teamid?: string|null,
1134+
* team?: array{name: string, category: string, externalid: string}|null,
1135+
* user_roles: Role[], ip_address: string|null},
1136+
* team?: array{name: string, externalid: string, category: TeamCategory,
1137+
* publicdescription?: string}}> $accountData
10921138
* @throws NonUniqueResultException
10931139
*/
10941140
protected function importAccountData(array $accountData, ?array &$saved = null): int
@@ -1181,6 +1227,8 @@ protected function importAccountData(array $accountData, ?array &$saved = null):
11811227

11821228
/**
11831229
* Import accounts TSV
1230+
*
1231+
* @param string[] $content
11841232
*/
11851233
protected function importAccountsTsv(array $content, ?string &$message = null): int
11861234
{

webapp/src/Service/ScoreboardService.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ public function initializeScoreboardFilter(Request $request, ?Response $response
745745

746746
/**
747747
* Get a list of affiliation names grouped on category name.
748+
*
749+
* @return array<array<string, array<array{id: string, name: string}>>>
748750
*/
749751
public function getGroupedAffiliations(Contest $contest): array
750752
{
@@ -863,6 +865,12 @@ public function getFilterValues(Contest $contest, bool $jury): array
863865

864866
/**
865867
* Get the scoreboard Twig data for a given contest.
868+
*
869+
* @return array{refresh: array{after: int, url: string, ajax: bool}, static: bool, contest: Contest,
870+
* scoreFilter: Filter, scoreboard: Scoreboard, filterValues: array<string, string[]>,
871+
* groupedAffiliations: null|TeamAffiliation[], showFlags: int, showAffiliationLogos: bool,
872+
* showAffiliations: int, showPending: int, showTeamSubmissions: int, scoreInSeconds: bool,
873+
* maxWidth: int, jury?: bool, public?: bool, ajax?: bool}
866874
*/
867875
public function getScoreboardTwigData(
868876
?Request $request,

0 commit comments

Comments
 (0)