Skip to content

Commit

Permalink
PHPStan Latte level 0 (#80)
Browse files Browse the repository at this point in the history
Increase PHPStan Latte config level to 0 (from _nothing_) to get more
errors. Some code changes that were initially targeted at level 1 (#71)
but they can be as well part of this PR, though I'm not sure whether
these changes are really needed for level 0 already. They may be needed
for level 1 but whatever.

Close #79
  • Loading branch information
spaze authored Jan 29, 2023
2 parents 93f4e4e + 5fed065 commit 01d1fc3
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{define #listItems}
<tr class="summary">
<tr class="summary" n:foreach="$applications as $application">
<td><small>{$iterator->getCounter()}.</small></td>
<td><small n:tag-if="$application->name === null"><a n:href="Trainings:application $application->id">{$application->name ?? smazáno}</a></small></td>
<td><small>{$application->email ?? smazáno}</small></td>
Expand Down Expand Up @@ -78,8 +78,8 @@
<th><small>Status</small></th>
<th><small title="Cena bez DPH">Cena</small></th>
</thead>
<tbody n:if="$training->applications" n:inner-foreach="$training->applications as $application">{include #listItems}</tbody>
<tbody n:if="$training->canceledApplications" class="discarded" n:inner-foreach="$training->canceledApplications as $application">{include #listItems}</tbody>
<tbody n:if="$training->applications">{include #listItems, applications: $training->applications}</tbody>
<tbody n:if="$training->canceledApplications" class="discarded">{include #listItems, applications: $training->canceledApplications}</tbody>
</table>
{else}
<span class="indent"><small>{if $training->public}Žádní přihlášení účastníci{else}Neznámý počet účastníků firemního školení{/if}</small></span>
Expand Down
15 changes: 11 additions & 4 deletions site/app/Pulse/Presenters/PasswordsStoragesPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ public function actionSite(string $param): void
throw new BadRequestException('Unknown site alias');
}

$this->template->isDetail = true;
$this->template->pageTitle = implode(', ', $sites) . ' password storage disclosures';
$this->template->data = $data;
$this->template->ratingGuide = $this->passwordsRating->getRatingGuide();
$this->setDetailDefaultTemplateVars();
$this->setView('default');
}

Expand All @@ -98,14 +97,22 @@ public function actionCompany(string $param): void
$names[] = $item->getDisplayName();
}

$this->template->isDetail = true;
$this->template->pageTitle = implode(', ', $names) . ' password storage disclosures';
$this->template->data = $data;
$this->template->ratingGuide = $this->passwordsRating->getRatingGuide();
$this->setDetailDefaultTemplateVars();
$this->setView('default');
}


private function setDetailDefaultTemplateVars(): void
{
$this->template->isDetail = true;
$this->template->ratingGuide = $this->passwordsRating->getRatingGuide();
$this->template->openSearchSort = false;
$this->template->canonicalLink = null;
}


/**
* Storages rating action handler.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class PasswordsStoragesDefaultTemplate extends Template

public bool $openSearchSort;

public string $canonicalLink;
public ?string $canonicalLink;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{/define}

{define #menuRight}
<a n:ifset="$openSearchSort" href="" n:class="open-button, $openSearchSort ? open" data-open="#search-sort">Search & sort</a>
<a n:if="!$isDetail" href="" n:class="open-button, $openSearchSort ? open" data-open="#search-sort">Search & sort</a>
{/define}

{define #metas}
Expand All @@ -25,7 +25,7 @@
{/define}

{define #content}
<div n:ifset="$openSearchSort" id="search-sort" n:class="inner-container, !$openSearchSort ? hidden">
<div n:if="!$isDetail" id="search-sort" n:class="inner-container, !$openSearchSort ? hidden">
<form n:name="searchSort">
<div class="left">{label search /} {input search}</div>
<div class="left">{label sort /} {input sort}</div>
Expand Down
61 changes: 23 additions & 38 deletions site/app/UpcKeys/Presenters/HomepagePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use MichalSpacekCz\Form\UpcKeysSsidFormFactory;
use MichalSpacekCz\UpcKeys\UpcKeys;
use MichalSpacekCz\UpcKeys\WiFiBand;
use MichalSpacekCz\UpcKeys\WiFiKey;
use MichalSpacekCz\Www\Presenters\BasePresenter;
use Nette\Application\BadRequestException;
use Nette\Forms\Form;
Expand Down Expand Up @@ -38,18 +37,29 @@ public function __construct(
public function actionDefault(?string $ssid = null, string $format = 'html'): void
{
$this->ssid = $ssid;
$keys = $this->loadKeys();
if ($this->ssid !== null) {
if ($this->upcKeys->isValidSsid($this->ssid)) {
if ($this->ssid !== strtoupper($this->ssid)) {
$this->redirect('this', strtoupper($this->ssid));
}
$keys = $this->upcKeys->getKeys($this->ssid);
if (!$keys) {
$error = 'Oops, something went wrong, please try again in a moment';
}
} else {
$error = 'Wi-Fi network name is not "UPC" and 7 numbers, the password cannot be recovered by this tool';
$this->httpResponse->setCode(IResponse::S404_NotFound);
}
}

$this->template->ssid = $this->ssid;
$this->template->error = $error ?? null;
$this->template->keys = isset($keys) && !isset($error) ? $keys : null;
switch ($format) {
case 'text':
foreach ($keys as $key => $value) {
$this->template->$key = $value;
}
$this->setView('text');
break;
case 'html':
foreach ($keys as $key => $value) {
$this->template->$key = $value;
}
$this->template->filterTypes = array_map(function (WiFiBand $band): string {
return $band->getLabel();
}, WiFiBand::getKnown());
Expand All @@ -58,43 +68,18 @@ public function actionDefault(?string $ssid = null, string $format = 'html'): vo
$this->template->placeholder = $this->upcKeys->getSsidPlaceholder();
break;
case 'json':
$this->sendJson($keys);
$this->sendJson(array_filter([
'ssid' => $this->ssid,
'error' => $error ?? null,
'keys' => $keys ?? null,
]));
// no break, Presenter::sendJson() is in earlyTerminatingMethodCalls defined in the phpstan-nette extension config
default:
throw new BadRequestException('Unknown format');
}
}


/**
* Check if SSID is valid and load keys
*
* @return array{}|array{ssid: string, error: string}|array{ssid: string, keys: non-empty-array<int, WiFiKey>}
*/
protected function loadKeys(): array
{
$result = [];
if ($this->ssid !== null) {
$result['ssid'] = $this->ssid;
if ($this->upcKeys->isValidSsid($this->ssid)) {
if ($this->ssid !== strtoupper($this->ssid)) {
$this->redirect('this', strtoupper($this->ssid));
}
$keys = $this->upcKeys->getKeys($this->ssid);
if (!$keys) {
$result['error'] = 'Oops, something went wrong, please try again in a moment';
} else {
$result['keys'] = $keys;
}
} else {
$result['error'] = 'Wi-Fi network name is not "UPC" and 7 numbers, the password cannot be recovered by this tool';
$this->httpResponse->setCode(IResponse::S404_NotFound);
}
}
return $result;
}


protected function createComponentSsid(): Form
{
return $this->upcKeysSsidFormFactory->create(
Expand Down
2 changes: 2 additions & 0 deletions site/app/UpcKeys/Presenters/templates/Homepage/text.latte
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{contentType text/plain}
{ifset $ssid}
# {$ssid}
{/ifset}
{ifset $error}
# Error: {$error}
{/ifset}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div n:if="isset($lastFreeSeats) && $lastFreeSeats" class="flash notice"><strong>{_messages.trainings.fewseatsleft}</strong></div>
<div n:if="$lastFreeSeats" class="flash notice"><strong>{_messages.trainings.fewseatsleft}</strong></div>
<ul class="vcalendar" id="training-dates">
{foreach $upcomingTrainings as $item}
<li class="vevent">
Expand Down
2 changes: 1 addition & 1 deletion site/phpstan-latte-templates.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
parameters:
customRulesetUsed: true
tmpDir: /tmp/phpstan/michalspacek.cz-latte-templates
paths:
- app
level: 0
latte:
engineBootstrap: app/PhpStan/latteEngine.php
applicationMapping: # Copy of mapping from common.neon until https://github.com/efabrica-team/phpstan-latte/issues/27 is resolved somehow
Expand Down

0 comments on commit 01d1fc3

Please sign in to comment.