From 5b2557c8587a36aea357268be7b13e7058f0f003 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Fri, 26 Jun 2020 12:52:44 -0500 Subject: [PATCH] Add more type checking to satisfy PHPStan and update some PHPStan exceptions --- config/phpstan/phpstan.neon | 6 ++++++ lib/OpdsFeed.php | 33 ++++++++++++++++++++++++++++----- scripts/generate-opds.php | 5 +++-- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/config/phpstan/phpstan.neon b/config/phpstan/phpstan.neon index cd07e7b6..bef367ab 100644 --- a/config/phpstan/phpstan.neon +++ b/config/phpstan/phpstan.neon @@ -13,6 +13,12 @@ parameters: # Ignore errors caused by missing phpdoc strings for arrays - '#Method .+? has parameter .+? with no value type specified in iterable type array.#' + + # Ignore errors caused by type hints that should be union types. Union types are not yet supported in PHP. + - '#Function vd(s|d)?\(\) has parameter \$var with no typehint specified.#' + - '#Method Ebook::NullIfEmpty\(\) has parameter \$elements with no typehint specified.#' + - '#Method HttpInput::GetHttpVar\(\) has no return typehint specified.#' + - '#Method HttpInput::GetHttpVar\(\) has parameter \$default with no typehint specified.#' level: 7 paths: diff --git a/lib/OpdsFeed.php b/lib/OpdsFeed.php index fc547952..26d3dd9d 100644 --- a/lib/OpdsFeed.php +++ b/lib/OpdsFeed.php @@ -1,6 +1,7 @@ registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/'); $xml->registerXPathNamespace('schema', 'http://schema.org/'); - $entries = $xml->xpath('/feed/entry') ?? []; + $entries = $xml->xpath('/feed/entry'); + + if($entries === false){ + $entries = []; + } $output = ''; foreach($entries as $entry){ // Remove any elements, we don't want to compare against those. // This makes it easier to for example generate a new subjects index, // while updating it at the same time. - foreach($xml->xpath('/feed/entry/updated') as $element){ + $elements = $xml->xpath('/feed/entry/updated'); + + if($elements === false){ + $elements = []; + } + + foreach($elements as $element){ unset($element[0]); } @@ -58,9 +69,21 @@ class OpdsFeed{ } $xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents($parentFilepath))); - $feedEntry = ($xml->xpath('/feed/entry[id="' . $this->Id . '"]/updated') ?? [])[0]; - $feedEntry[0] = $updatedTimestamp; - file_put_contents($parentFilepath, str_replace(" ns=", " xmlns=", $xml->asXml() ?? '')); + $feedEntries = $xml->xpath('/feed/entry[id="' . $this->Id . '"]/updated'); + if($feedEntries === false){ + $feedEntries = []; + } + + if(sizeof($feedEntries) > 0){ + $feedEntries[0][0] = $updatedTimestamp; + } + + $xmlString = $xml->asXml(); + if($xmlString === false){ + $xmlString = ''; + } + + file_put_contents($parentFilepath, str_replace(" ns=", " xmlns=", $xmlString)); rename($tempFilename, $path); } diff --git a/scripts/generate-opds.php b/scripts/generate-opds.php index c919948e..69cb981f 100644 --- a/scripts/generate-opds.php +++ b/scripts/generate-opds.php @@ -4,6 +4,7 @@ require_once('/standardebooks.org/web/lib/Core.php'); use function Safe\krsort; use function Safe\getopt; use function Safe\preg_replace; +use function Safe\sort; $longopts = array("webroot:", "weburl:"); $options = getopt("", $longopts); @@ -51,8 +52,8 @@ $subjectsFeed->Save(WEB_ROOT . '/opds/subjects/index.xml'); // Now generate each individual subject feed foreach($ebooksBySubject as $subject => $ebooks){ krsort($ebooks); - $subjectFeed = new OpdsAcquisitionFeed('/opds/subjects/' . Formatter::MakeUrlSafe($subject), $subject, '/opds/subjects', $ebooks); - $subjectFeed->Save(WEB_ROOT . '/opds/subjects/' . Formatter::MakeUrlSafe($subject) . '.xml'); + $subjectFeed = new OpdsAcquisitionFeed('/opds/subjects/' . Formatter::MakeUrlSafe((string)$subject), (string)$subject, '/opds/subjects', $ebooks); + $subjectFeed->Save(WEB_ROOT . '/opds/subjects/' . Formatter::MakeUrlSafe((string)$subject) . '.xml'); } // Create the 'all' feed