Skip to content

Commit

Permalink
Add default null values to "current /something/" properties
Browse files Browse the repository at this point in the history
These will always be initialized in the `action*()` methods and so will always be available in the `createComponent*()` methods. But Psalm can't possibly know that. Still not sure I like it this way, it's there mostly for Psalm only but maybe it's ok.

Resolves errors like
```
ERROR: PropertyNotSetInConstructor - app/Admin/Presenters/TrainingsPresenter.php:44:30 - Property MichalSpacekCz\Admin\Presenters\TrainingsPresenter::$application is not defined in constructor of MichalSpacekCz\Admin\Presenters\TrainingsPresenter or in any private or final methods called in the constructor (see https://psalm.dev/074)
        private TrainingApplication $application;
```
  • Loading branch information
spaze committed Sep 3, 2023
1 parent 6b5c199 commit 29cf163
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
6 changes: 5 additions & 1 deletion site/app/Admin/Presenters/InterviewsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
use MichalSpacekCz\Interviews\InterviewInputs;
use MichalSpacekCz\Interviews\InterviewInputsFactory;
use MichalSpacekCz\Interviews\Interviews;
use MichalSpacekCz\ShouldNotHappenException;
use Nette\Application\BadRequestException;

class InterviewsPresenter extends BasePresenter
{

private Interview $interview;
private ?Interview $interview = null;


public function __construct(
Expand Down Expand Up @@ -49,6 +50,9 @@ public function actionInterview(int $param): void

protected function createComponentEditInterviewInputs(): InterviewInputs
{
if (!$this->interview) {
throw new ShouldNotHappenException('actionInterview() will be called first');
}
return $this->interviewInputsFactory->createFor($this->interview);
}

Expand Down
9 changes: 8 additions & 1 deletion site/app/Admin/Presenters/TalksPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MichalSpacekCz\Form\TalkSlidesFormFactory;
use MichalSpacekCz\Http\HttpInput;
use MichalSpacekCz\Media\Exceptions\ContentTypeException;
use MichalSpacekCz\ShouldNotHappenException;
use MichalSpacekCz\Talks\Exceptions\TalkDoesNotExistException;
use MichalSpacekCz\Talks\Talk;
use MichalSpacekCz\Talks\TalkInputs;
Expand All @@ -21,7 +22,7 @@
class TalksPresenter extends BasePresenter
{

private Talk $talk;
private ?Talk $talk = null;

/** @var array<int, Row> slide number => data */
private array $slides = [];
Expand Down Expand Up @@ -86,6 +87,9 @@ public function actionSlides(int $param): void

protected function createComponentEditTalkInputs(): TalkInputs
{
if (!$this->talk) {
throw new ShouldNotHappenException('actionTalk() will be called first');
}
return $this->talkInputsFactory->createFor($this->talk);
}

Expand All @@ -98,6 +102,9 @@ protected function createComponentAddTalkInputs(): TalkInputs

protected function createComponentSlides(): Form
{
if (!$this->talk) {
throw new ShouldNotHappenException('actionSlides() will be called first');
}
return $this->talkSlidesFormFactory->create(
function (Html $message, string $type, int $talkId): never {
$this->flashMessage($message, $type);
Expand Down
25 changes: 22 additions & 3 deletions site/app/Admin/Presenters/TrainingsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MichalSpacekCz\Form\TrainingApplicationMultipleFormFactory;
use MichalSpacekCz\Form\TrainingFileFormFactory;
use MichalSpacekCz\Form\TrainingStatusesFormFactory;
use MichalSpacekCz\ShouldNotHappenException;
use MichalSpacekCz\Training\Applications\TrainingApplication;
use MichalSpacekCz\Training\Applications\TrainingApplications;
use MichalSpacekCz\Training\DateList\DateListOrder;
Expand Down Expand Up @@ -41,11 +42,11 @@ class TrainingsPresenter extends BasePresenter
/** @var int[] */
private array $applicationIdsAllowedFiles = [];

private TrainingApplication $application;
private ?TrainingApplication $application = null;

private TrainingReview $review;
private ?TrainingReview $review = null;

private TrainingDate $training;
private ?TrainingDate $training = null;

/** @var list<TrainingDate> */
private array $pastWithPersonalData = [];
Expand Down Expand Up @@ -243,6 +244,9 @@ function (?Html $message): never {

protected function createComponentApplications(): Form
{
if (!$this->training) {
throw new ShouldNotHappenException('actionDate() will be called first');
}
return $this->trainingApplicationMultipleFormFactory->create(
function (int $dateId): never {
$this->redirect($this->getAction(), $dateId);
Expand All @@ -254,6 +258,9 @@ function (int $dateId): never {

protected function createComponentApplicationForm(): Form
{
if (!$this->application) {
throw new ShouldNotHappenException('actionApplication() will be called first');
}
return $this->trainingApplicationAdminFactory->create(
function (?int $dateId): never {
if ($dateId) {
Expand All @@ -272,6 +279,9 @@ function (): never {

protected function createComponentFile(): Form
{
if (!$this->training) {
throw new ShouldNotHappenException('actionDate() or actionFiles() will be called first');
}
return $this->trainingFileFormFactory->create(
function (Html|string $message, string $type): never {
$this->flashMessage($message, $type);
Expand All @@ -285,6 +295,9 @@ function (Html|string $message, string $type): never {

protected function createComponentEditTrainingDateInputs(): TrainingDateInputs
{
if (!$this->training) {
throw new ShouldNotHappenException('actionDate() will be called first');
}
return $this->trainingDateInputsFactory->createFor($this->training);
}

Expand Down Expand Up @@ -321,12 +334,18 @@ protected function createComponentPastWithPersonalDataTrainingApplicationsList()

protected function createComponentEditReviewInputs(): TrainingReviewInputs
{
if (!$this->review) {
throw new ShouldNotHappenException('actionReview() will be called first');
}
return $this->trainingReviewInputsFactory->create(false, $this->review->getDateId(), $this->review);
}


protected function createComponentAddReviewInputs(): TrainingReviewInputs
{
if (!$this->training) {
throw new ShouldNotHappenException('actionDate() will be called first');
}
return $this->trainingReviewInputsFactory->create(true, $this->training->getId());
}

Expand Down
20 changes: 13 additions & 7 deletions site/app/Www/Presenters/TrainingsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class TrainingsPresenter extends BasePresenter
{

private Training $training;
private ?Training $training = null;

/** @var array<int, TrainingDate> id => date */
private array $dates = [];
Expand Down Expand Up @@ -86,22 +86,22 @@ public function actionTraining(string $name): void
}
$this->training = $training;

$this->redirectToSuccessor($this->training->getSuccessorId());
$this->redirectToSuccessor($training->getSuccessorId());

$this->dates = $this->trainingDates->getDates($this->training->getId());
$this->dates = $this->trainingDates->getDates($training->getId());

$session = $this->getSession();
$session->start(); // in createComponentApplication() it's too late as the session cookie cannot be set because the output is already sent

$this->template->pageTitle = $this->texyFormatter->translate('messages.title.training', [$this->training->getName()->render()]);
$this->template->training = $this->training;
$this->template->pageTitle = $this->texyFormatter->translate('messages.title.training', [$training->getName()->render()]);
$this->template->training = $training;
$this->template->lastFreeSeats = $this->freeSeats->lastFreeSeatsAnyDate($this->dates);
$this->template->dates = $this->dates;
$this->template->singleDate = count($this->dates) === 1 ? $this->trainingDates->formatDateVenueForUser(reset($this->dates)) : null;
$this->template->dataRetention = $this->trainingDates->getDataRetentionDays();
$this->template->reviews = $this->trainingReviews->getVisibleReviews($this->training->getId(), 3);
$this->template->reviews = $this->trainingReviews->getVisibleReviews($training->getId(), 3);
$this->template->loadCompanyDataVisible = $this->companyInfo->isLoadCompanyDataVisible();
$this->discontinuedTrainings->maybeMarkAsDiscontinued($this->template, $this->training->getDiscontinuedId());
$this->discontinuedTrainings->maybeMarkAsDiscontinued($this->template, $training->getDiscontinuedId());
}


Expand Down Expand Up @@ -131,6 +131,9 @@ public function actionApplication(string $name, ?string $param): void

protected function createComponentApplication(): Form
{
if (!$this->training) {
throw new ShouldNotHappenException('actionTraining() or actionSuccess() will be called first');
}
return $this->trainingApplicationFactory->create(
function (string $name): never {
$this->redirect('success', $name);
Expand All @@ -148,6 +151,9 @@ function (string $message): void {

protected function createComponentApplicationPreliminary(): Form
{
if (!$this->training) {
throw new ShouldNotHappenException('actionTraining() will be called first');
}
if ($this->training->getDiscontinuedId()) {
throw new BadRequestException("No signups for discontinued trainings id {$this->training->getDiscontinuedId()}");
}
Expand Down
6 changes: 5 additions & 1 deletion site/app/Www/Presenters/VenuesPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MichalSpacekCz\Www\Presenters;

use MichalSpacekCz\Formatter\TexyFormatter;
use MichalSpacekCz\ShouldNotHappenException;
use MichalSpacekCz\Training\DateList\UpcomingTrainingDatesList;
use MichalSpacekCz\Training\DateList\UpcomingTrainingDatesListFactory;
use MichalSpacekCz\Training\Exceptions\TrainingVenueNotFoundException;
Expand All @@ -13,7 +14,7 @@
class VenuesPresenter extends BasePresenter
{

private UpcomingTrainingDatesList $upcomingTrainingDatesList;
private ?UpcomingTrainingDatesList $upcomingTrainingDatesList = null;


public function __construct(
Expand Down Expand Up @@ -42,6 +43,9 @@ public function actionVenue(string $name): void

protected function createComponentUpcomingDatesList(): UpcomingTrainingDatesList
{
if (!$this->upcomingTrainingDatesList) {
throw new ShouldNotHappenException('actionVenue() will be called first');
}
return $this->upcomingTrainingDatesList;
}

Expand Down

0 comments on commit 29cf163

Please sign in to comment.