Tweaks suggested by IDE

This commit is contained in:
Daniel J. Summers 2023-11-25 09:24:17 -05:00
parent f8b5902aa1
commit 9af41447b7
9 changed files with 49 additions and 52 deletions

View File

@ -14,7 +14,7 @@ class Data
/**
* Ensure the table and index exist
*/
public static function startUp()
public static function startUp(): void
{
Definition::ensureTable(self::REQ_TABLE);
Definition::ensureIndex(self::REQ_TABLE, DocumentIndex::Optimized);

View File

@ -13,16 +13,16 @@ class Configuration
/** @var string $connectionString The connection string to use when establishing a database connection */
public static string $connectionString = "";
/** @var Connection $pgConn The active connection */
/** @var ?Connection $pgConn The active connection */
private static ?Connection $pgConn = null;
/** @var ?string $startUp The name of a function to run on first connection to the database */
public static ?string $startUp = null;
/**
* Ensure that the connection string is set, either explicity, by environment variables, or with defaults
* Ensure that the connection string is set, either explicitly, by environment variables, or with defaults
*/
private static function ensureConnectionString()
private static function ensureConnectionString(): void
{
if (self::$connectionString == "") {
$host = $_ENV['PGDOC_HOST'] ?? 'localhost';
@ -58,7 +58,7 @@ class Configuration
/**
* Close the PostgreSQL connection if it is open
*/
public static function closeConn()
public static function closeConn(): void
{
if (!is_null(self::$pgConn)) {
pg_close(self::$pgConn);

View File

@ -41,9 +41,9 @@ class Definition
*
* @param string $name The name of the table
*/
public static function ensureTable(string $name)
public static function ensureTable(string $name): void
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query(pg_conn(), self::createTable($name));
if ($result) pg_free_result($result);
}
@ -54,9 +54,9 @@ class Definition
* @param string $name The name of the table for which the index should be created
* @param DocumentIndex $type The type of index to create
*/
public static function ensureIndex(string $name, DocumentIndex $type)
public static function ensureIndex(string $name, DocumentIndex $type): void
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query(pg_conn(), self::createIndex($name, $type));
if ($result) pg_free_result($result);
}

View File

@ -50,9 +50,9 @@ class Document
* @param string $docId The ID of the document on which action should be taken
* @param array|object $document The array or object representing the document
*/
private static function executeNonQuery(string $query, string $docId, array|object $document)
private static function executeNonQuery(string $query, string $docId, array|object $document): void
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), $query, [ $docId, Query::jsonbDocParam($document) ]);
if ($result) pg_free_result($result);
}
@ -64,7 +64,7 @@ class Document
* @param string $docId The ID of the document to be inserted
* @param array|object $document The array or object representing the document
*/
public static function insert(string $tableName, string $docId, array|object $document)
public static function insert(string $tableName, string $docId, array|object $document): void
{
self::executeNonQuery(Query::insert($tableName), $docId, $document);
}
@ -76,7 +76,7 @@ class Document
* @param string $docId The ID of the document to be inserted
* @param array|object $document The array or object representing the document
*/
public static function save(string $tableName, string $docId, array|object $document)
public static function save(string $tableName, string $docId, array|object $document): void
{
self::executeNonQuery(Query::save($tableName), $docId, $document);
}
@ -90,7 +90,7 @@ class Document
*/
private static function runCount(string $sql, array $params): int
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), $sql, $params);
if (!$result) return -1;
$count = intval(pg_fetch_assoc($result)['it']);
@ -142,9 +142,9 @@ class Document
*/
private static function runExists(string $sql, array $params): bool
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), $sql, $params);
if (!$result) return -1;
if (!$result) return false;
$exists = boolval(pg_fetch_assoc($result)['it']);
pg_free_result($result);
return $exists;
@ -167,7 +167,7 @@ class Document
*
* @param string $tableName The name of the table in which existence should be checked
* @param array|object $criteria The criteria for the JSON containment query
* @return int True if any documents in the table match the JSON containment query, false if not
* @return bool True if any documents in the table match the JSON containment query, false if not
*/
public static function existsByContains(string $tableName, array|object $criteria): bool
{
@ -179,7 +179,7 @@ class Document
*
* @param string $tableName The name of the table in which existence should be checked
* @param string $jsonPath The JSON Path to be matched
* @return int True if any documents in the table match the JSON Path, false if not
* @return bool True if any documents in the table match the JSON Path, false if not
*/
public static function existsByJsonPath(string $tableName, string $jsonPath): bool
{
@ -196,7 +196,7 @@ class Document
*/
private static function runListQuery(string $sql, array $params, string $className): array
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), $sql, $params);
try {
if (!$result || pg_result_status($result) == PGSQL_EMPTY_QUERY) return [];
@ -294,7 +294,7 @@ class Document
* @param string $docId The ID of the document to be updated
* @param array|object $document The document to be updated
*/
public static function updateFull(string $tableName, string $docId, array|object $document)
public static function updateFull(string $tableName, string $docId, array|object $document): void
{
self::executeNonQuery(Query::updateFull($tableName), $docId, $document);
}
@ -306,7 +306,7 @@ class Document
* @param string $docId The ID of the document to be updated
* @param array|object $document The partial document to be updated
*/
public static function updatePartialById(string $tableName, string $docId, array|object $document)
public static function updatePartialById(string $tableName, string $docId, array|object $document): void
{
self::executeNonQuery(Query::updatePartialById($tableName), $docId, $document);
}
@ -318,9 +318,9 @@ class Document
* @param array|object $criteria The JSON containment criteria
* @param array|object $document The document to be updated
*/
public static function updatePartialByContains(string $tableName, array|object $criteria, array|object $document)
public static function updatePartialByContains(string $tableName, array|object $criteria, array|object $document): void
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), Query::updatePartialByContains($tableName),
[ Query::jsonbDocParam($criteria), Query::jsonbDocParam($document) ]);
if ($result) pg_free_result($result);
@ -333,9 +333,9 @@ class Document
* @param string $jsonPath The JSON Path to be matched
* @param array|object $document The document to be updated
*/
public static function updatePartialByJsonPath(string $tableName, string $jsonPath, array|object $document)
public static function updatePartialByJsonPath(string $tableName, string $jsonPath, array|object $document): void
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), Query::updatePartialByContains($tableName),
[ $jsonPath, Query::jsonbDocParam($document) ]);
if ($result) pg_free_result($result);
@ -347,7 +347,7 @@ class Document
* @param string $tableName The table from which a document should be deleted
* @param string $docId The ID of the document to be deleted
*/
public static function deleteById(string $tableName, string $docId)
public static function deleteById(string $tableName, string $docId): void
{
self::executeNonQuery(Query::deleteById($tableName), $docId, []);
}
@ -358,9 +358,9 @@ class Document
* @param string $tableName The table from which documents should be deleted
* @param array|object $criteria The criteria for the JSON containment query
*/
public static function deleteByContains(string $tableName, array|object $criteria)
public static function deleteByContains(string $tableName, array|object $criteria): void
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), Query::deleteByContains($tableName), [ Query::jsonbDocParam($criteria) ]);
if ($result) pg_free_result($result);
}
@ -371,9 +371,9 @@ class Document
* @param string $tableName The table from which documents should be deleted
* @param string $jsonPath The JSON Path expression to be matched
*/
public static function deleteByJsonPath(string $tableName, string $jsonPath)
public static function deleteByJsonPath(string $tableName, string $jsonPath): void
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), Query::deleteByJsonPath($tableName), [ $jsonPath ]);
if ($result) pg_free_result($result);
}
@ -389,7 +389,7 @@ class Document
*/
public static function customList(string $sql, array $params, string $className, callable $mapFunc): array
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), $sql, $params);
try {
if (!$result || pg_result_status($result) == PGSQL_EMPTY_QUERY) return [];
@ -420,9 +420,9 @@ class Document
* @param string $sql The SQL query to execute
* @param array $params A positional array of parameters for the SQL query
*/
public static function customNonQuery(string $sql, array $params)
public static function customNonQuery(string $sql, array $params): void
{
/** @var Result|bool */
/** @var Result|bool $result */
$result = pg_query_params(pg_conn(), $sql, $params);
if ($result) pg_free_result($result);
}

View File

@ -3,8 +3,6 @@ declare(strict_types=1);
namespace MyPrayerJournal\Domain;
use DateTimeImmutable, DateTimeZone;
/**
* A record of action taken on a prayer request, including updates to its text
*/

View File

@ -28,7 +28,7 @@ class JournalRequest extends AsOf
/** When this request will be shown again after having been snoozed */
public ?DateTimeImmutable $snoozedUntil = null;
/** When this request will be show agains after a non-immediate recurrence */
/** When this request will be show again after a non-immediate recurrence */
public ?DateTimeImmutable $showAfter = null;
/** The type of recurrence for this request */

View File

@ -3,7 +3,7 @@ declare(strict_types=1);
namespace MyPrayerJournal\Domain;
use DateTimeImmutable, DateTimeZone;
use DateTimeImmutable;
use Visus\Cuid2\Cuid2;
/**

View File

@ -39,7 +39,7 @@ Configuration::$startUp = '\MyPrayerJournal\Data::startUp';
/**
* Bring in a template
*/
function template(string $name)
function template(string $name): void
{
require_once __DIR__ . "/../templates/$name.php";
}
@ -49,7 +49,7 @@ function template(string $name)
*
* @param bool $fail Whether to fail the request (true) or redirect to log on (false - optional, default)
*/
function require_user(bool $fail = false)
function require_user(bool $fail = false): void
{
if (!array_key_exists(Constants::USER_ID, $_REQUEST)) {
if ($fail) {
@ -64,7 +64,7 @@ function require_user(bool $fail = false)
/**
* Write a bare header for a component result
*/
function bare_header()
function bare_header(): void
{
echo '<!DOCTYPE html><html lang="en"><head><meta charset="utf8"><title></title></head><body>';
}
@ -76,11 +76,11 @@ function bare_header()
* @param array $classNames CSS class names to be applied to the link (optional, default none)
* @param bool $checkActive Whether to apply an active class if the route matches (optional, default false)
*/
function page_link(string $url, array $classNames = [], bool $checkActive = false)
function page_link(string $url, array $classNames = [], bool $checkActive = false): void
{
echo 'href="'. $url . '" hx-get="' . $url . '"';
if ($checkActive && str_starts_with($_SERVER[Constants::REQUEST_URI], $url)) {
array_push($classNames, 'is-active-route');
$classNames[] = 'is-active-route';
}
if (!empty($classNames)) {
echo sprintf(' class="%s"', implode(' ', $classNames));
@ -91,7 +91,7 @@ function page_link(string $url, array $classNames = [], bool $checkActive = fals
/**
* Close any open database connection; close the `body` and `html` tags
*/
function end_request()
function end_request(): void
{
Configuration::closeConn();
echo '</body></html>';
@ -99,7 +99,7 @@ function end_request()
/**
* Create a new instance of the Unix epoch
*
*
* @return DateTimeImmutable An immutable date/time as of the Unix epoch
*/
function unix_epoch(): DateTimeImmutable

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
require_once '../../lib/start.php';
use DateTimeImmutable;
use MyPrayerJournal\{ Constants, Data, Dates };
use MyPrayerJournal\Domain\JournalRequest;
@ -38,7 +37,7 @@ end_request();
* @param string $activity The activity performed (activity or prayed)
* @param DateTimeImmutable $asOf The date/time the activity was performed
*/
function format_activity(string $activity, DateTimeImmutable $asOf)
function format_activity(string $activity, DateTimeImmutable $asOf): void
{
echo sprintf('last %s <span title="%s">%s</span>', $activity,
$asOf->setTimezone($_REQUEST[Constants::TIME_ZONE])->format('l, F jS, Y/g:ia T'),
@ -50,13 +49,13 @@ function format_activity(string $activity, DateTimeImmutable $asOf)
*
* @param JournalRequest $req The request for which a card should be generated
*/
function journal_card(JournalRequest $req)
function journal_card(JournalRequest $req): void
{
$spacer = '<span>&nbsp;</span>'; ?>
<div class="col">
<div class="card h-100">
<div class="card-header p-0 d-flex" role="tool-bar">
<a <?php page_link("/request/{$req->id}/edit"); ?> class="btn btn-secondary" title="Edit Request">
<div class="card-header p-0 d-flex" role="toolbar">
<a <?php page_link("/request/edit?{$req->id}"); ?> class="button btn-secondary" title="Edit Request">
<span class="material-icons">edit</span>
</a><?php echo $spacer; ?>
<button type="button" class="btn btn-secondary" title="Add Notes" data-bs-toggle="modal"
@ -70,10 +69,10 @@ function journal_card(JournalRequest $req)
<span class="material-icons">schedule</span>
</button>
<div class="flex-grow-1"></div>
<button type="button" class="btn btn-success w-25" hx-patch="/request/<?php echo $req->id; ?>/prayed"
title="Mark as Prayed">
<a href="/request/prayed?<?php echo $req->id; ?>" class="button btn-success w-25"
hx-patch="/request/prayed?<?php echo $req->id; ?>" title="Mark as Prayed">
<span class="material-icons">done</span>
</button>
</a>
</div>
<div class="card-body">
<p class="request-text"><?php echo htmlentities($req->text); ?></p>