Skip to content

Commit

Permalink
Fix PHPStan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
colagrosso committed May 21, 2024
1 parent 1e2dfc9 commit 406eff5
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/Ebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ public function __construct(?string $wwwFilesystemPath = null){
// METHODS
// *******

/**
* @throws \Exception
* @throws \Exceptions\ValidationException
*/
public function Validate(): void{

This comment has been minimized.

Copy link
@acabal

acabal May 23, 2024

Member

If you added \Exception because PHPStancalims that DateTimeImmutable throws an exception, then this is actually a bug in Safe: thecodingmachine/safe#444 You can use the workaround there instead of adding this generic exception annotation.

$now = new DateTimeImmutable();
$error = new Exceptions\ValidationException();
Expand Down Expand Up @@ -739,6 +743,9 @@ public function Validate(): void{
}
}

/**
* @throws \Exception

This comment has been minimized.

Copy link
@acabal

acabal May 23, 2024

Member

When annotating exceptions, use the actual specific exception that's thrown. A generic exception is not useful to annotate because if we miss a generic exception then that's almost always a fatal error that needs to stop the script and be logged internally.

*/
public function CreateOrUpdate(): void{
try{
$existingEbook = Ebook::GetByIdentifier($this->Identifier);
Expand All @@ -750,6 +757,9 @@ public function CreateOrUpdate(): void{
}
}

/**
* @throws \Exceptions\ValidationException
*/
private function InsertTagStrings(): void{
$tags = [];
foreach($this->Tags as $ebookTag){
Expand All @@ -758,6 +768,9 @@ private function InsertTagStrings(): void{
$this->Tags = $tags;
}

/**
* @throws \Exceptions\ValidationException
*/
private function InsertLocSubjectStrings(): void{
$subjects = [];
foreach($this->LocSubjects as $locSubject){
Expand Down Expand Up @@ -1089,6 +1102,9 @@ public static function GetByIdentifier(?string $identifier): Ebook{
return $result[0];
}

/**
* @throws \Exception
*/
public function Create(): void{
$this->Validate();

Expand Down Expand Up @@ -1140,6 +1156,9 @@ public function Create(): void{
$this->InsertTocEntries();
}

/**
* @throws \Exception
*/
public function Save(): void{
$this->Validate();

Expand Down
10 changes: 10 additions & 0 deletions lib/EbookTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ protected function GetUrl(): string{
// *******
// METHODS
// *******

/**
* @throws \Exceptions\ValidationException
*/
public function Validate(): void{
$error = new Exceptions\ValidationException();

Expand All @@ -34,6 +38,9 @@ public function Validate(): void{
}
}

/**
* @throws \Exceptions\ValidationException
*/
public function Create(): void{
$this->Validate();

Expand All @@ -44,6 +51,9 @@ public function Create(): void{
$this->TagId = Db::GetLastInsertedId();
}

/**
* @throws \Exceptions\ValidationException
*/
public function GetByNameOrCreate(string $name): EbookTag{
$result = Db::Query('
SELECT *
Expand Down
1 change: 1 addition & 0 deletions lib/Exceptions/EbookDescriptionRequiredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
namespace Exceptions;

class EbookDescriptionRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook Description required.';
}
1 change: 1 addition & 0 deletions lib/Exceptions/EbookIdentifierRequiredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
namespace Exceptions;

class EbookIdentifierRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook Identifier required.';
}
1 change: 1 addition & 0 deletions lib/Exceptions/EbookIndexableTextRequiredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
namespace Exceptions;

class EbookIndexableTextRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook IndexableText required.';
}
1 change: 1 addition & 0 deletions lib/Exceptions/EbookLongDescriptionRequiredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
namespace Exceptions;

class EbookLongDescriptionRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook LongDescription required.';
}
1 change: 1 addition & 0 deletions lib/Exceptions/EbookTitleRequiredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
namespace Exceptions;

class EbookTitleRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook Title required.';
}
4 changes: 4 additions & 0 deletions lib/Exceptions/InvalidEbookCreatedDatetimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
use Safe\DateTimeImmutable;

class InvalidEbookCreatedDatetimeException extends AppException{
/** @var string $message */
protected $message = 'Invalid EbookCreated datetime.';

/**
* @throws \Exception
*/
public function __construct(DateTimeImmutable $createdDatetime){
$now = new DateTimeImmutable();
$this->message = 'Invalid EbookCreated datetime. ' . $createdDatetime->format('Y-m-d') . ' is not between ' . EBOOK_EARLIEST_CREATION_DATE->format('Y-m-d') . ' and ' . $now->format('Y-m-d') . '.';
Expand Down
1 change: 1 addition & 0 deletions lib/Exceptions/InvalidEbookRepoFilesystemPathException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Safe\DateTimeImmutable;

class InvalidEbookRepoFilesystemPathException extends AppException{
/** @var string $message */
protected $message = 'Invalid RepoFilesystemPath.';

public function __construct(?string $path){
Expand Down
4 changes: 4 additions & 0 deletions lib/Exceptions/InvalidEbookUpdatedDatetimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
use Safe\DateTimeImmutable;

class InvalidEbookUpdatedDatetimeException extends AppException{
/** @var string $message */
protected $message = 'Invalid EbookUpdated datetime.';

/**
* @throws \Exception
*/
public function __construct(DateTimeImmutable $updatedDatetime){
$now = new DateTimeImmutable();
$this->message = 'Invalid EbookUpdated datetime. ' . $updatedDatetime->format('Y-m-d') . ' is not between ' . EBOOK_EARLIEST_CREATION_DATE->format('Y-m-d') . ' and ' . $now->format('Y-m-d') . '.';
Expand Down
1 change: 1 addition & 0 deletions lib/Exceptions/InvalidEbookWwwFilesystemPathException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Safe\DateTimeImmutable;

class InvalidEbookWwwFilesystemPathException extends AppException{
/** @var string $message */
protected $message = 'Invalid WwwFilesystemPath.';

public function __construct(?string $path){
Expand Down
9 changes: 9 additions & 0 deletions lib/LocSubject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
class LocSubject extends Tag{
public int $LocSubjectId;

/**
* @throws \Exceptions\ValidationException
*/
public function Validate(): void{
$error = new Exceptions\ValidationException();

Expand All @@ -14,6 +17,9 @@ public function Validate(): void{
}
}

/**
* @throws \Exceptions\ValidationException
*/
public function Create(): void{
$this->Validate();

Expand All @@ -24,6 +30,9 @@ public function Create(): void{
$this->LocSubjectId = Db::GetLastInsertedId();
}

/**
* @throws \Exceptions\ValidationException
*/
public function GetByNameOrCreate(string $name): LocSubject{
$result = Db::Query('
SELECT *
Expand Down

0 comments on commit 406eff5

Please # to comment.