Skip to content

Commit

Permalink
Fix some of the newly introduced PHPStan errors
Browse files Browse the repository at this point in the history
With the introduction of PHPStan scanning the dev folder a lot of errors were added to the baseline.

I've taken a short time to look at the newly introduced errors and fixed some of them.
  • Loading branch information
mharte-ib authored and MKodde committed Sep 11, 2024
1 parent 70988c9 commit 384732b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 54 deletions.
46 changes: 3 additions & 43 deletions ci/qa/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,15 @@ parameters:
path: ../../dev/Command/AuthenticationCommand.php

-
message: "#^Cannot access offset string on mixed\\.$#"
count: 2
path: ../../dev/Command/AuthenticationCommand.php

-
message: "#^Comparison operation \"\\>\\=\" between int\\<0, max\\>\\|false and 0 is always true\\.$#"
count: 1
path: ../../dev/Command/AuthenticationCommand.php

-
message: "#^Method Surfnet\\\\Tiqr\\\\Dev\\\\Command\\\\AuthenticationCommand\\:\\:decorateResult\\(\\) has parameter \\$text with no type specified\\.$#"
message: "#^Cannot access offset int\\|string\\|false on mixed\\.$#"
count: 1
path: ../../dev/Command/AuthenticationCommand.php

-
message: "#^Method Surfnet\\\\Tiqr\\\\Dev\\\\Command\\\\AuthenticationCommand\\:\\:readAuthenticationLinkFromFile\\(\\) should return string but returns mixed\\.$#"
message: "#^Cannot access offset string on mixed\\.$#"
count: 1
path: ../../dev/Command/AuthenticationCommand.php

-
message: "#^Method Symfony\\\\Component\\\\Console\\\\Input\\\\InputInterface\\:\\:getOption\\(\\) invoked with 2 parameters, 1 required\\.$#"
count: 2
path: ../../dev/Command/AuthenticationCommand.php

-
message: "#^Parameter \\#1 \\$array of function array_keys expects array, mixed given\\.$#"
count: 1
Expand Down Expand Up @@ -85,11 +70,6 @@ parameters:
count: 1
path: ../../dev/Command/AuthenticationCommand.php

-
message: "#^Strict comparison using \\=\\=\\= between string and null will always evaluate to false\\.$#"
count: 1
path: ../../dev/Command/AuthenticationCommand.php

-
message: "#^Cannot access offset 'authenticationUrl' on mixed\\.$#"
count: 1
Expand All @@ -115,11 +95,6 @@ parameters:
count: 2
path: ../../dev/Command/RegistrationCommand.php

-
message: "#^Method Surfnet\\\\Tiqr\\\\Dev\\\\Command\\\\RegistrationCommand\\:\\:decorateResult\\(\\) has parameter \\$text with no type specified\\.$#"
count: 1
path: ../../dev/Command/RegistrationCommand.php

-
message: "#^Method Surfnet\\\\Tiqr\\\\Dev\\\\Command\\\\RegistrationCommand\\:\\:storeIdentity\\(\\) has parameter \\$metadata with no type specified\\.$#"
count: 1
Expand Down Expand Up @@ -215,25 +190,10 @@ parameters:
count: 1
path: ../../dev/FileLogger.php

-
message: "#^Parameter \\#1 \\$stream of function fclose expects resource, resource\\|false given\\.$#"
count: 1
path: ../../dev/FileLogger.php

-
message: "#^Parameter \\#1 \\$stream of static method League\\\\Csv\\\\AbstractCsv\\:\\:createFromStream\\(\\) expects resource, resource\\|false given\\.$#"
count: 2
path: ../../dev/FileLogger.php

-
message: "#^Cannot call method getEntityId\\(\\) on Surfnet\\\\SamlBundle\\\\Entity\\\\IdentityProvider\\|null\\.$#"
count: 1
path: ../../dev/Twig/GsspExtension.php

-
message: "#^Parameter \\#1 \\$string of function urlencode expects string, string\\|null given\\.$#"
count: 1
path: ../../dev/Twig/GsspExtension.php
path: ../../dev/FileLogger.php

