Skip to content

Commit

Permalink
Add type hints for remaining classes
Browse files Browse the repository at this point in the history
  • Loading branch information
acabal committed Jan 8, 2024
1 parent d771821 commit 783c098
Show file tree
Hide file tree
Showing 35 changed files with 212 additions and 210 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ Before submitting design contributions, please discuss them with the Standard Eb
### Main website
- Add type hints for class variables.
- Convert ebooks from being stored in an APCu cache to our MariaDB database. (This is a big project!)
- Creating a search bar for the SE Manual of Style.
Expand Down Expand Up @@ -186,7 +184,7 @@ Before submitting design contributions, please discuss them with the Standard Eb
- Check for `null` using `===` and `!==`.
- Where possible, include type hints for functions. Due to PHP limitations this may not always be possible, for example in cases where `null` may be passed or returned.
- Where possible, include type hints for class properties and all functions.
- If using regex to parse HTML, use `|` as the regex delimiter instead of `/`.
Expand Down
5 changes: 2 additions & 3 deletions lib/ArtworkTag.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?

class ArtworkTag extends Tag{
// *******
// GETTERS
Expand All @@ -19,11 +18,11 @@ protected function GetUrl(): string{
protected function Validate(): void{
$error = new Exceptions\ValidationException();

if($this->Name === null || strlen($this->Name) === 0){
if(strlen($this->Name) == 0){
$error->Add(new Exceptions\InvalidArtworkTagException());
}

if($this->Url === null || strlen($this->Url) === 0){
if($this->Url === null || strlen($this->Url) == 0){
$error->Add(new Exceptions\InvalidArtworkTagException());
}

Expand Down
12 changes: 3 additions & 9 deletions lib/AtomFeed.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<?
use Safe\DateTime;
use function Safe\file_get_contents;
use function Safe\file_put_contents;
use function Safe\preg_replace;
use function Safe\rename;
use function Safe\tempnam;
use function Safe\unlink;

class AtomFeed extends Feed{
public $Id;
public $Updated = null;
public $Subtitle = null;
public string $Id;
public ?DateTime $Updated = null;
public ?string $Subtitle = null;

/**
* @param string $title
Expand All @@ -26,7 +21,6 @@ public function __construct(string $title, string $subtitle, string $url, string
$this->Stylesheet = SITE_URL . '/feeds/atom/style';
}


// *******
// METHODS
// *******
Expand Down
10 changes: 5 additions & 5 deletions lib/Benefits.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?
class Benefits{
public $CanAccessFeeds = false;
public $CanVote = false;
public $CanBulkDownload = false;
public $CanUploadArtwork = false;
public $CanReviewArtwork = false;
public bool $CanAccessFeeds = false;
public bool $CanVote = false;
public bool $CanBulkDownload = false;
public bool $CanUploadArtwork = false;
public bool $CanReviewArtwork = false;
}
9 changes: 4 additions & 5 deletions lib/Collection.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?

use function Safe\preg_replace;

class Collection{
public $Name;
public $Url;
public $SequenceNumber = null;
public $Type = null;
public string $Name;
public string $Url;
public ?int $SequenceNumber = null;
public ?string $Type = null;

public function __construct(string $name){
$this->Name = $name;
Expand Down
14 changes: 7 additions & 7 deletions lib/Contributor.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?
class Contributor{
public $Name;
public $UrlName;
public $SortName;
public $WikipediaUrl;
public $MarcRole;
public $FullName;
public $NacoafUrl;
public string $Name;
public string $UrlName;
public ?string $SortName;
public ?string $WikipediaUrl;
public ?string $MarcRole;
public ?string $FullName;
public ?string $NacoafUrl;

public function __construct(string $name, string $sortName = null, string $fullName = null, string $wikipediaUrl = null, string $marcRole = null, string $nacoafUrl = null){
$this->Name = str_replace('\'', '', $name);
Expand Down
1 change: 0 additions & 1 deletion lib/Db.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?

class Db{
public static function GetLastInsertedId(): int{
return $GLOBALS['DbConnection']->GetLastInsertedId();
Expand Down
22 changes: 15 additions & 7 deletions lib/DbConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
use function Safe\posix_getpwuid;

class DbConnection{
private $_link = null;
public $IsConnected = false;
public $QueryCount = 0;
public $LastQueryAffectedRowCount = 0;
private ?\PDO $_link = null;
public int $QueryCount = 0;
public int $LastQueryAffectedRowCount = 0;

public function __construct(?string $defaultDatabase = null, string $host = 'localhost', ?string $user = null, string$password = '', bool $forceUtf8 = true, bool $require = true){
if($user === null){
Expand Down Expand Up @@ -51,8 +50,6 @@ public function __construct(?string $defaultDatabase = null, string $host = 'loc

// We can't use persistent connections (connection pooling) because we would have race condition problems with last_insert_id()
$this->_link = new \PDO($connectionString, $user, $password, $params);

$this->IsConnected = true;
}
catch(Exception $ex){
if(SITE_STATUS == SITE_STATUS_DEV){
Expand All @@ -79,7 +76,7 @@ public function __construct(?string $defaultDatabase = null, string $host = 'loc
* @return Array<mixed>
*/
public function Query(string $sql, array $params = [], string $class = 'stdClass'): array{
if(!$this->IsConnected){
if($this->_link === null){
return [];
}

Expand Down Expand Up @@ -245,8 +242,19 @@ private function ExecuteQuery(PDOStatement $handle, string $class = 'stdClass'):

// Gets the last auto-increment id
public function GetLastInsertedId(): ?int{
if($this->_link === null){
return null;
}

$id = $this->_link->lastInsertId();

if($id === false){
return null;
}
else{
$id = (int)$id;
}

if($id == 0){
return null;
}
Expand Down
106 changes: 59 additions & 47 deletions lib/Ebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,70 @@
use function Safe\shell_exec;
use function Safe\substr;

/**
* @property array<GitCommit> $GitCommits
* @property array<EbookTag> $EbookTags
* @property array<string> $LocTags
* @property array<Collection> $Collections
* @property array<EbookSource> $Sources
* @property array<Contributor> $Authors
* @property array<Contributor> $Illustrators
* @property array<Contributor> $Translators
* @property array<Contributor> $Contributors
* @property ?array<string> $TocEntries
*/
class Ebook{
public $WwwFilesystemPath;
public $RepoFilesystemPath;
public $Url;
public $KindleCoverUrl;
public $EpubUrl;
public $AdvancedEpubUrl;
public $KepubUrl;
public $Azw3Url;
public $HasDownloads;
public string $WwwFilesystemPath;
public string $RepoFilesystemPath;
public string $Url;
public string $KindleCoverUrl;
public string $EpubUrl;
public string $AdvancedEpubUrl;
public string $KepubUrl;
public string $Azw3Url;
public bool $HasDownloads;
public $GitCommits = [];
public $Tags = [];
public $LocTags = [];
public $Collections = [];
public $Identifier;
public $UrlSafeIdentifier;
public $HeroImageUrl;
public $HeroImageAvifUrl;
public $HeroImage2xUrl;
public $HeroImage2xAvifUrl;
public $CoverImageUrl;
public $CoverImageAvifUrl;
public $CoverImage2xUrl;
public $CoverImage2xAvifUrl;
public $DistCoverUrl;
public $Title;
public $FullTitle;
public $AlternateTitle;
public $Description;
public $LongDescription;
public $Language;
public $WordCount;
public $ReadingEase;
public $ReadingEaseDescription;
public $ReadingTime;
public $GitHubUrl;
public $WikipediaUrl;
public string $Identifier;
public string $UrlSafeIdentifier;
public string $HeroImageUrl;
public string $HeroImageAvifUrl;
public string $HeroImage2xUrl;
public string $HeroImage2xAvifUrl;
public string $CoverImageUrl;
public string $CoverImageAvifUrl;
public string $CoverImage2xUrl;
public string $CoverImage2xAvifUrl;
public string $DistCoverUrl;
public ?string $Title = null;
public ?string $FullTitle = null;
public ?string $AlternateTitle = null;
public ?string $Description = null;
public ?string $LongDescription = null;
public ?string $Language = null;
public int $WordCount;
public float $ReadingEase;
public string $ReadingEaseDescription;
public string $ReadingTime;
public ?string $GitHubUrl = null;
public ?string $WikipediaUrl = null;
public $Sources = [];
public $Authors = []; // Array of Contributors
public $AuthorsHtml;
public $AuthorsUrl; // This is a single URL even if there are multiple authors; for example, /ebooks/karl-marx_friedrich-engels/
public $Illustrators = []; // Array of Contributors
public $Translators = []; // Array of Contributors
public $Contributors = []; // Array of Contributors
public $ContributorsHtml;
public $TitleWithCreditsHtml = '';
public $Created;
public $Updated;
public $TextUrl;
public $TextSinglePageUrl;
public $TextSinglePageSizeNumber = null;
public $TextSinglePageSizeUnit = null;
public $Authors = [];
public string $AuthorsHtml;
public string $AuthorsUrl; // This is a single URL even if there are multiple authors; for example, /ebooks/karl-marx_friedrich-engels/
public $Illustrators = [];
public $Translators = [];
public $Contributors = [];
public ?string $ContributorsHtml = null;
public string $TitleWithCreditsHtml = '';
public DateTime $Created;
public DateTime $Updated;
public string $TextUrl;
public string $TextSinglePageUrl;
public ?string $TextSinglePageSizeNumber = null;
public ?string $TextSinglePageSizeUnit = null;
public $TocEntries = null; // A list of non-Roman ToC entries ONLY IF the work has the 'se:is-a-collection' metadata element, null otherwise

public function __construct(?string $wwwFilesystemPath = null){
Expand Down Expand Up @@ -384,7 +396,7 @@ public function __construct(?string $wwwFilesystemPath = null){

// Figure out the reading time.
$readingTime = ceil($this->WordCount / AVERAGE_READING_WORDS_PER_MINUTE);
$this->ReadingTime = $readingTime;
$this->ReadingTime = (string)$readingTime;

if($readingTime < 60){
$this->ReadingTime .= ' minute';
Expand Down
4 changes: 2 additions & 2 deletions lib/EbookSource.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?
class EbookSource{
public $Type;
public $Url;
public int $Type;
public string $Url;

public function __construct(int $type, string $url){
$this->Type = $type;
Expand Down
1 change: 0 additions & 1 deletion lib/EbookTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public function __construct(string $name){
$this->_Url = '/subjects/' . $this->UrlName;
}


// *******
// GETTERS
// *******
Expand Down
26 changes: 13 additions & 13 deletions lib/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

use function Safe\define;
use function Safe\file_get_contents;

/**
* @property array<array<string>> $Attachments
*/
class Email{
public $To = '';
public $ToName = '';
public $From = '';
public $FromName = '';
public $ReplyTo = '';
public $Subject = '';
public $Body = '';
public $TextBody = '';
public string $To = '';
public string $ToName = '';
public string $From = '';
public string $FromName = '';
public string $ReplyTo = '';
public string $Subject = '';
public string $Body = '';
public string $TextBody = '';
public $Attachments = [];
public $PostmarkStream = null;
public ?string $PostmarkStream = null;

public function __construct(bool $isNoReplyEmail = false){
if($isNoReplyEmail){
Expand All @@ -35,7 +35,7 @@ public function Send(): bool{
$this->ReplyTo = $this->From;
}

if($this->To === null || $this->To == ''){
if($this->To == ''){
return false;
}

Expand Down
14 changes: 8 additions & 6 deletions lib/Feed.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?
use Safe\DateTime;
use function Safe\exec;
use function Safe\file_get_contents;
use function Safe\file_put_contents;
use function Safe\tempnam;
use function Safe\unlink;

/**
* @param array<string> $Entries
*/
class Feed{
public $Url;
public $Title;
public string $Url;
public string $Title;
public $Entries = [];
public $Path = null;
public $Stylesheet = null;
protected $XmlString = null;
public string $Path;
public ?string $Stylesheet = null;
protected ?string $XmlString = null;

/**
* @param string $title
Expand Down
Loading

0 comments on commit 783c098

Please sign in to comment.