-
message: "#^Parameter \\#3 \\$response of method Surfnet\\\\Tiqr\\\\Tiqr\\\\AuthenticationRateLimitServiceInterface\\:\\:authenticate\\(\\) expects string, mixed given\\.$#"
Expand Down
18 changes: 11 additions & 7 deletions dev/Command/AuthenticationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
[$serviceId, $session, $challenge, $sp, $version] = explode('/', $authn);

$userId = null;
if (strpos($serviceId, '@') >= 0) {
if (str_contains($serviceId, '@')) {
[$userId, $serviceId] = explode('@', $serviceId);
}

Expand All @@ -101,7 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'sp' => $sp,
'version' => $version,
'userId' => $userId,
], JSON_PRETTY_PRINT)),
], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)),
]);

$service = $this->getService($serviceId);
Expand All @@ -126,7 +126,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$service['id'] = $serviceId;
$output->writeln([
'<comment>Generate OCRA for service:</comment>',
$this->decorateResult(json_encode($service, JSON_PRETTY_PRINT)),
$this->decorateResult(json_encode($service, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)),
]);

$response = OCRA::generateOCRA($ocraSuite, $secret, '', $challenge, '', $session, '');
Expand All @@ -143,16 +143,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'sessionKey' => $session,
'userId' => $userId,
'response' => $response,
'notificationType' => $input->getOption('notificationType', ''),
'notificationAddress' => $input->getOption('notificationAddress', ''),
'notificationType' => $input->getOption('notificationType'),
'notificationAddress' => $input->getOption('notificationAddress'),
];

$output->writeln([
sprintf(
'<comment>Send authentication data to "%s" with body:</comment>',
$authenticationUrl
),
$this->decorateResult(json_encode($authenticationBody, JSON_PRETTY_PRINT)),
$this->decorateResult(json_encode($authenticationBody, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)),
]);

$result = $this->client->post($authenticationUrl, [
Expand All @@ -166,7 +166,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

protected function decorateResult($text): string
protected function decorateResult(string $text): string
{
return "<options=bold>$text</>";
}
Expand All @@ -177,6 +177,10 @@ protected function readAuthenticationLinkFromFile(string $file, OutputInterface
/** @phpstan-var mixed $link */
$link = $qrcode->text();

if (!is_string($link)) {
throw new RuntimeException('Unable to read a link from the QR code');
}

$output->writeln([
'Registration link result: ',
$this->decorateResult($link),
Expand Down
6 changes: 3 additions & 3 deletions dev/Command/RegistrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$metadata = json_decode($metadataBody);
$output->writeln([
'Metadata result:',
$this->decorateResult(json_encode($metadata, JSON_PRETTY_PRINT)),
$this->decorateResult(json_encode($metadata, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)),
]);
if ($metadata === false) {
$output->writeln('<error>Metadata has expired</error>');
Expand All @@ -99,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'<comment>Send registration data to enrollmentUrl "%s" with body:</comment>',
$metadata->service->enrollmentUrl
),
$this->decorateResult(json_encode($registrationBody, JSON_PRETTY_PRINT)),
$this->decorateResult(json_encode($registrationBody, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)),
]);

$result = $this->client->post($metadata->service->enrollmentUrl, ['form_params' => $registrationBody]);
Expand All @@ -121,7 +121,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

protected function decorateResult($text): string
protected function decorateResult(string $text): string
{
return "<options=bold>$text</>";
}
Expand Down
4 changes: 4 additions & 0 deletions dev/FileLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function log($level, $message, array $context = []): void
return;
}
$file = fopen($this->getCSVFile(), 'ab+');
if (!$file) {
return;
}

$csv = Writer::createFromStream($file);
$csv->setDelimiter(';');
$csv->insertOne([$level, $message, json_encode($context)]);
Expand Down
2 changes: 1 addition & 1 deletion dev/Twig/GsspExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function generateDemoSPUrl(): string
{
return sprintf(
'https://pieter.aai.surfnet.nl/simplesamlphp/sp.php?idp=%s',
urlencode($this->hostedEntities->getIdentityProvider()->getEntityId())
urlencode($this->hostedEntities->getIdentityProvider()?->getEntityId() ?? '')
);
}
}

0 comments on commit 384732b

Please sign in to comment.